OverSim
TopologyVis.cc
Go to the documentation of this file.
1 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
23 #include <omnetpp.h>
24 
25 #include <NodeHandle.h>
26 #include <GlobalNodeList.h>
27 #include <GlobalNodeListAccess.h>
28 #include <PeerInfo.h>
29 
30 #include "TopologyVis.h"
31 
32 
34 {
35  thisTerminal = NULL;
36  globalNodeList = NULL;
37 }
38 
39 void TopologyVis::initVis(cModule* terminal)
40 {
41  thisTerminal = terminal;
43 
44  // set up arrow-gates
45 // thisOutGateArray = thisTerminal->gate("overlayNeighborArrowOut");
46 // thisInGateArray = thisTerminal->gate("overlayNeighborArrowIn");
47  thisTerminal->setGateSize("overlayNeighborArrowOut", 1);
48  thisTerminal->setGateSize("overlayNeighborArrowIn", 1);
49 
50 }
51 
53  bool flush, const char* displayString)
54 {
55  if (!ev.isGUI() || !thisTerminal)
56  return;
57 
58  char red[] = "ls=red,1";
59 
60  if (displayString == NULL)
61  displayString = red;
62 
63  cModule* neighborTerminal;
64 
65  // flush
66  if (flush) {
67  for (int l = 0; l < thisTerminal->gateSize("overlayNeighborArrowOut"); l++) {
68  cGate* tempGate =
69  thisTerminal->gate("overlayNeighborArrowOut", l)->getNextGate();
70 
71  thisTerminal->gate("overlayNeighborArrowOut", l)->disconnect();
72  if (tempGate != NULL)
73  compactGateArray(tempGate->getOwnerModule(), VIS_IN);
74  }
75  thisTerminal->setGateSize("overlayNeighborArrowOut" ,0);
76  }
77 
78  if (globalNodeList->getPeerInfo(neighbor) == NULL)
79  return;
80 
81  neighborTerminal = simulation.getModule(globalNodeList->
82  getPeerInfo(neighbor)->getModuleID());
83 
84  if (neighborTerminal == NULL)
85  return;
86 
87  if (thisTerminal == neighborTerminal)
88  return;
89 
90  //do not draw double
91  for (int i = 0; i < thisTerminal->gateSize("overlayNeighborArrowOut"); i++)
92  if (thisTerminal->gate("overlayNeighborArrowOut", i)
93  ->getNextGate() != NULL &&
94  neighborTerminal == thisTerminal
95  ->gate("overlayNeighborArrowOut", i)
96  ->getNextGate()->getOwnerModule())
97  return;
98 
99  // IN
100  int i = 0;
101  if (neighborTerminal->gateSize("overlayNeighborArrowIn") == 0) {
102  neighborTerminal->setGateSize("overlayNeighborArrowIn", 1);
103  } else {
104  for (i = 0; i < neighborTerminal->gateSize("overlayNeighborArrowIn") - 1; i++) {
105  if (!(neighborTerminal->gate("overlayNeighborArrowIn", i)
106  ->isConnectedOutside()))
107  break;
108  }
109  if (neighborTerminal->gate("overlayNeighborArrowIn", i)
110  ->isConnectedOutside()) {
111  neighborTerminal->setGateSize("overlayNeighborArrowIn", i + 2);
112  i++;
113  }
114  }
115 
116  // OUT
117  int j = 0;
118  if (thisTerminal->gateSize("overlayNeighborArrowOut") == 0)
119  thisTerminal->setGateSize("overlayNeighborArrowOut", 1);
120  else {
121  for (j = 0; j < (thisTerminal->gateSize("overlayNeighborArrowOut") - 1); j++) {
122  if (!(thisTerminal->gate("overlayNeighborArrowOut", j)
123  ->isConnectedOutside()))
124  break;
125  }
126  if (thisTerminal->gate("overlayNeighborArrowOut", j)
127  ->isConnectedOutside()) {
128  thisTerminal->setGateSize("overlayNeighborArrowOut", j + 2);
129  j++;
130  }
131  }
132 
133  thisTerminal->gate("overlayNeighborArrowOut", j)->connectTo(neighborTerminal->gate("overlayNeighborArrowIn", i));
134 
135  thisTerminal->gate("overlayNeighborArrowOut", j)->setDisplayString(displayString);
136 }
137 
139 {
140  if (!ev.isGUI() || !thisTerminal)
141  return;
142 
143  PeerInfo* peerInfo = globalNodeList->getPeerInfo(neighbor);
144  if (peerInfo == NULL) {
145  return;
146  }
147 
148  cModule* neighborTerminal = simulation.getModule(peerInfo->getModuleID());
149  if (neighborTerminal == NULL) {
150  return;
151  }
152 
153  //find gate
154  bool compactOut = false;
155  bool compactIn = false;
156  for (int i = 0; i < thisTerminal->gateSize("overlayNeighborArrowOut"); i++) {
157  // NULL-Gate?
158  if (thisTerminal->gate("overlayNeighborArrowOut", i)
159  ->getNextGate() == NULL) {
160  compactOut = true;
161  continue;
162  }
163 
164  if (thisTerminal->gate("overlayNeighborArrowOut", i)
165  ->getNextGate()->getOwnerModule()->getId() == neighborTerminal->getId()) {
166  thisTerminal->gate("overlayNeighborArrowOut", i)->disconnect();
167  compactOut = true;
168  compactIn = true;
169  }
170  }
171 
172  //compact OUT-array
173  if (compactOut)
175  //compact IN-array
176  if (compactIn)
177  compactGateArray(neighborTerminal, VIS_IN);
178 }
179 
180 void TopologyVis::compactGateArray(cModule* terminal,
181  enum VisDrawDirection dir)
182 {
183  const char* gateName = (dir == VIS_OUT ? "overlayNeighborArrowOut"
184  : "overlayNeighborArrowIn");
185 
186  for (int j = 0; j < terminal->gateSize(gateName) - 1; j++) {
187  if (terminal->gate(gateName, j)->isConnectedOutside())
188  continue;
189 
190  cGate* tempGate = NULL;
191  int k = 1;
192  while ((tempGate == NULL) && ((j + k) != terminal->gateSize(gateName))) {
193  tempGate = (dir == VIS_OUT ?
194  terminal->gate(gateName, j + k)->getNextGate() :
195  terminal->gate(gateName, j + k)->getPreviousGate());
196  k++;
197  }
198 
199  if (tempGate == NULL)
200  break;
201 
202  cDisplayString tempDisplayStr;
203  if (dir == VIS_OUT) {
204  tempDisplayStr = terminal->gate(gateName, j + k - 1)->getDisplayString();
205  terminal->gate(gateName, j + k - 1)->disconnect();
206  terminal->gate(gateName, j)->connectTo(tempGate);
207  terminal->gate(gateName, j)->setDisplayString(tempDisplayStr.str());
208  } else {
209  tempDisplayStr = tempGate->getDisplayString();
210  tempGate->disconnect();
211  tempGate->connectTo(terminal->gate(gateName, j));
212  tempGate->setDisplayString(tempDisplayStr.str());
213  }
214  }
215 
216  int nullGates = 0;
217  for (int j = 0; j < terminal->gateSize(gateName); j++)
218  if (!terminal->gate(gateName, j)->isConnectedOutside())
219  nullGates++;
220 
221  terminal->setGateSize(gateName, terminal->gateSize(gateName) - nullGates);
222 }