[31] | 1 | /************************************************************************************ |
---|
| 2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
| 3 | * All rights reserved. |
---|
| 4 | * Refer to LICENSE for terms and conditions of use. |
---|
| 5 | ***********************************************************************************/ |
---|
| 6 | package vnsim.applications.vitp.utils; |
---|
| 7 | |
---|
| 8 | import java.util.*; |
---|
| 9 | |
---|
| 10 | public class MessageIndexModule { |
---|
| 11 | //an instance of this class manages the unique identifiers of the VITP |
---|
| 12 | //messages issued by the local node. It is used by the routing layer to |
---|
| 13 | //match incoming replies to pending requests |
---|
| 14 | |
---|
| 15 | public static int index=0; |
---|
| 16 | |
---|
| 17 | Vector<Integer> pending=new Vector<Integer>(); //the pending replies |
---|
| 18 | |
---|
| 19 | Vector<Integer> broadcasted=new Vector<Integer>(); //the |
---|
| 20 | //messages already broadcasted |
---|
| 21 | |
---|
| 22 | public synchronized static int getNewMessageIdentifier(){ |
---|
| 23 | index++; |
---|
| 24 | return index; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | public void addToPending(int id) { |
---|
| 28 | pending.addElement(new Integer(id)); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | public void addToBroacasted(int id) { |
---|
| 32 | broadcasted.addElement(new Integer(id)); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | public boolean isPending(int id){ |
---|
| 36 | for(int i=0;i<pending.size();i++){ |
---|
| 37 | if(((Integer)pending.elementAt(i)).intValue()==id) |
---|
| 38 | return true; |
---|
| 39 | } |
---|
| 40 | return false; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | public boolean wasBroadcasted(int id){ |
---|
| 44 | for(int i=0;i<broadcasted.size();i++){ |
---|
| 45 | if(((Integer)broadcasted.elementAt(i)).intValue()==id) |
---|
| 46 | return true; |
---|
| 47 | } |
---|
| 48 | return false; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | public void removeFromPending(int id){ |
---|
| 52 | pending.removeElement(new Integer(id)); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | public void clearPending(){ |
---|
| 56 | pending.removeAll(pending); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | private HashMap<Integer, Integer> sentMessages=new HashMap<Integer,Integer> (); |
---|
| 62 | // <messageId, vehicleId> |
---|
| 63 | // keeps track of which messages have been unicasted to which vehicle; |
---|
| 64 | // this is necessary for the reply-computation phase; when a node receives |
---|
| 65 | // the same message again, it should not send it to the same node (because |
---|
| 66 | // this suggests a loop) |
---|
| 67 | |
---|
| 68 | public void addToSent(int msgId, int vehicleId) { |
---|
| 69 | sentMessages.put(msgId, vehicleId); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public Integer sentTo(int msgId) { |
---|
| 73 | return sentMessages.get(msgId); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | } |
---|