% the stat_e.txt file should be a copy of the stat-* file wrote by the python modules

load stat_e.txt
l=length(stat_e(:,1));


% fix the end of routes not ended in the trace

last_full_entry=find(stat_e(:,4)==-1,1)-1;
last_second=max(stat_e(:,2));
last_usecond=max(stat_e(:,3));
stat_e(last_full_entry+1:end,4)=last_second*ones(l-last_full_entry,1);
stat_e(last_full_entry+1:end,5)=last_usecond*ones(l-last_full_entry,1);

stat_e=[stat_e (stat_e(:,2)*1e6+stat_e(:,3)) ((stat_e(:,4)-stat_e(:,2))*1e6+(stat_e(:,5)-stat_e(:,3)))  (stat_e(:,4)*1e6+stat_e(:,5))];

% evaluation of number of active routes 
arrival_events_no=[ones(l,1) stat_e(:,1) stat_e(:,6) stat_e(:,7)]; %not time ordered
arrival_events=sortrows(arrival_events_no,3); % time ordered
departure_events_no=[-1*ones(l,1) stat_e(:,1) stat_e(:,8) stat_e(:,7)]; %not time ordered
ev=[arrival_events_no;departure_events_no];
ev=sortrows(ev,3);
ao=cumsum(ev(:,1));
max_index=find(ev(:,3)>ev(end,3)-1e6,1); % remove the last five seconds from the analysis
min_index=find(ev(:,3)>ev(1,3)+1e6,1); % remove the first five seconds from the analysis
ro=mean(ao(min_index:max_index));
max_ao=max(ao(min_index:max_index));
plot(ao)
hold on
fprintf('Average number of active routes %d \n',ro);

max_index=find(arrival_events(:,3)>arrival_events(end,3)-5e6,1);
min_index=find(arrival_events(:,3)>arrival_events(1,3)+5e6,1);
fprintf('Average active-route inter-time %d \n',mean(diff(arrival_events(min_index:max_index,3))));

uniq_np=numel(unique(arrival_events(min_index:max_index,2)));

nlookups_v=[];

m_range=[max_ao:(uniq_np-max_ao)/5:uniq_np];
max_index=find(arrival_events(:,3)>arrival_events(end,3)-1e6,1);
min_index=find(arrival_events(:,3)>arrival_events(1,3)+1e6,1);
for m=m_range
    arrived=0;
    nlookups=0; 
    s=zeros(m,2);   % vector containig the id of the route contained in  servers and the next free time of  servers 
    for i=1:max_index
        arrived=arrived+1;
        now=arrival_events(i,3);
        si=find(s(:,1)==arrival_events(i,2),1);
        if (numel(si)>0)
          % route already contained in the FIB
          if ((s(si,2)>now))
            fprintf('Warning route aggregation');
            return;
          end
          s(si,2)=now+arrival_events(i,4);
          continue;
        end
        % route not contained in the FIB
        
        if i==min_index
           arrived=0;
           nlookups=0;
        end
        [min_value,min_i]=min(s);
        if (min_value<now)
            %server #min_index is free then occupied by current arrival
            s(min_i,1)=arrival_events(i,2);
            s(min_i,2)=now+arrival_events(i,4);
            nlookups=nlookups+1;
        else
            %servers all all busy and the current arrival, virtually occupy the first
            %server that will be free
            fprintf('Warning critical route blocking');
            return;
            %s(min_index)=s(min_index)+arrival_events(i,4);
        end
    end
    nlookups_v=[nlookups_v nlookups];
end
figure
duration=(arrival_events(max_index,3)-arrival_events(min_index,3))/1e6;
plot(m_range,nlookups_v/duration,'b')