#n = num of nodes
#edge = nodes-size struct .in / .out list of in = out edges, 
#nextHop = edgesxnodes if that edge nexthop to that node
# event = Nx4 list of time/source/dest/size (sorted acc to time)
function packetTimes=trafficFlowSim(id,n,m,edge,capacity,buffer,nextHop, eventlist)
  packetTimes=[];
  nodes=1:n;
  m= length(capacity); # num of edges
  traffic=zeros(n,n,m); # list of traffic matrices on edges
  temp=zeros(n,n,n); # 
  load=zeros(m,n); # load on the edges according to the sources
  target=zeros(1,n); # actual target
  inSource=zeros(1,n); # packets waiting at source
  inSystem=zeros(1,n); # packets waiting at source or during process in the network
  arrivalTime=zeros(1,n);
  lasttime=0;
  
  
  for u=nodes
    event(u).l=[];
  endfor
  for i=1:rows(eventlist)
    ev=eventlist(i,:);
    event(ev(2)).l(end+1,:)=ev([1,3,4]); 
  endfor
  
#  disp(eventlist);
#  for l=event disp(l); endfor
  
  
  t=0;
  while(t<1000)
#    disp(target);
#    disp(length(temp));
    #init temp
    for u=nodes
      temp(:,:,u)=zeros(n);  
    endfor


    
    #source
    for u=nodes
      if(target(u)!=0)
        x=floor(strat(id,inSource,sum(load'),load(:,u)',capacity,buffer));
        if(x<0) x=0;
        elseif (x>inSource(u)) x=inSource(u);
        endif
        temp(u,target(u),u)=x;
        inSource(u)-=x;
      endif;  
    endfor

#    disp(t);
#    disp(length(temp));
    
    
    #into temp
    for u=nodes
      M=zeros(n);
      for e=edge(u).in
        if(sum(load(e,:))<=capacity(e))
          D=traffic(:,:,e);
        else
          D=floor(traffic(:,:,e)*capacity(e)/sum(load(e,:)));  
        endif
        traffic(:,:,e)-=D;
        load(e,:)-=sum(D');
        M+=D;
      endfor
      temp(:,:,u)+=M;
    endfor
    
    #destinations
    for u=nodes
      for s=nodes
        inSystem(s)-=temp(s,u,u); 
      endfor
      temp(:,u,u)=zeros(n,1);
    endfor
    
    #outfrom temp
    for u=nodes
      for e=edge(u).out
        A=ones(n,1)*nextHop(e,:);
        T=A.*temp(:,:,u);
        if(sum(sum(T))<=buffer(e)-sum(load(e,:)))
          D=T;
        else 
          D=floor(T*(buffer(e)-sum(load(e,:)))/sum(sum(T)));
        endif
        traffic(:,:,e)+=D;
        load(e,:)+=sum(D');
        temp(:,:,u)-=D;
      endfor
    endfor

    #lost
    for s=nodes
      inSource(s)+=sum(sum(temp(s,:,:)));       
    endfor
    
#    if(t==0)
#      arrivalTime=ones(1,6);
#      inSource = 5000*ones(1,6);
#      inSystem = 5000*ones(1,6);
#      target= [2,4,1,3,0,0];
#      target= [3,4,1,3,0,0];
#    endif

#  if(t<10) disp(t); disp(traffic); endif
    
    for u=nodes
      if (inSystem(u)==0)
        if(arrivalTime(u)!=0)
          packetTimes(end+1,:)=[t, u,target(u),t-arrivalTime(u)];
          lasttime=t;
          arrivalTime(u)=0;
        endif
        if(length(event(u).l)>0)
          if(event(u).l(1,1)<=t)
            target(u)=event(u).l(1,2);
            inSource(u)=event(u).l(1,3);
            inSystem(u)=event(u).l(1,3);
            arrivalTime(u)=t+1;
            event(u).l(1,:)=[];
#            disp(u);
#            disp(event(u));        
          endif
        endif    
#        if(arrivalTime(u)!=0)
#          packetTimes(end+1,:)=[u,target(u),t-arrivalTime(u)];
#        endif
#        target(u)=randi(3);
#        if (u==target(u)) target(u)=4; endif;
#        inSource(u)=5000;
#        inSystem(u)=5000;
#        arrivalTime(u)=t+1;
      endif
    endfor

    t++;    
  endwhile
    
#  disp(lasttime)
endfunction