1   package de.uni_stuttgart.informatik.canu.uomm;
2   
3   import jade.core.Profile;
4   import jade.core.ProfileImpl;
5   import jade.core.Runtime;
6   import jade.wrapper.AgentContainer;
7   import jade.wrapper.AgentController;
8   
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.StringTokenizer;
12  
13  import CanuMobiSim.de.uni_stuttgart.informatik.canu.mobisim.notifications.VehicleCountReachedNotification;
14  import CanuMobiSim.de.uni_stuttgart.informatik.canu.mobisim.notifications.VehiclesOnRIQNotification;
15  
16  import de.uni_stuttgart.informatik.canu.mobisim.core.ExtensionModule;
17  import de.uni_stuttgart.informatik.canu.mobisim.core.Notification;
18  import de.uni_stuttgart.informatik.canu.mobisim.core.Position3D;
19  import de.uni_stuttgart.informatik.canu.mobisim.core.Universe;
20  import de.uni_stuttgart.informatik.canu.mobisim.notifications.FinalDestinationReachedNotification;
21  import de.uni_stuttgart.informatik.canu.mobisim.notifications.VehiclesInRangeNotification;
22  import examples.bookTrading.NegotiatingVehicleAgent;
23  import examples.bookTrading.DataAccess;
24  import examples.bookTrading.QueueManager;
25  
26  /**
27   * This class initialises the creation of agents in JADE and is also responsible
28   * for updating their specific parameters based on dynamic changes in vehicular
29   * mobility pattern.
30   * 
31   * @author prajakta
32   * 
33   */
34  public class JADECommunicationModule extends ExtensionModule {
35  
36      private static AgentContainer ac = null;
37  
38      public JADECommunicationModule() {
39          super("JADECommunicationModule");
40  
41          u.addNotificationListener(this);
42      }
43  
44      @Override
45      public int act() {
46  
47          return 0;
48      }
49  
50      @Override
51      public String getDescription() {
52          // TODO Auto-generated method stub
53          return null;
54      }
55  
56      /**
57       * This is the notification passing method
58       */
59      public synchronized void sendNotification(Notification notification) {
60          VehiclesInRangeNotification notif;
61          if (notification instanceof VehiclesInRangeNotification) {
62              notif = (VehiclesInRangeNotification) notification;
63              // Jun27
64              String notifSender = (String) notif.getOwner();
65              String notifReciever = (String) notif.getNode();
66  
67              // Jun27
68              String notifCommID = notif.getCommunID();
69              String vehLst = notif.getVehicles();
70              String yCoordinates = notif.getYCoord();
71              String xCoordinates = notif.getXCoord();
72              boolean lockFlag = notif.isLockFlag();
73              boolean commonSegmentFlag = notif.isOnCommonSegment();
74  
75              // System.out.println("comm module lock flag: "+lockFlag);
76              boolean rngFlag = notif.isRngChng();
77              // Jun27 int jnOrder = notif.getJuncOrder();
78              // System.out.println("junction order in comm module : "+jnOrder);
79              // if(notifSender.equals("#0")&& notifReciever.equals("#1")){
80              Object[] classArgs = { new String("#8"), new String("#2"), "2",
81                      notifCommID, vehLst, rngFlag, lockFlag, commonSegmentFlag,
82                      yCoordinates, xCoordinates /* ,jnOrder */};
83  
84              String host = "131.172.245.98";
85              Runtime rt = Runtime.instance();
86              rt.setCloseVM(true);
87              Profile p = new ProfileImpl(false);
88  
89              // System.out.println("commonSegmentFlag "+commonSegmentFlag);
90  
91              AgentController dummy, dummy1;
92  
93              try {
94                  if (ac == null)
95                      ac = rt.createAgentContainer(p);
96                  
97                  synchronized (ac) {
98                      // System.out.println("vehList..."+vehLst);
99                      if (vehLst != null) {
100                         if (vehLst.indexOf(",") > 0) {
101 
102                             StringTokenizer st = new StringTokenizer(vehLst,
103                                     ",");
104                             if (st != null) {
105                                 while (st.hasMoreElements()) {
106                                     String agent = (String) st.nextElement();
107                                     if (agent != null) {
108 
109                                         if (QueueManager.getAgentName(agent) == null) {
110                                             // System.out.println("Starting up a DummyAgent "+agent
111                                             // + " invoked by "+notifSender);
112                                             dummy = QueueManager
113                                                     .createNewAgent(
114                                                             ac,
115                                                             agent,
116                                                             "examples.bookTrading.NegotiatingVehicleAgent",
117                                                             classArgs);
118                                             if (vehLst != null
119                                                     && notifSender != null
120                                                     && notifSender.equals("#2")) {
121                                                 // if(xCoordinates.equals("60.0")
122                                                 // &&
123                                                 // yCoordinates.equals("240.0")){
124                                                 // System.out.println("Printing agents in vicinity........IF"+vehLst+" "+xCoordinates+" "+yCoordinates);
125                                                 // }
126                                             }
127                                             // Fire up the agent
128 
129                                             if (dummy != null)
130                                                 dummy.start();
131                                             // lock.waitOn();
132                                         }// new code
133                                         else {
134                                             // if(rngFlag){
135                                             NegotiatingVehicleAgent ag = QueueManager
136                                                     .getAgent(agent);
137                                             if (ag != null) {
138                                                 // System.out.println("sending vehicles in range notificiation..."+vehLst+" and agent cnt...."+vehNotif.getAgentCnt()+" for communic ID "+notifCommID);
139                                                 ag.setRngChk(true);
140                                                 ag.setAgentsInVicinity(vehLst);
141                                                 ag.setyCoordinatesOfEdge(yCoordinates);
142                                                 ag.setxCoordinatesOfEdge(xCoordinates);
143                                                 ag.setCommunicID(notifCommID);
144                                                 // if(!(xCoordinates.equals("0.0")
145                                                 // &&
146                                                 // yCoordinates.equals("240.0"))){
147                                                 // System.out.println("sending vehicle range notificiation for commID..."+notifCommID+" and agent cnt...."+vehLst+" by "+ag.getLocalName());
148                                                 // }
149                                                 // if(ag.getOwner().equals("#15")
150                                                 // &&
151                                                 // ag.getAgentsInVicinity().indexOf("#15")>-1
152                                                 // &&
153                                                 // ag.getAgentsInVicinity().indexOf("#7")>-1){
154                                                 if (vehLst != null
155                                                         && notifSender != null
156                                                         && notifSender
157                                                                 .equals("#2")) {
158                                                     if (xCoordinates
159                                                             .equals("60.0")
160                                                             && yCoordinates
161                                                                     .equals("240.0")) {
162                                                         // System.out.println("Printing agents in vicinity........ELSE"+vehLst+" "+xCoordinates+" "+yCoordinates);
163                                                     }
164                                                 }
165                                                 // }
166                                                 // System.out.println("vehicles in range...."+vehLst+"for....."+notifCommID);
167                                                 // ag.setJnOrder(jnOrder);
168                                                 if (/* u.isJnReachedFlag() */lockFlag) {
169                                                     // System.out.println("VANETVehicle "+ag.getLocalName()+"reached the junction");
170                                                     ag.setJunctionReached(true);
171                                                 } else {
172                                                     ag.setJunctionReached(false);
173                                                 }
174                                             }
175                                             // }
176 
177                                         }
178                                         // new code
179                                     }
180                                 }
181                             }
182                         }
183                     }
184                 }
185 
186             } catch (Exception e) {
187                 // TODO Auto-generated catch block
188                 e.printStackTrace();
189             }
190 
191             // }
192         } else if (notification instanceof FinalDestinationReachedNotification) {
193             FinalDestinationReachedNotification destNotif = (FinalDestinationReachedNotification) notification;
194             String node = destNotif.getOwner();
195             NegotiatingVehicleAgent ag = QueueManager.getAgent(node);
196             if (ag != null)
197                 ag.setDestReachedFlag(true);
198 
199         } else if (notification instanceof VehiclesOnRIQNotification) {
200             VehiclesOnRIQNotification vehNotif = (VehiclesOnRIQNotification) notification;
201             String node = vehNotif.getOwner();
202             NegotiatingVehicleAgent ag = QueueManager.getAgent(node);
203             if (ag != null) {
204                 ag.setVehOnRIQNotif(true);
205                 ag.setAgentsInVicinity(vehNotif.getVehicles());
206 
207             }
208         } else if (notification instanceof VehicleCountReachedNotification) {
209            VehicleCountReachedNotification vehNotif = (VehicleCountReachedNotification) notification;
210             String node = vehNotif.getOwner();
211             NegotiatingVehicleAgent ag = QueueManager.getAgent(node);
212             if (ag != null) {
213                 ag.setCommunicID(vehNotif.getCommID());
214                 ag.setCongSegID(vehNotif.getCongSegID());
215                 ag.setAgentCnt(vehNotif.getAgentCnt());
216                 ag.setxCoordinatesOfEdge(vehNotif.getxCoordinatesOfEdge());
217                 ag.setyCoordinatesOfEdge(vehNotif.getyCoordinatesOfEdge());
218                 // g.setOwner(vehNotif.getOwner());
219                 // if(vehNotif!=null && vehNotif.getCongSegID()!=null &&
220                 // vehNotif.getCongSegID().equals("s4") &&
221                 // vehNotif.getOwner().equals("#44")){
222                 System.out.println("sending vehicle count notificiation..."
223                         + vehNotif.getCongSegID() + " and agent cnt...."
224                         + vehNotif.getAgentCnt() + " by " + node
225                         + " for communication Id: " + vehNotif.getCommID());
226                 // }
227             }
228         }
229     }
230 
231     public void load(org.w3c.dom.Element element) throws java.lang.Exception {
232         // my code
233         u.addNotificationListener(this);
234         // my code
235     }
236 
237 }
238