source: xssim/src/gridsim/gssim/network/flow/reservation/GSSIMFlowLink.java @ 104

Revision 104, 53.9 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package gridsim.gssim.network.flow.reservation;
2
3import eduni.simjava.Sim_entity;
4import eduni.simjava.Sim_event;
5import eduni.simjava.Sim_system;
6import gridsim.Gridlet;
7import gridsim.ParameterException;
8import gridsim.gssim.network.ExtendedGridSim;
9import gridsim.gssim.network.ExtendedGridSimTags;
10import gridsim.net.Link;
11import gridsim.net.Packet;
12import gssim.schedframe.scheduling.plugin.grid.network.manager.ReservationIntervalCharacteristic;
13import gssim.schedframe.scheduling.plugin.grid.network.manager.NetworkData;
14import gssim.schedframe.scheduling.plugin.grid.network.manager.NetworkGridlet;
15import gssim.schedframe.scheduling.plugin.grid.network.manager.NetworkReservationObject;
16
17import java.util.ArrayList;
18import java.util.Collections;
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.LinkedHashSet;
22import java.util.List;
23import java.util.Map;
24import java.util.TreeMap;
25import java.util.Vector;
26
27import org.joda.time.DateTime;
28
29import schedframe.net.pce.topology.AvailableBandwidth;
30
31
32public class GSSIMFlowLink extends Link {
33
34        private Vector q_;
35        private HashMap activeFlows_;
36        private double lastUpdateTime_;
37        private int inEnd1_;
38        private int outEnd1_;
39        private int inEnd2_;
40        private int outEnd2_;
41
42        public static double BUFFER_BAUDRATE = 0;
43       
44        private Vector qFlowsUsingReservation;
45        private HashMap reservationID_activeFlowsUsingReservation;
46        private List<NetworkReservationObject> reservationList;
47               
48        private double activeReservationsBandwidth;
49        private double lastUpdateTimeReservation;
50        private double startBaudRate;
51        private double lastBaudRate;
52       
53        private List<AvailableBandwidth> bandwidthCalendar;
54       
55        private Sim_entity point1;
56        private Sim_entity point2;
57        private String point1Name;
58        private String point2Name;
59
60        private List<NetworkReservationObject> completedReservationsList;
61        private ArrayList flowsSizeCharacteristic;
62        private HashMap flowsUsingReservationsSizeCharacteristic;
63       
64        public GSSIMFlowLink(String name, double baudRate, double propDelay, int MTU)
65                        throws ParameterException, NullPointerException {
66                super(name, baudRate, propDelay, MTU);
67                init();
68        }
69
70        private void init() {
71                lastUpdateTime_ = 0.0;
72                q_ = new Vector();
73                activeFlows_ = new HashMap();
74                inEnd1_ = -1;
75                outEnd1_ = -1;
76                inEnd2_ = -1;
77                outEnd2_ = -1;
78
79                qFlowsUsingReservation = new Vector();
80                reservationID_activeFlowsUsingReservation = new HashMap();
81                reservationList = new ArrayList<NetworkReservationObject>();
82                activeReservationsBandwidth = 0;
83                lastUpdateTimeReservation = 0.0;
84                startBaudRate = getBaudRate();
85                lastBaudRate = getBaudRate();
86                bandwidthCalendar = new ArrayList<AvailableBandwidth>();
87                bandwidthCalendar.add(new AvailableBandwidth(0, (long)getStartBaduRate()));
88               
89                point1 = null;
90                point2 = null;
91                point1Name = null;
92                point2Name = null;
93               
94                completedReservationsList = new ArrayList<NetworkReservationObject>();
95                flowsSizeCharacteristic = new ArrayList();
96                flowsUsingReservationsSizeCharacteristic = new HashMap();
97        }
98
99        public void attach(Sim_entity end1, Sim_entity end2) {
100                if (end1 == null || end2 == null) {
101                        System.out.println(super.get_name() + ".attach(): Warning - "
102                                        + "one or both entities are null.");
103                        return;
104                }
105
106                inEnd1_ = ExtendedGridSim.getEntityId("Input_" + end1.get_name());
107                outEnd1_ = ExtendedGridSim.getEntityId("Output_" + end1.get_name());
108
109                // if end1 is a router/gateway with no Input and Output port
110                if (inEnd1_ == -1 || outEnd1_ == -1) {
111                        inEnd1_ = end1.get_id();
112                        outEnd1_ = end1.get_id();
113                }
114
115                inEnd2_ = ExtendedGridSim.getEntityId("Input_" + end2.get_name());
116                outEnd2_ = ExtendedGridSim.getEntityId("Output_" + end2.get_name());
117
118                // if end1 is a router/gateway with no Input and Output port
119                if (inEnd2_ == -1 || outEnd2_ == -1) {
120                        inEnd2_ = end2.get_id();
121                        outEnd2_ = end2.get_id();
122                }
123        }
124
125        public void attach(String end1, String end2) {
126                if (end1 == null || end2 == null) {
127                        System.out.println(super.get_name() + ".attach(): Warning - "
128                                        + "can not connect since one or both entities are null.");
129                        return;
130                }
131
132                if (end1.length() == 0 || end2.length() == 0) {
133                        System.out.println(super.get_name() + ".attach(): Warning - "
134                                        + "can not connect since one or both entities are null.");
135                        return;
136                }
137
138                inEnd1_ = ExtendedGridSim.getEntityId("Input_" + end1);
139                outEnd1_ = ExtendedGridSim.getEntityId("Output_" + end1);
140
141                // if end1 is a router/gateway with no Input and Output port
142                if (inEnd1_ == -1 || outEnd1_ == -1) {
143                        inEnd1_ = ExtendedGridSim.getEntityId(end1);
144                        outEnd1_ = inEnd1_;
145                }
146
147                inEnd2_ = ExtendedGridSim.getEntityId("Input_" + end2);
148                outEnd2_ = ExtendedGridSim.getEntityId("Output_" + end2);
149
150                // if end1 is a router/gateway with no Input and Output port
151                if (inEnd2_ == -1 || outEnd2_ == -1) {
152                        inEnd2_ = ExtendedGridSim.getEntityId(end1);
153                        outEnd2_ = inEnd2_;
154                }
155        }
156
157        public void body() {
158                // register oneself to the system GIS
159                super.sim_schedule(ExtendedGridSim.getGridInfoServiceEntityId(),
160                                ExtendedGridSimTags.SCHEDULE_NOW, ExtendedGridSimTags.REGISTER_LINK,
161                                new Integer(super.get_id()));
162
163                Sim_event ev = new Sim_event();
164
165                while (Sim_system.running()) {
166
167                        super.sim_get_next(ev);
168                        //super.sim_wait_for(1, ev);
169                        //checkReservations();
170                        // System.out.println(super.get_name() + ".body(): ev.get_tag() is "
171                        // + ev.get_tag());
172                        // System.out.println(super.get_name() + ".body(): ev.get_src() is "
173                        // + ev.get_src());
174
175                        // if the simulation finishes then exit the loop
176                        if (ev.get_tag() == ExtendedGridSimTags.END_OF_SIMULATION) {
177
178                                break;
179                        }
180                        if (ev.get_tag() == ExtendedGridSimTags.UNKNOWN) {
181                                continue;
182                        }
183                        // process the received event
184                        // System.out.println(super.get_name() + ".body(): processEvent() at
185                        // time = " + GridSim.clock());
186                        processEvent(ev);
187                        sim_completed(ev);
188
189                }
190
191                while (sim_waiting() > 0) {
192                        // wait for event and ignore
193                        // System.out.println(super.get_name() + ".body(): Ignore !!");
194                        sim_get_next(ev);
195                }
196
197                // System.out.println(super.get_name() + ":%%%% Exiting body() at time "
198                // +
199                // GridSim.clock() );
200        }
201
202        private void processEvent(Sim_event ev) {
203                //System.out.println(get_name()+"LINK - PROCESS EVENT"+ev.get_tag());
204                switch (ev.get_tag()) {
205                case ExtendedGridSimTags.PKT_FORWARD: // for normal packets
206                case ExtendedGridSimTags.JUNK_PKT: // for background traffic
207                        // System.out.println(super.get_name() + ".processEvent(): enque()
208                        // at time = " + GridSim.clock());
209                case ExtendedGridSimTags.RESERVATION_PACKET_FORWARD:
210                        enque(ev);
211                        break;
212
213                case ExtendedGridSimTags.INSIGNIFICANT:
214                        // System.out.println(super.get_name() + ".processEvent():
215                        // processInternalEvent() + at time = " + GridSim.clock());
216                        processInternalEvent();
217                        break;
218                       
219                case ExtendedGridSimTags.RESERVATION_INTERNAL:
220                        processInternalEventFlowsUsingReservation();
221                        break;
222                       
223                case ExtendedGridSimTags.UPDATE_BAUDRATE:
224                        updateBaudRate();
225                        break;
226                       
227                default:
228                        System.out.println(super.get_name() + ".body(): Warning - "
229                                        + "unable to handle request from ExtendedGridSimTags "
230                                        + "with constant number " + ev.get_tag());
231                        break;
232                }
233        }
234
235        private synchronized boolean sendInternalEvent(double time) {
236                // System.out.println(super.get_name() + " sendInternalEvent(): called
237                // at time = " + GridSim.clock());
238
239                if (time < 0.0) {
240                        // System.out.println(super.get_name() + " sendInternalEvent():
241                        // false at time = " + GridSim.clock());
242                        return false;
243                }
244
245                // System.out.println(super.get_name() + " sendInternalEvent():
246                // scheduled for = " + time + " from now");
247                super.sim_schedule(super.get_id(), time, ExtendedGridSimTags.INSIGNIFICANT);
248                return true;
249        }
250
251        private synchronized void processInternalEvent() {
252
253                // System.out.println(super.get_name() + "processInternalEvent(): " +
254                // GridSim.clock());
255                //System.out.println("LINK - PROCESS INTERNAL EVENT");
256                // this is a constraint that prevents an infinite loop
257                // Compare between 2 floating point numbers. This might be incorrect
258                // for some hardware platform.
259                if (lastUpdateTime_ == ExtendedGridSim.clock()) {
260                        return;
261                }
262
263                lastUpdateTime_ = ExtendedGridSim.clock();
264
265                if (q_.size() == 0) {
266                        return;
267                } else if (q_.size() == 1) {
268                        deque((Packet) q_.remove(0));
269                } else {
270                        deque((Packet) q_.remove(0));
271                        sendInternalEvent(super.delay_ / super.MILLI_SEC); // delay in ms
272
273                }
274
275                // System.out.println("Super.delay_ is " + super.delay_);
276                // System.out.println(super.get_name() + " processInternalEvent(): done
277                // at time = " + GridSim.clock());
278        }
279
280        private synchronized void enque(Sim_event ev) {
281                // System.out.println(super.get_name() + " enque() + at time = " +
282                // GridSim.clock());
283
284                int tag = ((Packet) ev.get_data()).getTag();
285                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()+" LINK: - ENQUE" + tag+";"+ev.get_tag()+";"+((Packet) ev.get_data()).getID());
286                // Register passing flow, gridlet or junk as active on this link
287
288                if (tag == ExtendedGridSimTags.FLOW_SUBMIT || tag == ExtendedGridSimTags.GRIDLET_SUBMIT
289                                || tag == ExtendedGridSimTags.GRIDLET_SUBMIT_ACK
290                                || tag == ExtendedGridSimTags.SEND_AR_COMMIT_WITH_GRIDLET
291                                // || tag == ExtendedGridSimTags.GRIDLET_RETURN
292                                || tag == ExtendedGridSimTags.JUNK_PKT) {
293                        registerFlow((Packet) ev.get_data());
294                }
295                else if (tag == ExtendedGridSimTags.FLOW_RESERVATION
296                                || tag == ExtendedGridSimTags.GRIDLET_RESERVATION
297                                || tag == ExtendedGridSimTags.GRIDLET_ACK_RESERVATION) {
298
299                        GSSIMFlowPacket fp = (GSSIMFlowPacket) ev.get_data();
300
301                        if (!checkIfReservationExist(fp))
302                                return;
303                        registerFlowUsingReservation((Packet) ev.get_data());
304                }
305
306                else if (tag == ExtendedGridSimTags.GRIDLET_RETURN) {
307                        GSSIMFlowPacket fp = (GSSIMFlowPacket) ev.get_data();
308                        Gridlet gridlet = (Gridlet) fp.getData();
309                        if (gridlet instanceof NetworkGridlet) {
310                                if (!checkIfReservationExist(fp))
311                                        return;
312                                registerFlowUsingReservation((Packet) ev.get_data());
313                        }
314                        else{
315                                registerFlow((Packet) ev.get_data());
316                        }
317                }
318
319                if (tag == ExtendedGridSimTags.FLOW_RESERVATION
320                                || tag == ExtendedGridSimTags.GRIDLET_RESERVATION
321                                || tag == ExtendedGridSimTags.GRIDLET_ACK_RESERVATION) {
322                        qFlowsUsingReservation.add(ev.get_data());
323
324                        if (qFlowsUsingReservation.size() == 1) {
325                                sendInternalEventFlowsUsingReservation(super.delay_
326                                                / super.MILLI_SEC);
327                        }
328                }
329
330                else if (tag == ExtendedGridSimTags.GRIDLET_RETURN) {
331                        GSSIMFlowPacket fp = (GSSIMFlowPacket) ev.get_data();
332
333                        Gridlet gridlet = (Gridlet) fp.getData();
334                        if (gridlet instanceof NetworkGridlet) {
335                                qFlowsUsingReservation.add(ev.get_data());
336
337                                if (qFlowsUsingReservation.size() == 1) {
338                                        sendInternalEventFlowsUsingReservation(super.delay_
339                                                        / super.MILLI_SEC);
340                                }
341                        } else {
342                                q_.add(ev.get_data());
343
344                                if (q_.size() == 1) {
345
346                                        sendInternalEvent(super.delay_ / super.MILLI_SEC);
347                                }
348                        }
349                }
350
351                else {
352                        q_.add(ev.get_data());
353                       
354                        if (q_.size() == 1) {
355                                // System.out.println(" MYFLOWLINK - SENDING INTERNAL EVENT");
356                                sendInternalEvent(super.delay_ / super.MILLI_SEC); // delay in
357                                // ms}
358                        }
359                }
360
361        }
362
363        private synchronized void deque(Packet np) {
364                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()+" LINK - DEQUE"+np.getTag()+";"+np.getID());
365                // System.out.println(super.get_name() + ".deque() for packet " +
366                // np.getID() +" here");
367                // System.out.println(super.get_name() + ".deque() packet " +
368                // np.toString());
369
370                int dest = getNextHop(np);
371                if (dest == -1) {
372                        // System.out.println(super.get_name() + ".deque() here3");
373                        return;
374                }
375
376                // other side is a Sim_entity
377                int tag = 0;
378                if (dest == outEnd2_ || dest == outEnd1_) {
379                        //System.out.println(super.get_name() + ".deque() here1");
380
381                        // for junk packets only
382                        if (np.getTag() == ExtendedGridSimTags.JUNK_PKT) {
383                                tag = ExtendedGridSimTags.JUNK_PKT;
384                        } else if (np.getTag() == ExtendedGridSimTags.FLOW_RESERVATION
385                                        || np.getTag() == ExtendedGridSimTags.GRIDLET_RESERVATION
386                                        || np.getTag() == ExtendedGridSimTags.GRIDLET_ACK_RESERVATION) {
387                                tag = ExtendedGridSimTags.RESERVATION_PACKET_FORWARD;
388                        } else if (np.getTag() == ExtendedGridSimTags.GRIDLET_RETURN) {
389                                GSSIMFlowPacket fp = (GSSIMFlowPacket) np;
390                                if (checkIfReservationExist(fp)) {
391                                        tag = ExtendedGridSimTags.RESERVATION_PACKET_FORWARD;
392                                } else
393                                        tag = ExtendedGridSimTags.PKT_FORWARD;
394                        }
395                        // for other packets
396                        else {
397                                tag = ExtendedGridSimTags.PKT_FORWARD;
398                        }
399                }
400                // other side is a GridSim entity
401                else {
402                        // System.out.println(super.get_name() + ".deque() here2");
403
404                        tag = np.getTag();
405                }
406                if (tag == ExtendedGridSimTags.RESERVATION_PACKET_FORWARD) {
407                        super.sim_schedule(dest, ExtendedGridSimTags.SCHEDULE_NOW, tag, np);
408                }
409                // sends the packet
410                else {
411                        super.sim_schedule(dest, ExtendedGridSimTags.SCHEDULE_NOW, tag, np);
412                }
413
414                // System.out.println(super.get_name() + ".deque() + at time = " +
415                // GridSim.clock());
416
417        }
418
419        private synchronized int getNextHop(Packet np) {
420                int dest = -1;
421                int src = np.getLast();
422               
423                // check if source is from outEnd1_
424                if (src == outEnd1_) {
425                        dest = inEnd2_;
426                }
427                // or source is from outEnd2_
428                else if (src == outEnd2_) {
429                        dest = inEnd1_;
430                }
431                return dest;
432        }
433
434        private synchronized void registerFlow(Packet np) {
435
436                GSSIMFlowPacket tempFlow = null;
437                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
438                //              + ":REGISTERING FLOW " + np.getID());
439                // Add flow to link
440                activeFlows_.put(np.getID(), np);
441
442                flowsSizeCharacteristic.add(np.getSize());
443
444                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
445                                + ExtendedGridSim.getEntityName(np.getDestID())),
446                                ExtendedGridSimTags.SCHEDULE_NOW, ExtendedGridSimTags.FLOW_ADD, np);
447                // System.out.println(super.get_name() + ".registerFlow(): registering
448                // flow #" + np.getID()
449                // + " total of " + activeFlows_.size() + " flows");
450
451                // Register link to flow
452                ((GSSIMFlowPacket) np).addLink(this);
453
454                // Check if this affects any existing flows
455                Iterator<Integer> flowsIter = activeFlows_.keySet().iterator();
456                while (flowsIter.hasNext()) {
457                        tempFlow = (GSSIMFlowPacket) activeFlows_.get(flowsIter.next());
458                        // If change in bandwidth affects an existing flow i.e. is < current
459                        // bottleneck
460                        // System.out.println(get_name()+" this ged baud rate "+this.
461                        // getBaudRate
462                        // ()+" templfolobadwidth "+tempFlow.getBandwidth()+"tempflowid "
463                        // +tempFlow.getID() +"npid "+np.getID());
464                        if (this.getBaudRate() < tempFlow.getBandwidth()
465                                        && tempFlow.getID() != np.getID()) {
466
467                                // Need to notify flow
468                                // System.out.println(super.get_name() + ".registerFlow(): flow
469                                // #" + np.getID()
470                                // + " bottleneck now " + this.getBaudRate() + " at time " +
471                                // GridSim.clock());
472                                // I can notify directly as I know the destId's!!!!
473                                // System.out.println(super.get_name() + ".registerFlow():
474                                // updating flow #" + tempFlow.getID()
475                                // + " destination " + tempFlow.getDestID());
476
477                                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
478                                                + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
479                                                ExtendedGridSimTags.SCHEDULE_NOW, ExtendedGridSimTags.FLOW_UPDATE,
480                                                new Integer(tempFlow.getID()));
481                        }
482                }
483        }
484
485        public synchronized void deregisterFlow(Packet np) {
486                GSSIMFlowPacket fp = null;
487                GSSIMFlowPacket tempFlow;
488                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
489                //              + ":DEREGISTEREING FLOW");
490                // If the flow hasn't been removed already, remove from active flow list
491                if ((fp = (GSSIMFlowPacket) activeFlows_.remove(np.getID())) != null) {
492
493                        // System.out.println(super.get_name() + ".deregisterFlow() success
494                        // flow # " + np.getID()
495                        // + " " + fp.getBandwidth());
496
497                        // Check if this affects any existing flows
498                        Iterator<Integer> flowsIter = activeFlows_.keySet().iterator();
499                        while (flowsIter.hasNext()) {
500                                tempFlow = (GSSIMFlowPacket) activeFlows_.get(flowsIter.next());
501                                // If change in bandwidth affects an existing flow i.e. is >
502                                // current bottleneck
503                                // AND this link is the particular flow's bottleneck
504                                if (this.getBaudRate() > tempFlow.getBandwidth()
505                                                && tempFlow.getID() != np.getID()
506                                                && tempFlow.getBottleneckID() == this.get_id()) {
507                                        // Need to notify flow
508                                        // System.out.println(super.get_name() + ".deregisterFlow():
509                                        // flow #" + np.getID()
510                                        // + " bottleneck now " + this.getBaudRate() + " at time " +
511                                        // GridSim.clock());
512                                        // I can notify directly as I know the destId's!!!!
513                                        // System.out.println(super.get_name() + ".deregisterFlow():
514                                        // updating flow #" + tempFlow.getID()
515                                        // + " destination " + tempFlow.getDestID());
516                                        super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
517                                                        + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
518                                                        ExtendedGridSimTags.SCHEDULE_NOW, ExtendedGridSimTags.FLOW_UPDATE,
519                                                        new Integer(tempFlow.getID()));
520                                }
521
522                        }
523                }
524        }
525
526        public synchronized double getBaudRate() {
527
528                if(super.baudRate_ == 0)
529                        return 1;
530                if (activeFlows_.size() != 0) {
531                        // System.out.println(super.get_name() + ".getBaudRate() Getting
532                        // latest baud! " + (super.baudRate_)/(activeFlows_.size()));
533                        return (super.baudRate_) / (activeFlows_.size());
534                } else {
535                        // System.out.println(super.get_name() + ".getBaudRate() Getting
536                        // latest baud! " + (super.baudRate_));
537                        return super.baudRate_;
538                }
539
540        }
541
542        /*public synchronized boolean makeReservation(NetworkReservationObject netResObj) {
543
544                System.out.println(get_name() + " making reservation "
545                                + netResObj.getReservationID());
546                if (netResObj.getStartTime() < ExtendedGridSim.getCurrentTime()) {
547                        System.out.println(get_name() + " to late for reservation "
548                                        + netResObj.getReservationID() + ";" + netResObj.getStartTime()
549                                        + ";" + ExtendedGridSim.getCurrentTime());
550                        // printReservationInfo();
551                        return false;
552                } else {
553                        if (checkAvailableBandwidth2(netResObj)) {
554                                System.out.println(get_name()
555                                                + " ADD RESERAVTION TO LIST SUCCES");
556                                reservationList.add(netResObj);
557                                sendBaduRateUpdateEvent(((double)netResObj.getStartTime() - (double)ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC, ((double)netResObj.getStartTime() + (double)netResObj.getDuration() - ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC);
558
559                                createNEWCalendar();
560
561                        } else {
562                                System.out.println(get_name()
563                                                + "ADD RESERAVTION TO LIST FAILED");
564                                // printReservationInfo();
565                                return false;
566                        }
567                        // printReservationInfo();
568                        return true;
569                }
570        }*/
571
572        /*public synchronized boolean deleteReservation(NetworkReservationObject netResObj) {
573                removeReservation(netResObj);
574                checkReservations();
575                return true;
576        }*/
577
578        public synchronized boolean removeReservation(NetworkReservationObject netResObj) {
579                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
580                //              + " Removing reservation " + netResObj.getReservationID());
581                //if (netResObj.getStartTime() < getCurrentTime()) {
582                        //if (listofReservations == null)
583                        //      return false;
584                        if (reservationList.contains(netResObj)) {
585                                cancelFlowsUsingCanceledReservation(netResObj.getReservationID());
586                                reservationList.remove(netResObj);
587                                createNEWCalendar();
588                                // printReservationInfo();
589
590                                return true;
591                        }
592                        else return false;
593                //}
594
595                //reservationList.remove(netResObj);
596                //createNEWCalendar();
597                // printReservationInfo();
598
599                //return true;
600        }
601
602        /*public synchronized boolean changeReservation(NetworkReservationObject netResObj) {
603                NetworkReservationObject oldNetResObj = getNetResObj(netResObj.getReservationID());
604                if (oldNetResObj == null)
605                        return false;
606                printReservationInfo();
607                if (oldNetResObj.getStartTime() > ExtendedGridSim.getCurrentTime()
608                                && netResObj.getStartTime() > ExtendedGridSim.getCurrentTime()) {
609                        //removeReservation(netObjTemp);
610                        reservationList.remove(oldNetResObj);
611                        createNEWCalendar();
612                        if (checkAvailableBandwidth(netResObj)) {
613                                oldNetResObj.setStartTime(netResObj.getStartTime());
614                                oldNetResObj.setEndTime(netResObj.getEndTime());
615                                oldNetResObj.setDuration(netResObj.getDuration());
616                                oldNetResObj.setBandwidth(netResObj.getBandwidth());
617                                reservationList.add(oldNetResObj);
618                                sendBaduRateUpdateEvent(((double)netResObj.getStartTime() - (double)ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC, ((double)netResObj.getStartTime() + (double)netResObj.getDuration() - ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC);
619                                //printReservationInfo();
620                                //createNEWCalendar();
621                                //checkReservations();
622                                return true;
623                        } else {
624                                reservationList.add(oldNetResObj);
625                                //printReservationInfo();
626                                //createNEWCalendar();
627                                //checkReservations();
628                                return false;
629                        }
630                }
631                // printReservationInfo();
632                return false;
633        }*/
634
635        private synchronized void registerFlowUsingReservation(Packet np) {
636
637                GSSIMFlowPacket tempFlow = null;
638                GSSIMFlowPacket fp = (GSSIMFlowPacket) np;
639
640                // activeFlowsUsingReservations.put(np.getID(), np);
641                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
642                                + ExtendedGridSim.getEntityName(np.getDestID())),
643                                ExtendedGridSimTags.SCHEDULE_NOW,
644                                ExtendedGridSimTags.FLOW_USING_RESERVATION_ADD, np);
645                int reservationID;
646                if (fp.getTag() == ExtendedGridSimTags.GRIDLET_RESERVATION
647                                || fp.getTag() == ExtendedGridSimTags.GRIDLET_ACK_RESERVATION
648                                || fp.getTag() == ExtendedGridSimTags.GRIDLET_RETURN) {
649                        NetworkGridlet data = (NetworkGridlet) fp.getData();
650                        reservationID = data.getReservationID();
651                } /*else if (fp.getTag() == ExtendedGridSimTags.GRIDLET_RETURN) {
652                        ReservationGridlet data = (ReservationGridlet) fp.getData();
653                        reservationID = data.getReservationID();
654                } */else {
655                        NetworkData data = (NetworkData) fp.getData();
656                        reservationID = data.getReservationID();
657                }
658               
659                // ReservationData data = (ReservationData) fp.getData();
660                //System.out.println(ExtendedGridSim.clock() + " LINK: " + get_name()
661                //              + " REGISTEREING FLOW: " + np.getID() + " USING RESERVAION "
662                //              + reservationID+";"+ExtendedGridSim.getCurrentTime());
663                HashMap activeFlowsUsingReservations = (HashMap) reservationID_activeFlowsUsingReservation
664                                .get(reservationID);
665
666                if (activeFlowsUsingReservations == null) {
667                        activeFlowsUsingReservations = new HashMap();
668                        activeFlowsUsingReservations.put(np.getID(), np);
669                        reservationID_activeFlowsUsingReservation.put(reservationID,
670                                        activeFlowsUsingReservations);
671                } else {
672                        activeFlowsUsingReservations.put(np.getID(), np);
673                        reservationID_activeFlowsUsingReservation.put(reservationID,
674                                        activeFlowsUsingReservations);
675                }
676               
677                ArrayList furc = (ArrayList)flowsUsingReservationsSizeCharacteristic.get(reservationID);
678                if(furc == null)
679                        furc = new ArrayList();
680                furc.add(np.getSize());
681                flowsUsingReservationsSizeCharacteristic.put(reservationID, furc);
682               
683                ((GSSIMFlowPacket) np).addReservedLink(this, reservationID);
684                Iterator flowsIter2 = ((HashMap) reservationID_activeFlowsUsingReservation
685                                .get(reservationID)).keySet().iterator();
686                while (flowsIter2.hasNext()) {
687                        tempFlow = (GSSIMFlowPacket) ((HashMap) reservationID_activeFlowsUsingReservation
688                                        .get(reservationID)).get(flowsIter2.next());
689                        // System.out.println(
690                        // "MYFLOWLINK- REGISTER RESEVATION - INSIDE LOOP; FLOW ID: "
691                        // +tempFlow.getID());
692                        if (this.getReservedLinkBaudRate(reservationID) < tempFlow
693                                        .getBandwidth()
694                                        && tempFlow.getID() != np.getID()) {
695                                //System.out.println("updating:"
696                                //              + this.getReservedLinkBaudRate(reservationID) + ";"
697                                //              + tempFlow.getBandwidth() + ";" + reservationID);
698                                //System.out.println("temp flow id : " + tempFlow.getID()
699                                //              + " np id: " + np.getID());
700
701                                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
702                                                + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
703                                                ExtendedGridSimTags.SCHEDULE_NOW,
704                                                ExtendedGridSimTags.RESERVATION_UPDATE, new Integer(tempFlow
705                                                                .getID()));
706                        }
707                }
708
709        }
710
711        public synchronized void deregisterFlowUsingReservation(Packet np) {
712                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()+ " LINK - DEREGISTEREING FLOW USING RESERVATION " + np.getID());
713                GSSIMFlowPacket tempFlow;
714
715                GSSIMFlowPacket fp = (GSSIMFlowPacket) np;
716                int reservationID;
717                if (fp.getTag() == ExtendedGridSimTags.GRIDLET_RESERVATION
718                                || fp.getTag() == ExtendedGridSimTags.GRIDLET_ACK_RESERVATION) {
719                        NetworkGridlet data = (NetworkGridlet) fp.getData();
720                        reservationID = data.getReservationID();
721                } else if (fp.getTag() == ExtendedGridSimTags.GRIDLET_RETURN) {
722                        NetworkGridlet data = (NetworkGridlet) fp.getData();
723                        reservationID = data.getReservationID();
724                } else {
725                        NetworkData data = (NetworkData) fp.getData();
726                        reservationID = data.getReservationID();
727                }
728
729                //System.out.println(ExtendedGridSim.clock() + " LINK" + get_name()
730                //              + " DEREGISTEREING FLOW USING RESERVATION " + reservationID);
731                // tempMap.remove(np.getID());
732
733                if ((fp = (GSSIMFlowPacket) ((HashMap) reservationID_activeFlowsUsingReservation
734                                .get(reservationID)).remove(np.getID())) != null) {
735
736                        Iterator<Integer> flowsIter2 = ((HashMap) reservationID_activeFlowsUsingReservation
737                                        .get(reservationID)).keySet().iterator();
738                        while (flowsIter2.hasNext()) {
739                                tempFlow = (GSSIMFlowPacket) ((HashMap) reservationID_activeFlowsUsingReservation
740                                                .get(reservationID)).get(flowsIter2.next());
741                                if (this.getReservationBandwidth(reservationID) > tempFlow
742                                                .getBandwidth()
743                                                && tempFlow.getID() != np.getID()
744                                                && tempFlow.getBottleneckID() == this.get_id()) {
745                                        super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
746                                                        + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
747                                                        ExtendedGridSimTags.SCHEDULE_NOW,
748                                                        ExtendedGridSimTags.RESERVATION_UPDATE, new Integer(
749                                                                        tempFlow.getID()));
750                                }
751
752                        }
753                }
754
755        }
756
757        public synchronized void printReservationInfo() {
758                if (reservationList == null) {
759                        return;
760                }
761                Iterator it = reservationList.iterator();
762                //double accumulatedReseravationsBanwidth = 0;
763                while (it.hasNext()) {
764                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
765                        System.out.println(get_name()+"******************************"+netResObj);
766                        //NetResObj netResObj = (NetResObj) it.next();
767                        System.out.println("userID " + netResObj.getUserID());
768                        System.out.println("startTime " + netResObj.getStartTime());
769                        System.out.println("endTime " + netResObj.getEndTime());
770                        System.out.println("duration " + netResObj.getDuration());
771                        System.out.println("size " + netResObj.getBandwidth());
772                        //accumulatedReseravationsBanwidth = accumulatedReseravationsBanwidth
773                        //              + netResObj.getBandwidth();
774
775                }
776                it = reservationList.iterator();
777                int nrOfAciveReservations = 0;
778                double activeReseravationsBandwidth = 0;
779                while (it.hasNext()) {
780                        //System.out.println("******************************");
781                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
782                        if (netResObj.getStartTime() <= ExtendedGridSim.getCurrentTime()
783                                        && netResObj.getEndTime() > ExtendedGridSim.getCurrentTime()) {
784                                nrOfAciveReservations++;
785                                activeReseravationsBandwidth = activeReseravationsBandwidth
786                                                + netResObj.getBandwidth();
787                        }
788
789                }
790                System.out.println("*********SUMARY*************");
791                System.out.println(ExtendedGridSim.clock() + " " + get_name()
792                                + " nrOfReservation " + reservationList.size());
793                        //      + " sizeOfAllReseravation " + accumulatedReseravationsBanwidth);
794                System.out.println("nrOfAciveReservation " + nrOfAciveReservations
795                                + " sizeOfActiveReseravation " + activeReseravationsBandwidth);
796                System.out.println("*********SUMARY*************");
797        }
798
799        public synchronized void handleFinishedReservations() {
800
801                if (reservationList == null) {
802                        return;
803                }
804                Iterator it = reservationList.iterator();
805                List<NetworkReservationObject> finishedReservations = new ArrayList<NetworkReservationObject>();
806                while (it.hasNext()) {
807                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
808                        if (netResObj.getEndTime() <= ExtendedGridSim.getCurrentTime()) {
809                                finishedReservations.add(netResObj);
810                                // System.out.println("list size"+listofReservations.size());
811                                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
812                                //              + " check finished reservations "
813                                //              + netResObj.getStartTime() + ";" + netResObj.getDuration()
814                                //              + ";" + "user id: " + netResObj.getUserID());
815                        }
816                }
817               
818                it = finishedReservations.iterator();
819                while (it.hasNext()) {
820                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
821                        cancelFlowsUsingFinishedReservation(netResObj.getReservationID());
822                        netResObj.setStatus(ExtendedGridSimTags.RESERVATION_EXPIRED);
823                        completedReservationsList.add(netResObj);
824                        removeReservation(netResObj);
825                        //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
826                        //              + " REMOVED EXPIRED RESERVATION:"
827                        //              + netResObj.getReservationID());
828                }
829        }
830
831        /*public synchronized void checkReservations() {
832                handleFinishedReservations();
833                activeReservationsBandwidth = getActiveReservationsBandwidth();
834                setBaduRate(startBaudRate - activeReservationsBandwidth);
835                if (super.getBaudRate() > lastBaudRate) {
836                        System.out.println(System.currentTimeMillis()+":"+ExtendedGridSim.getCurrentTime());
837                        System.out.println("active flows" + activeFlows_.size());
838                        System.out.println(ExtendedGridSim.clock() + ";" + get_name() + ";"
839                                        + super.getBaudRate() + ";" + lastBaudRate);
840                        increaseFlow();
841                } else if (super.getBaudRate() < lastBaudRate) {
842                        System.out.println(System.currentTimeMillis()+":"+ExtendedGridSim.getCurrentTime());
843                        System.out.println(ExtendedGridSim.clock() + ";" + get_name() + ";"
844                                        + super.getBaudRate() + ";" + lastBaudRate);
845                        decreaseFlow();
846                }
847                lastBaudRate = super.getBaudRate();
848        }*/
849
850        public synchronized double getActiveReservationsBandwidth() {
851                double reservationsBandwidth = 0;
852                if (reservationList == null) {
853                        return 0;
854                }
855                Iterator it = reservationList.iterator();
856                while (it.hasNext()) {
857                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
858                        if (netResObj.getStatus() == ExtendedGridSimTags.RESERVATION_CREATED && netResObj.getStartTime() <= ExtendedGridSim.getCurrentTime()
859                                        && netResObj.getEndTime() >= ExtendedGridSim.getCurrentTime()) {
860                                reservationsBandwidth  = reservationsBandwidth  + netResObj.getBandwidth();
861
862                        }
863                }
864
865                return reservationsBandwidth ;
866        }
867
868        //OK ale malo wydajne
869        /*public synchronized boolean checkAvailableBandwidth2(
870                        NetworkReservationObject netResObj) {
871                boolean answer = true;
872                double reservationsBandwidth = 0;
873                LinkedHashSet<Long> reservationTimestampsSet = createReservationsTimestampsSet();
874                TreeMap<Long, Double> reservationCalendar = createReservationsCalendar(reservationTimestampsSet);
875                reservationsBandwidth = getMaxCalendarValue(reservationCalendar,netResObj.getStartTime(), netResObj.getEndTime());
876                System.out.println(get_name() + " general bandwidth "
877                                + getStartBaduRate() + "; yet reservation "
878                                + reservationsBandwidth + "; reservation object "
879                                + netResObj.getReservationID() + " " + netResObj.getBandwidth());
880                if (getStartBaduRate() - reservationsBandwidth - BUFFER_BAUDRATE < netResObj.getBandwidth())
881                        answer = false;
882                return answer;
883        }*/
884
885         /************CHANGED************/
886        /*public synchronized boolean checkAvailableBandwidth(
887                        NetworkReservationObject netResObj) {
888                boolean answer = true;
889                double freeBandwidth = 0;
890                Iterator<AvailableBandwidth> it = bandwidthCalendar.iterator();
891                double minCalenderValue = getStartBaduRate();
892                while (it.hasNext()) {
893                        AvailableBandwidth availableBandwidth = (AvailableBandwidth) it.next();
894                        if (availableBandwidth.getTimestamp() >= netResObj.getStartTime()) {
895                                if(availableBandwidth.getValue() < minCalenderValue)
896                                        minCalenderValue = availableBandwidth.getValue();
897                                if(availableBandwidth.getTimestamp() >= netResObj.getEndTime())
898                                        break;
899                        }
900
901                }
902                freeBandwidth = minCalenderValue;
903
904                System.out.println(get_name() + " general bandwidth "
905                                + getStartBaduRate() + "; yet reservation "
906                                + (getStartBaduRate() - freeBandwidth) + "; reservation object "
907                                + netResObj.getReservationID() + " " + netResObj.getBandwidth());
908                if(bandwidthCalendar.size() == 0 && getStartBaduRate() > netResObj.getBandwidth())
909                        return answer;
910                if (freeBandwidth - BUFFER_BAUDRATE < netResObj.getBandwidth())
911                        answer = false;
912                return answer;
913        }
914        */
915        public synchronized double getReservedLinkBaudRate(int reservationID) {
916
917                /*if((HashMap) (reservationsID_activeFlowsUsingReservations
918                                .get(reservationID))==null)
919                                return 0;*/
920                int size = ((HashMap) (reservationID_activeFlowsUsingReservation
921                                .get(reservationID))).size();
922                //System.out.println("get reservation baudrate; hashmap size " + size
923                //              + " reservation id " + reservationID + " badnwidth "
924                //              + getReservationBandwidth(reservationID));
925                if (size != 0) {
926
927                        // System.out.println(super.get_name() + ".getBaudRate() Getting
928                        // latest baud! " + (super.baudRate_)/(activeFlows_.size()));
929                        return getReservationBandwidth(reservationID) / size;
930                } else {
931                        // System.out.println(super.get_name() + ".getBaudRate() Getting
932                        // latest baud! " + (super.baudRate_));
933                        return getReservationBandwidth(reservationID);
934                }
935
936        }
937
938        public double getReservationBandwidth(int reservationID) {
939                // System.out.println("get");
940                /*if (listofReservations == null) {
941                        return 0;
942                }*/
943                Iterator it = reservationList.iterator();
944                while (it.hasNext()) {
945                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
946                       
947                        if (netResObj.getReservationID() == reservationID) {
948                                return netResObj.getBandwidth();
949                        }
950                }
951                return 0;
952
953        }
954       
955        public void setBaduRate(double baudRate) {
956                this.baudRate_ = baudRate;
957        }
958
959        public double getStartBaduRate() {
960                return this.startBaudRate;
961        }
962
963        private synchronized void processInternalEventFlowsUsingReservation() {
964
965                if (lastUpdateTimeReservation == ExtendedGridSim.clock()) {
966                        return;
967                }
968
969                lastUpdateTimeReservation = ExtendedGridSim.clock();
970
971                if (qFlowsUsingReservation.size() == 0) {
972                        return;
973                } else if (qFlowsUsingReservation.size() == 1) {
974                        deque((Packet) qFlowsUsingReservation.remove(0));
975                } else {
976                        deque((Packet) qFlowsUsingReservation.remove(0));
977                        sendInternalEventFlowsUsingReservation(super.delay_
978                                        / super.MILLI_SEC);
979
980                }
981
982        }
983
984        private synchronized boolean sendInternalEventFlowsUsingReservation(
985                        double time) {
986
987                if (time < 0.0) {
988                        return false;
989                }
990
991                super.sim_schedule(super.get_id(), time,
992                                ExtendedGridSimTags.RESERVATION_INTERNAL);
993                return true;
994        }
995
996        public synchronized void decreaseFlow() {
997
998                GSSIMFlowPacket tempFlow = null;
999
1000                //System.out.println(get_name() + " DECREASE FLOW");
1001                // printReservationInfo();
1002                Iterator<Integer> flowsIter = activeFlows_.keySet().iterator();
1003                while (flowsIter.hasNext()) {
1004                        tempFlow = (GSSIMFlowPacket) activeFlows_.get(flowsIter.next());
1005                        //System.out.println("before decreasing "
1006                        //              + tempFlow.getBottleneckID() + ";" + tempFlow.getID() + ";"
1007                        //              + this.getBaudRate() + ";" + tempFlow.getBandwidth() + ";"
1008                        //              + this.get_id());
1009                        if (this.getBaudRate() < tempFlow.getBandwidth()
1010                                        && tempFlow.getBottleneckID() == this.get_id()) {
1011                                //System.out.println("decreasing " + tempFlow.getBottleneckID()
1012                                //              + ";" + tempFlow.getID());
1013                                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
1014                                                + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
1015                                                ExtendedGridSimTags.SCHEDULE_NOW, ExtendedGridSimTags.FLOW_UPDATE,
1016                                                new Integer(tempFlow.getID()));
1017                        }
1018                }
1019        }
1020
1021        public synchronized void increaseFlow() {
1022                GSSIMFlowPacket tempFlow = null;
1023
1024                //System.out.println("INCREASE FLOW");
1025
1026                Iterator<Integer> flowsIter = activeFlows_.keySet().iterator();
1027                while (flowsIter.hasNext()) {
1028                        tempFlow = (GSSIMFlowPacket) activeFlows_.get(flowsIter.next());
1029
1030                        //System.out.println("IN UPDATE, baudrate :" + this.getBaudRate()
1031                        //              + " flow " + tempFlow.getBandwidth() + "id "
1032                        //              + tempFlow.getID());
1033                        if (this.getBaudRate() > tempFlow.getBandwidth()
1034                                        && tempFlow.getBottleneckID() == this.get_id()) {
1035
1036                                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
1037                                                + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
1038                                                ExtendedGridSimTags.SCHEDULE_NOW, ExtendedGridSimTags.FLOW_UPDATE,
1039                                                new Integer(tempFlow.getID()));
1040                        }
1041
1042                }
1043        }
1044
1045        public synchronized void cancelFlowsUsingFinishedReservation(
1046                        int reservationID) {
1047
1048                GSSIMFlowPacket tempFlow = null;
1049                if (reservationID_activeFlowsUsingReservation
1050                                .containsKey(reservationID)) {
1051                        Iterator<Integer> flowsIter = ((HashMap) reservationID_activeFlowsUsingReservation
1052                                        .get(reservationID)).keySet().iterator();
1053                        while (flowsIter.hasNext()) {
1054                                tempFlow = (GSSIMFlowPacket) ((HashMap) reservationID_activeFlowsUsingReservation
1055                                                .get(reservationID)).get(flowsIter.next());
1056
1057                                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
1058                                //              + " cancel flows using reservation " + reservationID
1059                                //              + " flow id " + tempFlow.getID());
1060                                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
1061                                                + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
1062                                                ExtendedGridSimTags.SCHEDULE_NOW,
1063                                                ExtendedGridSimTags.RESERVATION_EXPIRED, new Integer(tempFlow
1064                                                                .getID()));
1065
1066                        }
1067                }
1068
1069                reservationID_activeFlowsUsingReservation.remove(reservationID);
1070
1071        }
1072
1073        public synchronized void cancelFlowsUsingCanceledReservation(
1074                        int reservationID) {
1075
1076                GSSIMFlowPacket tempFlow = null;
1077                if (reservationID_activeFlowsUsingReservation
1078                                .containsKey(reservationID)) {
1079                        Iterator<Integer> flowsIter = ((HashMap) reservationID_activeFlowsUsingReservation
1080                                        .get(reservationID)).keySet().iterator();
1081                        while (flowsIter.hasNext()) {
1082                                tempFlow = (GSSIMFlowPacket) ((HashMap) reservationID_activeFlowsUsingReservation
1083                                                .get(reservationID)).get(flowsIter.next());
1084
1085                                //System.out.println(ExtendedGridSim.clock() + ";" + get_name()
1086                                //              + " cancel flows using reservation " + reservationID
1087                                //              + " flow id " + tempFlow.getID());
1088                                super.sim_schedule(ExtendedGridSim.getEntityId("Input_"
1089                                                + ExtendedGridSim.getEntityName(tempFlow.getDestID())),
1090                                                ExtendedGridSimTags.SCHEDULE_NOW,
1091                                                ExtendedGridSimTags.RESERVATION_CANCELED, new Integer(tempFlow
1092                                                                .getID()));
1093
1094                        }
1095                }
1096
1097        }
1098
1099        /*
1100         * public synchronized void cancelFlowsUsingReservation(int reservationID) {
1101         *
1102         * MyFlowPacket tempFlow = null; if
1103         * (reservationsID_activeFlowsUsingReservations .containsKey(reservationID)) {
1104         * Iterator<Integer> flowsIter2 = ((HashMap)
1105         * reservationsID_activeFlowsUsingReservations
1106         * .get(reservationID)).keySet().iterator(); while (flowsIter2.hasNext()) {
1107         * tempFlow = (MyFlowPacket) ((HashMap)
1108         * reservationsID_activeFlowsUsingReservations
1109         * .get(reservationID)).get(flowsIter2.next());
1110         *
1111         * m.out .println(GridSim.clock() + ";" + get_name() + " cancel flows
1112         * using reservation "+reservationID+" flow id " + tempFlow.getID());
1113         * super.sim_schedule(GridSim.getEntityId("Input_" +
1114         * GridSim.getEntityName(tempFlow.getDestID())), ExtendedGridSimTags.SCHEDULE_NOW,
1115         * ExtendedGridSimTags.RESERVATION_CANCELED, new Integer(tempFlow .getID())); } } }
1116         */
1117       
1118        public NetworkReservationObject getNetResObj(int reservationID) {
1119                if (reservationList == null)
1120                        return null;
1121                Iterator it = reservationList.iterator();
1122                NetworkReservationObject netResObj = null;
1123                while (it.hasNext()) {
1124
1125                        netResObj = (NetworkReservationObject) it.next();
1126                        if (netResObj.getReservationID() == reservationID) {
1127                                return netResObj;
1128                        }
1129                }
1130
1131                return null;
1132
1133        }
1134
1135        public boolean checkIfReservationExist(GSSIMFlowPacket fp) {
1136               
1137                /*******CHANGED*******/
1138                //teoretycznie zbedne
1139                //handleFinishedReservations();
1140                int reservationID;
1141                if (fp.getTag() == ExtendedGridSimTags.GRIDLET_RESERVATION
1142                                || fp.getTag() == ExtendedGridSimTags.GRIDLET_ACK_RESERVATION) {
1143                        NetworkGridlet data = (NetworkGridlet) fp.getData();
1144                        reservationID = data.getReservationID();
1145                } else if (fp.getTag() == ExtendedGridSimTags.GRIDLET_RETURN) {
1146                       
1147                        /**********CHANGED************/
1148                        if (fp.getData() instanceof NetworkGridlet) {
1149                               
1150                                NetworkGridlet data = (NetworkGridlet) fp.getData();
1151                                reservationID = data.getReservationID();
1152                        } else{
1153                                return true;
1154                        }
1155                       
1156
1157                } else {
1158                        NetworkData data = (NetworkData) fp.getData();
1159                        reservationID = data.getReservationID();
1160                }
1161               
1162                /*******CHANGED*******/
1163                //teoretycznie zbedne
1164                //if (reservationList == null)
1165                //      return false;
1166               
1167                Iterator it = reservationList.iterator();
1168                //System.out.println(get_name()+"check if reservation: " + reservationID + " exist");
1169               
1170                NetworkReservationObject netResObj = getNetResObj(reservationID);
1171                if(netResObj != null && netResObj.getStartTime() <= ExtendedGridSim.getCurrentTime()){
1172                        //System.out.println("TRUE");
1173                        //System.out.println(netResObj.getStartTime() + ":"
1174                        //              + ExtendedGridSim.getCurrentTime());
1175                        return true;
1176                }
1177                else{
1178                        //System.out.println("FALSE");
1179                        return false;
1180                }
1181                /*while (it.hasNext()) {
1182
1183                        netResObj = (NetworkReservationObject) it.next();
1184                        if (netResObj.getReservationID() == reservationID) {
1185                                System.out.println(netResObj.getStartTime() + ":"
1186                                                + ExtendedGridSim.getCurrentTime());
1187                                if (netResObj.getStartTime() <= ExtendedGridSim.getCurrentTime()) {// zmieniono(<
1188                                        // na <=)
1189                                        System.out.println("TRUE");
1190                                        return true;
1191                                }
1192                        }
1193                }
1194                System.out.println("FALSE");
1195                return false;*/
1196
1197        }
1198
1199        /*
1200         * public boolean checkIfGridletIsUsingReservation(MyFlowPacket fp) {
1201         * Gridlet data = (Gridlet) fp.getData(); HashMap
1202         * tempMapActiveFlowsUsingReservations = (HashMap)
1203         * reservationsID_activeFlowsUsingReservations
1204         * .get(data.getReservationID()); if (tempMapActiveFlowsUsingReservations !=
1205         * null) { return true; } return false; }
1206         */
1207
1208        public void setPoints(Sim_entity point1, Sim_entity point2) {
1209                this.point1 = point1;
1210                this.point2 = point2;
1211        }
1212
1213        public void printPoints() {
1214                System.out.println("point1:" + this.point1.get_name()+ "; point2: "
1215                                + this.point2.get_name());
1216        }
1217
1218        public void setPoint1(Sim_entity point1) {
1219                this.point1 = point1;
1220        }
1221
1222        public void setPoint2(Sim_entity point2) {
1223                this.point2 = point2;
1224        }
1225
1226        public Sim_entity getPoint1() {
1227                return this.point1;
1228        }
1229
1230        public Sim_entity getPoint2() {
1231                return this.point2;
1232        }
1233
1234        public void setPointsNames(String point1Name, String point2Name) {
1235                this.point1Name = point1Name;
1236                this.point2Name = point2Name;
1237        }
1238       
1239        public void printPointsNames() {
1240                System.out.println("point1Name:" + this.point1Name + "; point2Name: "
1241                                + this.point2Name);
1242        }
1243
1244        public void setPoint1(String point1Name) {
1245                this.point1Name = point1Name;
1246        }
1247
1248        public void setPoint2(String point2Name) {
1249                this.point2Name = point2Name;
1250        }
1251
1252        public String getPoint1Name() {
1253                return this.point1Name;
1254        }
1255
1256        public String getPoint2Name() {
1257                return this.point2Name;
1258        }
1259       
1260        public void setBandwidthCalendar(List<AvailableBandwidth> calendar) {
1261                this.bandwidthCalendar = calendar;
1262        }
1263
1264        public List<AvailableBandwidth> getBandwidthCalendar() {
1265                return bandwidthCalendar;
1266        }
1267
1268        /*
1269         * public void addCalendar(AvailableBandwidth calendar) {
1270         * this.calendar.add(calendar); }
1271         *
1272         * public void addCalendar(int index, AvailableBandwidth calendar) {
1273         * this.calendar.add(index, calendar); }
1274         */
1275
1276        /*public List<AvailableBandwidth> createTempCalendar() {
1277                List<AvailableBandwidth> tempCalendar = new ArrayList<AvailableBandwidth>();
1278                Iterator it = calendar.iterator();
1279                AvailableBandwidth cal = null;
1280                AvailableBandwidth tempCal = null;
1281                while (it.hasNext()) {
1282
1283                        cal = (AvailableBandwidth) it.next();
1284                        tempCal = new AvailableBandwidth(cal.getTimestamp(), cal.getValue());
1285                        tempCalendar.add(tempCal);
1286                }
1287                return tempCalendar;
1288        }
1289
1290        public List<AvailableBandwidth> getCalendarFromConcreteTime(long time) {
1291                List<AvailableBandwidth> tempCalendar = createTempCalendar();
1292                Iterator it = tempCalendar.iterator();
1293                AvailableBandwidth tempCal = null;
1294                while (it.hasNext()) {
1295
1296                        tempCal = (AvailableBandwidth) it.next();
1297                        tempCal.setTimestamp(tempCal.getTimestamp() - time);
1298
1299                }
1300                return tempCalendar;
1301        }
1302
1303        public List<AvailableBandwidth> getCalendarFromCurrentTime() {
1304                List<AvailableBandwidth> tempCalendar = createTempCalendar();
1305                Iterator it = tempCalendar.iterator();
1306                AvailableBandwidth tempCal = null;
1307                //long time = (long) GridSim.clock();
1308                long time = getCurrentTime();
1309                while (it.hasNext()) {
1310
1311                        tempCal = (AvailableBandwidth) it.next();
1312                        tempCal.setTimestamp(tempCal.getTimestamp() - time);
1313
1314                }
1315                return tempCalendar;
1316        }*/
1317
1318        /*public void organizeCalendar() {
1319                int calendarSize = calendar.size();
1320                List indexesToRemove = new ArrayList();
1321                for (int i = 1; i < calendarSize; i++) {
1322                        if (calendar.get(i).getValue() == calendar.get(i - 1).getValue()) {
1323                                indexesToRemove.add(i - 1);
1324                        }
1325                }
1326                for (int i = calendarSize - 1; i >= 0; i--) {
1327                        if (indexesToRemove.contains(i)) {
1328                                calendar.remove(i);
1329                        }
1330                }
1331        }*/
1332       
1333        /*public void reverseCalendar() {
1334                int calendarSize = calendar.size();
1335                for (int i = 0; i < calendarSize; i++) {
1336                        AvailableBandwidth ab = calendar.get(i);
1337                        ab.setValue((long)getStartBaduRate()-ab.getValue()-(long)BUFFER_BANDWIDTH);
1338                }
1339        }*/
1340
1341
1342        /*public long getCurrentTime() {
1343                Calendar calendar = ExtendedGridSim.getSimulationCalendar();
1344                long simStartTime = calendar.getTimeInMillis();
1345
1346                return simStartTime + (int) (ExtendedGridSim.clock() * super.MILLI_SEC);
1347        }*/
1348       
1349       
1350       
1351        /*public double getCalendarValue(long timestamp){
1352                Iterator it = bandwidthCalendar.iterator();
1353                AvailableBandwidth ab = null;
1354                while (it.hasNext()) {
1355
1356                        if(timestamp < ab.getTimestamp()){
1357                                return (double) ab.getValue();
1358                        }
1359                        ab = (AvailableBandwidth) it.next();
1360
1361
1362                }
1363                return getStartBaduRate();
1364               
1365        }
1366       
1367        /*public double getMinFreeBandwidth(long time1, long time2){
1368               
1369                double minFreeBandwidth = getStartBaduRate();
1370                double tempMinFreeBandwidth = getStartBaduRate();
1371       
1372                Iterator it = bandwidthCalendar.iterator();
1373                AvailableBandwidth ab = null;
1374                while (it.hasNext()) {
1375
1376                        ab = (AvailableBandwidth) it.next();
1377                        if(time1 < ab.getTimestamp()){
1378                                tempMinFreeBandwidth = (double)ab.getValue();
1379                                if(tempMinFreeBandwidth < minFreeBandwidth)
1380                                        minFreeBandwidth = tempMinFreeBandwidth;
1381                        }
1382
1383                }
1384                tempMinFreeBandwidth = getCalendarValue(time2);
1385                if(tempMinFreeBandwidth < minFreeBandwidth)
1386                        minFreeBandwidth = tempMinFreeBandwidth;
1387                return minFreeBandwidth;
1388               
1389        }*/
1390       
1391        public void createNEWCalendar() {
1392                LinkedHashSet<Long> reservationsTimestampsSet = createReservationsTimestampsSet();
1393                //System.out.println("TIMESTAMPSET "+reservationTimestampset);
1394                TreeMap<Long, Double> reservationsCalendar = createReservationsCalendar(reservationsTimestampsSet);
1395                //System.out.println("reservationCalendar"+reservationCalendar);
1396                /*for(int i=size-1;i>=0;i--){
1397                        calendarNEW.remove(i);
1398                }
1399                Iterator<Long> it = calendar.keySet().iterator();
1400                while (it.hasNext()) {
1401                        Long time = (Long) it.next();
1402                        Double value = (Double)calendar.get(time);
1403                        AvailableBandwidth ab= new AvailableBandwidth(time, (long)getStartBaduRate()-value.longValue());
1404                        calendarNEW.add(ab);
1405                }*/
1406               
1407                List<AvailableBandwidth> newCalendar = new ArrayList<AvailableBandwidth>();
1408                Iterator<Long> it = reservationsCalendar.keySet().iterator();
1409                Long previousTime = new Long(0);
1410                while (it.hasNext()) {
1411                        Long time = (Long) it.next();
1412                        Double value = (Double)reservationsCalendar.get(time);
1413                        AvailableBandwidth ab = new AvailableBandwidth(previousTime, (long)getStartBaduRate() - value.longValue());
1414                        newCalendar.add(ab);
1415                        previousTime = time;
1416
1417                }
1418                AvailableBandwidth ab = new AvailableBandwidth(previousTime, (long)getStartBaduRate());
1419                newCalendar.add(ab);
1420                setBandwidthCalendar(newCalendar);
1421                //reverseCalendar();
1422        }
1423       
1424        private TreeMap<Long, Double> createReservationsCalendar(
1425                        LinkedHashSet<Long> reservationsTimestampsSet) {
1426                TreeMap<Long, Double> reservationsCalendar = new TreeMap<Long, Double>();
1427
1428                Iterator<Long> it = reservationsTimestampsSet.iterator();
1429                while (it.hasNext()) {
1430                        Long timestamp = (Long) it.next();
1431                        reservationsCalendar.put(timestamp, 0.0);
1432                }
1433                Iterator it2 = reservationList.iterator();
1434                while (it2.hasNext()) {
1435                        NetworkReservationObject netResObj = (NetworkReservationObject) it2.next();
1436                        long startTime = netResObj.getStartTime();
1437                        long endTime = netResObj.getEndTime();
1438                        Iterator<Long> it3 = reservationsCalendar.keySet().iterator();
1439                        while (it3.hasNext()) {
1440                                Long timestamp = (Long) it3.next();
1441                                if (timestamp > startTime && timestamp <= endTime) {
1442                                        reservationsCalendar.put(timestamp,
1443                                                        (Double) reservationsCalendar.get(timestamp)
1444                                                                        + (Double) netResObj.getBandwidth());
1445                                }
1446                        }
1447
1448                }
1449                return reservationsCalendar;
1450        }
1451
1452       
1453        private LinkedHashSet<Long> createReservationsTimestampsSet() {
1454                LinkedHashSet<Long> reservationsTimestampsSet = new LinkedHashSet<Long>();
1455
1456                Iterator it = reservationList.iterator();
1457                while (it.hasNext()) {
1458                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
1459                        long timestamp = netResObj.getStartTime();
1460                        reservationsTimestampsSet.add(timestamp);
1461                        timestamp = netResObj.getEndTime();
1462                        reservationsTimestampsSet.add(timestamp);
1463
1464                }
1465                return reservationsTimestampsSet;
1466        }
1467
1468        /*private double getMaxCalendarValue(TreeMap<Long, Double> calendar,
1469                        long startTime, long endTime) {
1470                long endDateInCalendar = getCeilKey(calendar, endTime);
1471                TreeMap<Long, Double> partOfCalendar = getPartOfCalendar(calendar,
1472                                startTime, false, endDateInCalendar, true);
1473                Double value;
1474                if (partOfCalendar.size() == 0)
1475                        value = 0.0;
1476                else
1477                        value = (Double) Collections.max(partOfCalendar.values());
1478                return value;
1479        }
1480        private long getCeilKey(TreeMap<Long, Double> calendar, Long value) {
1481                Iterator<Long> it = calendar.keySet().iterator();
1482                while (it.hasNext()) {
1483                        Long timestamp = (Long) it.next();
1484                        if (timestamp >= value)
1485                                return timestamp;
1486                }
1487                return value;
1488        }
1489       
1490
1491        private TreeMap<Long, Double> getPartOfCalendar(
1492                        TreeMap<Long, Double> map, long startKey, boolean includeStartKey,
1493                        long endKey, boolean includeEndKey) {
1494
1495                TreeMap<Long, Double> partMap = new TreeMap<Long, Double>();
1496                Iterator<Long> it = map.keySet().iterator();
1497                while (it.hasNext()) {
1498                        Long time = (Long) it.next();
1499                        if (time > startKey && time < endKey) {
1500                                partMap.put(time, map.get(time));
1501                        }
1502                        if (time == startKey && includeStartKey) {
1503                                partMap.put(time, map.get(time));
1504                        }
1505                        if (time == endKey && includeEndKey) {
1506                                partMap.put(time, map.get(time));
1507                                break;
1508                        }
1509
1510                }
1511                return partMap;
1512        }
1513         */
1514        private synchronized void updateBaudRate() {
1515                //System.out.println(GridSim.clock()+" UPDATE BAUD RATE "+get_name());
1516                handleFinishedReservations();
1517                activeReservationsBandwidth = getActiveReservationsBandwidth();
1518                setBaduRate(startBaudRate - activeReservationsBandwidth);
1519                if (super.getBaudRate() > lastBaudRate) {
1520                        //System.out.println(VirtualClock.getStaticCurrentDateTimeMillis()+":"+ExtendedGridSim.getCurrentTime());
1521                        //System.out.println("active flows" + activeFlows_.size());
1522                        //System.out.println(ExtendedGridSim.clock() + ";" + get_name() + ";"
1523                        //              + super.getBaudRate() + ";" + lastBaudRate);
1524                        increaseFlow();
1525                } else if (super.getBaudRate() < lastBaudRate) {
1526                        //System.out.println(System.currentTimeMillis()+":"+ExtendedGridSim.getCurrentTime());
1527                        //System.out.println(ExtendedGridSim.clock() + ";" + get_name() + ";"
1528                        //              + super.getBaudRate() + ";" + lastBaudRate);
1529                        decreaseFlow();
1530                }
1531                lastBaudRate = super.getBaudRate();
1532        }
1533
1534       
1535        public void addReservation(NetworkReservationObject netResObj){
1536                reservationList.add(netResObj);
1537                sendBaudRateUpdateEvent(((double)netResObj.getStartTime() - (double)ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC);
1538                sendBaudRateUpdateEvent(((double)netResObj.getEndTime() - (double) ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC);
1539
1540                createNEWCalendar();
1541        }
1542        private void sendBaudRateUpdateEvent(double delay){
1543                super.sim_schedule(get_id(), delay, ExtendedGridSimTags.UPDATE_BAUDRATE);
1544        }
1545
1546        /*public void addReservation(NetworkReservationObject netResObj){
1547                reservationList.add(netResObj);
1548                sendBaduRateUpdateEvent(((double)netResObj.getStartTime() - (double)ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC, ((double)netResObj.getEndTime() - ExtendedGridSim.getCurrentTime()) / (double)MILLI_SEC);
1549                createNEWCalendar();
1550        }*/
1551
1552        /*private void sendBaduRateUpdateEvent(double time1, double time2){
1553                super.sim_schedule(get_id(), time1, ExtendedGridSimTags.UPDATE_BAUDRATE);
1554                super.sim_schedule(get_id(), time2,     ExtendedGridSimTags.UPDATE_BAUDRATE);
1555        }*/
1556       
1557        public List<NetworkReservationObject> getCompletedReservationsList(){
1558                return this.completedReservationsList;
1559        }
1560
1561        public List getFlowsSizeCharacteristic(){
1562                return this.flowsSizeCharacteristic;
1563        }       
1564
1565        public Map getFlowsUsingReservationsCharacteristic(){
1566                return this.flowsUsingReservationsSizeCharacteristic;
1567        }
1568       
1569        public List<ReservationIntervalCharacteristic> getNumberOfReservations(DateTime border1, DateTime border2){
1570
1571                LinkedHashSet<Long> reservationsTimestampsSet = new LinkedHashSet<Long>();
1572
1573                Iterator it = reservationList.iterator();
1574                while (it.hasNext()) {
1575                        NetworkReservationObject netResObj = (NetworkReservationObject) it.next();
1576
1577                        if(netResObj.getEndTime() > border1.getMillis() && netResObj.getStartTime()<border2.getMillis())
1578                        {       long timestamp = netResObj.getStartTime();
1579                                reservationsTimestampsSet.add(timestamp);
1580                                timestamp = netResObj.getEndTime();
1581                                reservationsTimestampsSet.add(timestamp);
1582                        }
1583
1584                }
1585                reservationsTimestampsSet.add(border1.getMillis());
1586                reservationsTimestampsSet.add(border2.getMillis());
1587                List sortedList = new ArrayList(reservationsTimestampsSet);
1588                Collections.sort(sortedList);
1589               
1590                List<ReservationIntervalCharacteristic> resultList = null;
1591
1592                resultList = new ArrayList<ReservationIntervalCharacteristic>();
1593                int size = reservationsTimestampsSet.size();
1594                DateTime startDate = new DateTime(border1);
1595                DateTime endDate = null;
1596                Iterator<Long> it2 = sortedList.iterator();
1597                while (it2.hasNext()) {
1598                        Long timestamp = (Long) it2.next();
1599                        if(border1.getMillis() == timestamp)
1600                                continue;
1601                        endDate = new DateTime(timestamp);
1602                        ReservationIntervalCharacteristic ric = new ReservationIntervalCharacteristic(startDate, endDate, 0);
1603                        resultList.add(ric);
1604                        startDate = new DateTime(endDate);
1605                }
1606                if(border2.getMillis() != endDate.getMillis())
1607                {
1608                        ReservationIntervalCharacteristic ric = new ReservationIntervalCharacteristic(endDate, border2, 0);
1609                        resultList.add(ric);
1610                }
1611                Iterator it3 = resultList.iterator();
1612                while(it3.hasNext()){
1613                       
1614                        ReservationIntervalCharacteristic ric = (ReservationIntervalCharacteristic)it3.next();
1615                        Iterator it4 = reservationList.iterator();
1616                        while(it4.hasNext()){
1617                                NetworkReservationObject netResObj = (NetworkReservationObject)it4.next();
1618                                long startTime = netResObj.getStartTime();
1619                                long endTime = netResObj.getEndTime();
1620                                if(startTime < ric.getEndTime().getMillis() && endTime > ric.getStartTime().getMillis()){
1621                                        ric.setNrOfReservations(ric.getNrOfReservations() + 1);
1622                                }
1623                        }
1624                }
1625       
1626                return resultList;
1627        }
1628       
1629} // end class
1630
Note: See TracBrowser for help on using the repository browser.