%procedure:
%
%init
%  load up an image of the underlying map (i.e. poli map of the world)
%  determine the positions of servers: read from some file
%
%  generate some random client positions [only on land, not ocean]
%  generate some packets (i.e. queues of servers to visit)
%
%sim
%  walk packets from their start positions to destinations
%  walk the packets back
%
%  with each step, generate new packets
%

%number of clients and servers
CLIENTS = 64;
SERVERS = 64;

%load matrix of geolocated tor routers
load 'torServers';

%load coastline data
load coast;

clients = zeros(CLIENTS, 2);
endServers = zeros(SERVERS, 2);
%make some random clients and servers
for i = 1:CLIENTS;
  clients(i,:) = gfx.randomPoint(lat, long);
  endServers(i,:) = gfx.randomPoint(lat, long);
end

packets = {};

%init packets
%create a packet for each client
for i = 1:size(clients, 1);
  packets{i} = Packet([ ...
    clients(i,:); ...
    torServers(randi(size(torServers, 1), 1, 1),:); ...
    torServers(randi(size(torServers, 1), 1, 1),:); ...
    torServers(randi(size(torServers, 1), 1, 1),:); ...
    endServers(randi(size(endServers, 1), 1, 1),:); ...
    ], 1);
end

%load graphics
GFX = gfx([lat long], clients, endServers, torServers);

%begin simulation
for i=1:1000; %simulate for 1000 timesteps
  %redraw
  GFX.update(packets);
  saveas(gcf, ['f', sprintf('%03d', i), '.png']);
  pause(.02);

  idx = 1;
  newPackets = {};
  %advance each packet
  for i = 1:size(packets, 2);
    o = packets{i}.advance();
    %add new packets
    for idy = 1:size(o,2);
      newPackets{idx} = o{idy};
      idx = idx+1;
    end
  end
  packets = newPackets;
end
