1   package de.uni_stuttgart.informatik.canu.uomm;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.Map;
7   import java.util.Map.Entry;
8   import java.util.Set;
9   import java.util.StringTokenizer;
10  import java.util.concurrent.ConcurrentHashMap;
11  
12  import de.uni_stuttgart.informatik.canu.mobisim.core.Position3D;
13  import de.uni_stuttgart.informatik.canu.senv.core.Vertex;
14  import examples.bookTrading.DataAccess;
15  import examples.bookTrading.StoredCommunicationInformation;
16  
17  public class CommunicationsManager {
18  
19      public static HashMap listOfVehicles = new HashMap();
20      public static HashMap listOfRIQVehicles = new HashMap();
21      public static ConcurrentHashMap<String,ArrayList> actVehPos = new ConcurrentHashMap<String,ArrayList>();
22      public static ConcurrentHashMap<String,Position3D> initVehPos = new ConcurrentHashMap<String,Position3D>();
23      public static ConcurrentHashMap<String,Position3D> destVehPos = new ConcurrentHashMap<String,Position3D>();
24      public static ConcurrentHashMap communicationStarted = new ConcurrentHashMap();
25      public static ConcurrentHashMap agentCreated = new ConcurrentHashMap();
26      private static boolean returnAgentCreationFlag=false;
27  
28      public static void setAgentCreated(String agentToCreate) {
29          agentCreated.put(agentToCreate, true);
30      }
31  
32      public static boolean getAgentCreated(String agentCreatedID) {
33          if(agentCreated!=null && agentCreatedID!=null){
34              if(agentCreated.get(agentCreatedID)!=null){
35                  return true;
36              }
37          }
38          return false;
39  
40      }
41  
42      //this map maintains a mapping from the second end of the vertex to the forthcoming congested road
43      public static ConcurrentHashMap mapOfVertexToSegmentID = new ConcurrentHashMap();
44  
45  
46      public static ConcurrentHashMap communicationRecordedVehicles = new ConcurrentHashMap();
47  
48      /**
49       * This method is used for initialising the map of coordinates and segment ID
50       */
51      public static void initialiseTheMap(){
52          if(communicationRecordedVehicles!=null && communicationRecordedVehicles.size()==0){
53              //congestion - s1,s2,s37,s43,s31,s11,s17,s10
54  
55              communicationRecordedVehicles.put("12.0,8.0", "s1");
56              communicationRecordedVehicles.put("24.5,21.0", "s2");
57          /*  communicationRecordedVehicles.put("11.0,47.0", "s37");
58              communicationRecordedVehicles.put("17.0,55.0", "s39");
59              communicationRecordedVehicles.put("22.5,90.0", "s43");
60              communicationRecordedVehicles.put("46.5,41.0", "s24");*/
61          //  communicationRecordedVehicles.put("71.0,131.0", "s31");
62          //  communicationRecordedVehicles.put("83.0,129.0", "s11");
63          //  communicationRecordedVehicles.put("83.0,129.0", "s10");
64  
65      //      communicationRecordedVehicles.put("43.0,84.5", "s27");
66  
67              //communicationRecordedVehicles.put("96.5,232.0", "s17");
68  
69  
70              //communicationRecordedVehicles.put("180.0,240.0", "s4");
71      ////    communicationRecordedVehicles.put("240.0,240.0", "s4");
72          //  communicationRecordedVehicles.put("300.0,240.0", "s6");
73  ////        communicationRecordedVehicles.put("360.0,240.0", "s6");
74          ////    communicationRecordedVehicles.put("240.0,180.0", "s43");
75          //  communicationRecordedVehicles.put("360.0,120.0", "s62");
76  ////        communicationRecordedVehicles.put("360.0,60.0", "s62");
77  
78              //communicationRecordedVehicles.put("240.0,120.0", "s60");
79      ////    communicationRecordedVehicles.put("240.0,60.0", "s60");
80          }
81      }
82  
83      public static ConcurrentHashMap returnCommunicationRecordedVehicles(){
84          return communicationRecordedVehicles;
85      }
86  
87      public static synchronized boolean getCommunicationStarted(String vehicleID, Vertex toCheck) {
88          if(communicationStarted!=null){
89              Vertex commStartedFlag = (Vertex)communicationStarted.get(vehicleID);
90              if(commStartedFlag==null){
91                  if(toCheck.getX()==0.0 && toCheck.getY()==180.0){
92                  }
93                  return false;
94              }else{
95  
96                  if(commStartedFlag.getX()==toCheck.getX() && commStartedFlag.getY()==toCheck.getY()){
97                      if(toCheck.getX()==0.0 && toCheck.getY()==180.0){
98                      }
99                      return true;
100                 }else{
101                     if(toCheck.getX()==0.0 && toCheck.getY()==180.0){
102                     }
103                     return false;
104                 }
105             }
106         }else{
107     //      System.out.println("communicationStarted returning FALSE");
108             return false;
109         }
110     }
111 
112     public static synchronized void addToCommunicationStarted(String vehicleID,Vertex toAdd) {
113         int indexOf2 =0;
114         String firstHalf=null, nextHalf=null;
115         if(toAdd!=null && toAdd.getX() != 0.0){
116 
117         }
118     //  System.out.println("addToCommunicationStarted "+ toAdd.getX()+" & "+ toAdd.getY()+" for vehicleID "+vehicleID);
119         communicationStarted.put(vehicleID, toAdd);
120 
121         String ID = checkIfNodesInTheRing(vehicleID);
122         if(ID!=null){
123             String vehListNew = (String) listOfVehicles.get(ID);
124             try{
125                 if(vehListNew!=null){
126                     indexOf2 = vehListNew.indexOf(vehicleID);
127                     if(vehListNew.length()>3){
128                     firstHalf= vehListNew.substring(0,indexOf2-1);
129                     if(vehicleID!=null && vehicleID.length()==2){
130                         nextHalf = vehListNew.substring(indexOf2+2,vehListNew.length());
131                     }else if(vehicleID!=null && vehicleID.length()==3){
132                         nextHalf = vehListNew.substring(indexOf2+3,vehListNew.length());
133                     }
134             //  System.out.println("for vehicleID "+vehicleID+ "indexof2 "+indexOf2+" "+ firstHalf+" "+nextHalf+"vehListNew "+vehListNew+" addToCommunicationStarted "+ toAdd.getX()+" & "+ toAdd.getY());
135                     vehListNew = firstHalf+nextHalf;
136                     }else if(indexOf2>2){
137                         firstHalf = vehListNew.substring(0,0);
138                         nextHalf = vehListNew.substring(indexOf2+3,vehListNew.length());
139             //      System.out.println("for vehicleID "+vehicleID+ "indexof2 "+indexOf2+" with firsthalf:  "+ firstHalf+" with nexthalf: "+nextHalf+"vehListNew "+vehListNew+" addToCommunicationStarted "+ toAdd.getX()+" & "+ toAdd.getY());
140                         vehListNew = firstHalf+nextHalf;
141                     }
142 
143                     removeFromCommRing(ID);
144                     addToCommRing(ID,vehListNew);
145                 }
146 
147         }catch(Exception e){
148     //      System.out.println("exception-->for vehicleID "+vehicleID+" indexof2 "+indexOf2+" with firsthalf:  "+ firstHalf+" with nextHalf "+nextHalf+"vehListNew "+vehListNew);
149         //  e.printStackTrace();
150         }
151         }
152     }
153 
154 /**
155  * This method is used to remove vehicles in range list
156  * @param vehicleOwnerID vehicle owner ID
157  * @param vehicleNodeID vehicle node ID
158  */
159     public static synchronized void removeFromVehInRngList(String vehicleOwnerID,String vehicleNodeID)
160     {
161         int indexOf2 =0;
162         String firstHalf=null, nextHalf=null;
163         boolean flagPresent = false;
164         String newString  = "";
165         String iterStr = null;
166         String ID = checkIfNodesInTheRing(vehicleOwnerID);
167         if(ID!=null){
168             String vehListNew = (String) listOfVehicles.get(ID);
169             StringTokenizer st = new StringTokenizer(vehListNew,",");
170             while(st!=null && st.hasMoreTokens()){
171                 iterStr = st.nextToken();
172                 if(iterStr!=null && iterStr.equals(vehicleNodeID)){
173 
174                     flagPresent = true;
175                 }else{
176                     if(newString.length()==0){
177                         newString = newString.concat(iterStr);
178                     }else{
179                         newString = newString.concat(",");
180                         newString = newString.concat(iterStr);
181                     }
182                 }
183 
184             }
185 
186 
187         }
188 
189         if(!flagPresent){
190             newString=null;
191         }else{
192             removeFromCommRing(ID);
193             addToCommRing(ID,newString);
194         //  System.out.println("removed : "+vehicleNodeID+ " from "+newString);
195         }
196     }
197 
198     public static ArrayList actList = new ArrayList();
199 
200     public static synchronized void addToCommRing(String commID, String vehiclesList) {
201     //  System.out.println("ADDED "+vehiclesList+ " to the RING : "+commID);
202         listOfVehicles.put(commID, vehiclesList);
203 
204     }
205 
206     public static void addToRIQCommRing(String commID, String vehiclesList) {
207         listOfRIQVehicles.put(commID, vehiclesList);
208     }
209 
210     public static synchronized void removeFromCommRing(String commID) {
211     //  System.out.println("REMOVED "+commID+ " from the RING : ");
212         listOfVehicles.remove(commID);
213 
214     }
215     public static void addToList(Vertex vertex) {
216 //      System.out.println("Communication Manager: "+vertex.getX()+vertex.getY());
217         actList.add(vertex);
218     }
219 
220     /**
221      * This method returns a list of vehicles based on communication ID
222      * @param commnID communication ID
223      * @return list of vehicles
224      */
225     public static synchronized String returnVehicleList(String commnID){
226         Set entrySet = listOfVehicles.entrySet();
227 
228         String returnList = null;
229         String commID = null;
230         String list = null;
231         if(entrySet!=null){
232         for (Iterator it = entrySet.iterator(); it.hasNext();) {
233             Map.Entry entry = (Entry) it.next();
234             commID = (String) entry.getKey();
235             returnList = entry.getValue().toString();
236         //  System.out.println("comm "+commID+" and return list "+returnList);
237             if(commnID!=null && commnID.equals(commID)){
238                 return returnList;
239             }
240         }
241         }
242         return returnList;
243     }
244 
245     /**
246      * This method checks if the given vehicle is already added to the communication ring
247      * @param vehicleID ID of the vehicle
248      * @return list of vehicles
249      */
250     public static synchronized String checkIfNodesInTheRing(String vehicleID) {
251         Set entrySet = listOfVehicles.entrySet();
252 
253         String returnList = null;
254         String commID = null;
255         String list = null;
256         for (Iterator it = entrySet.iterator(); it.hasNext();) {
257             Map.Entry entry = (Entry) it.next();
258             commID = (String) entry.getKey();
259             list = entry.getValue().toString();
260             if (list != null) {
261                 StringTokenizer st = new StringTokenizer(list, ",");
262                 while (st.hasMoreElements()) {
263                     String elem = (String) st.nextElement();
264                         if(elem != null) {
265 
266                         if(elem.equals(vehicleID))
267                         return commID ;
268                         }
269                     }
270 
271             }
272         }
273 
274         return null;
275     }
276 
277 
278     public static synchronized String checkIfNodesInTheRIQRing(String vehicleID) {
279         Set entrySet = listOfRIQVehicles.entrySet();
280 
281         String returnList = null;
282         String commID = null;
283         String list = null;
284         for (Iterator it = entrySet.iterator(); it.hasNext();) {
285             Map.Entry entry = (Entry) it.next();
286             commID = (String) entry.getKey();
287             list = entry.getValue().toString();
288             if (list != null) {
289                 StringTokenizer st = new StringTokenizer(list, ",");
290                 while (st.hasMoreElements()) {
291                     String elem = (String) st.nextElement();
292                         if(elem != null) {
293 
294                         if(elem.equals(vehicleID))
295                         return commID ;
296                         }
297                     }
298 
299             }
300         }
301 
302         return null;
303     }
304 
305     public static void clearNodesInRing()
306     {
307         if(listOfVehicles!=null && listOfVehicles.size()>0)
308         listOfVehicles = new HashMap();
309     }
310 
311     /**
312      * This method adds the vehicle and its position to the list
313      * @param vID vehicle ID
314      * @param vd position of the vehicle
315      */
316     public static void addVehPos(String vID,Position3D vd)
317     {
318         Set entrySet = actVehPos.entrySet();
319         ArrayList aList = new ArrayList();
320         boolean flagFound = false;
321         boolean listContains = false;
322         String returnList = null;
323         String vehID = null;
324         ArrayList list = null;
325         for (Iterator it = entrySet.iterator(); it.hasNext();) {
326             Map.Entry entry = (Entry) it.next();
327             vehID = (String) entry.getKey();
328             list = (ArrayList) entry.getValue();
329 
330             if (list != null) {
331                 if(vID!=null && vID.equals(vehID))
332                 {
333                     if(!listContainsPos(vID,list,vd)){
334                         if(invalidPos(vd)){
335 
336                             listContains = true;
337                             break;
338                         }
339                     list.add(vd);
340 
341                     flagFound = true;
342                     break;
343                     }else{
344                         listContains = true;
345                         break;
346                     }
347                 }
348 
349             }
350 
351         }
352         if(!flagFound && !listContains){
353             aList.add(vd);
354             actVehPos.put(vID, aList);
355         }
356     }
357 
358 
359     private static boolean invalidPos(Position3D vd) {
360 
361         ArrayList vertList   = new ArrayList();
362         try {
363             vertList.add(new Vertex("dest","3","100.0","400.0"));
364             vertList.add(new Vertex("dest","3","100.0","300.0"));
365             vertList.add(new Vertex("dest","3","200.0","400.0"));
366             vertList.add(new Vertex("dest","3","200.0","300.0"));
367             vertList.add(new Vertex("dest","3","300.0","300.0"));
368             vertList.add(new Vertex("dest","3","300.0","400.0"));
369             vertList.add(new Vertex("dest","3","400.0","300.0"));
370             vertList.add(new Vertex("dest","3","400.0","200.0"));
371             vertList.add(new Vertex("dest","3","500.0","300.0"));
372             vertList.add(new Vertex("dest","3","500.0","400.0"));
373             vertList.add(new Vertex("dest","3","400.0","400.0"));
374             vertList.add(new Vertex("dest","3","200.0","200.0"));
375             vertList.add(new Vertex("dest","3","300.0","200.0"));
376             vertList.add(new Vertex("dest","3","100.0","200.0"));
377             vertList.add(new Vertex("dest","3","100.0","200.0"));
378             vertList.add(new Vertex("dest","3","200.0","200.0"));
379             vertList.add(new Vertex("dest","3","300.0","200.0"));
380             vertList.add(new Vertex("dest","3","400.0","200.0"));
381             vertList.add(new Vertex("dest","3","500.0","200.0"));
382             vertList.add(new Vertex("dest","3","400.0","220.0"));
383             vertList.add(new Vertex("dest","3","400.0","100.0"));
384 
385 
386 
387         } catch (Exception e1) {
388             // TODO Auto-generated catch block
389             e1.printStackTrace();
390         }
391 
392 
393 
394 
395                 try {
396                     Vertex v = new Vertex("dest","3",new Double(vd.getX()).toString(),new Double(vd.getY()).toString());
397                     if(v!=null && vertList!=null){
398                         if(vertList.contains(v))
399                             return false;
400                         else {
401 
402                             return true;
403                         }
404                     }
405                 } catch (Exception e) {
406                     // TODO Auto-generated catch block
407                     e.printStackTrace();
408                 }
409 
410 
411         return true;
412     }
413 
414     public static ArrayList getActualTraversalVertices(String vID)
415     {
416         Set entrySet = actVehPos.entrySet();
417         ArrayList aList = new ArrayList();
418         boolean flagFound = false;
419         String returnList = null;
420         String vehID = null;
421         ArrayList list = null;
422         for (Iterator it = entrySet.iterator(); it.hasNext();) {
423             Map.Entry entry = (Entry) it.next();
424             vehID = (String) entry.getKey();
425             list = (ArrayList) entry.getValue();
426 
427             if (list != null) {
428                 if(vID!=null && vID.equals(vehID))
429                 {
430                 break;
431                 }
432 
433             }
434 
435         }
436 
437         return list;
438     }
439 
440     private static boolean listContainsPos(String vID, ArrayList al,Position3D vd) {
441         Iterator iter = al.iterator();
442         while(iter.hasNext()){
443             Position3D pos = (Position3D) iter.next();
444             if(pos.getX()==vd.getX() && pos.getY() == vd.getY())
445                 return true;
446         }
447         return false;
448     }
449 
450      public static Vertex getClosestVertex(double x, double y)
451       {
452 
453          double d_min = Double.MAX_VALUE;
454         Vertex v_res = null;
455 
456         java.util.Iterator iter = actList.iterator();
457         while (iter.hasNext())
458         {
459           Vertex v = (Vertex)iter.next();
460           double d = Math.sqrt((v.getX()-x)*(v.getX()-x)+(v.getY()-y)*(v.getY()-y));
461           if (d<d_min)
462           {
463             d_min = d;
464             v_res = v;
465           }
466         }
467 
468         return v_res;
469       }
470      public static Position3D retrieveInitPos(String vehicleID)
471      {
472          Position3D initPos = initVehPos.get(vehicleID);
473          return initPos;
474      }
475      public static Position3D storeInitPos(String vehicleID)
476      {
477          Position3D initPos = DataAccess.getInitialPosition(vehicleID);
478          initVehPos.put(vehicleID, initPos);
479          return initPos;
480      }
481      public static Position3D storeDestPos(String vehicleID)
482      {
483          Position3D initPos = DataAccess.getDestinationPosition(vehicleID);
484          destVehPos.put(vehicleID, initPos);
485          return initPos;
486      }
487 
488      public static Position3D retrieveDestPos(String vehicleID)
489      {
490          Position3D initPos = destVehPos.get(vehicleID);
491          return initPos;
492      }
493 
494      public static void addToCommunicationRecordedVehiclesMap(Vertex src, Vertex dest, String vehicles, String commID,String congSegID, int cnt){
495          StoredCommunicationInformation storedInfo =  new StoredCommunicationInformation(src, dest,commID,vehicles,cnt,congSegID);
496          communicationRecordedVehicles.put(commID, storedInfo);
497 
498      }
499 
500 
501      public static boolean notifSent(Vertex src, Vertex dest, String vehicles, String commID,String congSegID, int cnt){
502 
503          StoredCommunicationInformation storedInfo =  new StoredCommunicationInformation();
504 
505 
506          if(commID!=null && communicationRecordedVehicles!=null){
507              storedInfo = (StoredCommunicationInformation) communicationRecordedVehicles.get(commID);
508 
509              if(storedInfo!=null){
510 
511                  if(storedInfo.getSource().getX() == src.getX() && storedInfo.getSource().getY() == src.getY()){
512                      if(storedInfo.getDestination().getX() == dest.getX() && storedInfo.getDestination().getY() == dest.getY()){
513 
514                          if(congSegID!=null){
515                          if(storedInfo.getCongSegID().equals(congSegID) && storedInfo.getCount()==cnt){
516                          String listVeh  = storedInfo.getListOfVehicles();
517 
518                          if(listVeh!=null){
519                              StringTokenizer st = new StringTokenizer(listVeh,",");
520 
521                              while(st!=null && st.hasMoreTokens()){
522 
523                                  String vehicle = st.nextToken();
524 
525                                  if(vehicle!=null && vehicles.indexOf(vehicle)<=-1){
526                                      return false;
527                                  }
528 
529                              }
530                              return true;
531                          }else{
532                              return false;
533                          }
534 
535                          }else{
536                              return false;
537                          }
538                         } //congSegId !=null
539                          else{
540 
541                                  String listVeh  = storedInfo.getListOfVehicles();
542 
543                                  if(listVeh!=null){
544                                      StringTokenizer st = new StringTokenizer(listVeh,",");
545 
546                                      while(st!=null && st.hasMoreTokens()){
547 
548                                          String vehicle = st.nextToken();
549 
550                                          if(vehicle!=null && vehicles.indexOf(vehicle)<=-1){
551                                              return false;
552                                          }
553 
554                                      }
555                                      return true;
556                                  }else{
557                                      return false;
558                                  }
559 
560 
561                          }
562                      }//destination matcheselse{
563                      return false;
564                  }else{
565                      return false;
566                  }//src matches
567              }else{
568                  return false;
569              }
570 
571 
572          }else{
573              return false;
574          }
575 
576 
577 
578 
579      }
580 
581 
582 
583 }
584