1 | // [License]
|
---|
2 | // The Ariba-Underlay Copyright
|
---|
3 | //
|
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
5 | //
|
---|
6 | // Institute of Telematics
|
---|
7 | // UniversitÀt Karlsruhe (TH)
|
---|
8 | // Zirkel 2, 76128 Karlsruhe
|
---|
9 | // Germany
|
---|
10 | //
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are
|
---|
13 | // met:
|
---|
14 | //
|
---|
15 | // 1. Redistributions of source code must retain the above copyright
|
---|
16 | // notice, this list of conditions and the following disclaimer.
|
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | // notice, this list of conditions and the following disclaimer in the
|
---|
19 | // documentation and/or other materials provided with the distribution.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | //
|
---|
33 | // The views and conclusions contained in the software and documentation
|
---|
34 | // are those of the authors and should not be interpreted as representing
|
---|
35 | // official policies, either expressed or implied, of the Institute of
|
---|
36 | // Telematics.
|
---|
37 | // [License]
|
---|
38 |
|
---|
39 | #include "DddVis.h"
|
---|
40 |
|
---|
41 | namespace ariba {
|
---|
42 | namespace utility {
|
---|
43 |
|
---|
44 | const string DddVis::del = ":";
|
---|
45 | use_logging_cpp(DddVis);
|
---|
46 |
|
---|
47 | DddVis::DddVis() : commandid(0) {
|
---|
48 | srand(time(NULL));
|
---|
49 | }
|
---|
50 |
|
---|
51 | DddVis::~DddVis(){
|
---|
52 | }
|
---|
53 |
|
---|
54 | void DddVis::sendMessage( const string msg, NETWORK_ID nid ) {
|
---|
55 | sendSocket( msg + "\n" );
|
---|
56 | }
|
---|
57 |
|
---|
58 | long DddVis::getCommandID() {
|
---|
59 | return ++commandid;
|
---|
60 | }
|
---|
61 |
|
---|
62 | long DddVis::getTimestamp() {
|
---|
63 | return 0; //time(NULL);
|
---|
64 | }
|
---|
65 |
|
---|
66 | unsigned int DddVis::getNodeNumber(const NodeID& node){
|
---|
67 | NodeSet::iterator i = nodeSet.find(node);
|
---|
68 |
|
---|
69 | if(i == nodeSet.end()){
|
---|
70 | unsigned int number = node.get(MAX_KEYLENGTH-16, 16);
|
---|
71 | nodeSet.insert(make_pair(node, number));
|
---|
72 | return number;
|
---|
73 | } else {
|
---|
74 | return i->second;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void DddVis::visCreate(
|
---|
79 | NETWORK_ID network,
|
---|
80 | NodeID& node,
|
---|
81 | string nodename,
|
---|
82 | string info
|
---|
83 | ){
|
---|
84 | //
|
---|
85 | // create layer first if not already done
|
---|
86 | //
|
---|
87 |
|
---|
88 | if(layerSet.find(network) == layerSet.end()){
|
---|
89 |
|
---|
90 | //
|
---|
91 | // create layer
|
---|
92 | //
|
---|
93 |
|
---|
94 | {
|
---|
95 | ostringstream out;
|
---|
96 | out << CREATE_LAYER_TYPE << del
|
---|
97 | << getCommandID() << del
|
---|
98 | << getTimestamp() << del
|
---|
99 | << getNetworkName(network) << del
|
---|
100 | << 0 << del;
|
---|
101 |
|
---|
102 | sendMessage( out.str(), network );
|
---|
103 | layerSet.insert(network);
|
---|
104 | }
|
---|
105 |
|
---|
106 | //
|
---|
107 | // set layer layout
|
---|
108 | //
|
---|
109 |
|
---|
110 | {
|
---|
111 | LayoutType layout = FORCE_LAYOUT;
|
---|
112 | LayoutOrderStrategie order = ORDER_RANDOMLY;
|
---|
113 |
|
---|
114 | switch(network){
|
---|
115 | case NETWORK_ID_BASE_COMMUNICATION:
|
---|
116 | layout = FORCE_LAYOUT;
|
---|
117 | order = ORDER_RANDOMLY;
|
---|
118 | break;
|
---|
119 | case NETWORK_ID_BASE_OVERLAY:
|
---|
120 | layout = CIRCULAR_LAYOUT;
|
---|
121 | order = ORDER_BY_ID;
|
---|
122 | break;
|
---|
123 | case NETWORK_ID_MCPO:
|
---|
124 | layout = FORCE_LAYOUT;
|
---|
125 | order = ORDER_RANDOMLY;
|
---|
126 | break;
|
---|
127 | }
|
---|
128 |
|
---|
129 | ostringstream out;
|
---|
130 | out << SET_CLUSTER_LAYOUT_TYPE << del
|
---|
131 | << getCommandID() << del
|
---|
132 | << getTimestamp() << del
|
---|
133 | << getNetworkName(network) << del
|
---|
134 | << 0 << del
|
---|
135 | << layout << del
|
---|
136 | << order << del;
|
---|
137 |
|
---|
138 | sendMessage( out.str(), network );
|
---|
139 | }
|
---|
140 |
|
---|
141 | } // if(layerSet.find(network) == layerSet.end())
|
---|
142 |
|
---|
143 | //
|
---|
144 | // create node
|
---|
145 | //
|
---|
146 |
|
---|
147 | ostringstream out;
|
---|
148 | out << CREATE_NODE_TYPE << del
|
---|
149 | << getCommandID() << del
|
---|
150 | << getTimestamp() << del
|
---|
151 | << getNetworkName(network) << del
|
---|
152 | << 0 << del
|
---|
153 | << getNodeNumber(node) << del
|
---|
154 | << 0x00000000 << del
|
---|
155 | << "null" << del;
|
---|
156 |
|
---|
157 | sendMessage( out.str(), network );
|
---|
158 | }
|
---|
159 |
|
---|
160 | void DddVis::visConnect(
|
---|
161 | NETWORK_ID network,
|
---|
162 | NodeID& srcnode,
|
---|
163 | NodeID& destnode,
|
---|
164 | string info
|
---|
165 | ){
|
---|
166 | // if we already have a link between the two nodes
|
---|
167 | // we just ignore the call and leave the old link
|
---|
168 |
|
---|
169 | if( networkLinks.exists( network, NodePair(srcnode,destnode) )) return;
|
---|
170 |
|
---|
171 | ostringstream out;
|
---|
172 | unsigned long edgekey = networkLinks.insert( network, NodePair(srcnode,destnode) );
|
---|
173 |
|
---|
174 | out << CREATE_EDGE_TYPE << del
|
---|
175 | << getCommandID() << del
|
---|
176 | << getTimestamp() << del
|
---|
177 | << getNetworkName(network) << del
|
---|
178 | << 0 << del
|
---|
179 | << getNodeNumber(srcnode) << del
|
---|
180 | << getNodeNumber(destnode) << del
|
---|
181 | << edgekey << del
|
---|
182 | << 0x00000000 << del
|
---|
183 | << "null" << del;
|
---|
184 |
|
---|
185 | sendMessage( out.str(), network );
|
---|
186 | }
|
---|
187 |
|
---|
188 | void DddVis::visDisconnect(
|
---|
189 | NETWORK_ID network,
|
---|
190 | NodeID& srcnode,
|
---|
191 | NodeID& destnode,
|
---|
192 | string info
|
---|
193 | ){
|
---|
194 | if( !networkLinks.exists(network, NodePair(srcnode, destnode)) ) return;
|
---|
195 |
|
---|
196 | unsigned long edgekey = networkLinks.get( network, NodePair(srcnode, destnode) );
|
---|
197 | networkLinks.remove( network, NodePair(srcnode, destnode) );
|
---|
198 |
|
---|
199 | ostringstream out;
|
---|
200 | out << REMOVE_EDGE_TYPE << del
|
---|
201 | << getCommandID() << del
|
---|
202 | << getTimestamp() << del
|
---|
203 | << getNetworkName(network) << del
|
---|
204 | << edgekey << del;
|
---|
205 |
|
---|
206 | sendMessage( out.str(), network );
|
---|
207 | }
|
---|
208 |
|
---|
209 | void DddVis::visShutdown(
|
---|
210 | NETWORK_ID network,
|
---|
211 | NodeID& node,
|
---|
212 | string info
|
---|
213 | ){
|
---|
214 | ostringstream out;
|
---|
215 |
|
---|
216 | out << REMOVE_NODE_TYPE << del
|
---|
217 | << getCommandID() << del
|
---|
218 | << getTimestamp() << del
|
---|
219 | << getNetworkName(network) << del
|
---|
220 | << getNodeNumber(node) << del;
|
---|
221 |
|
---|
222 | sendMessage( out.str(), network );
|
---|
223 | }
|
---|
224 |
|
---|
225 | void DddVis::visChangeNodeColor (
|
---|
226 | NETWORK_ID network,
|
---|
227 | NodeID& node,
|
---|
228 | unsigned char r,
|
---|
229 | unsigned char g,
|
---|
230 | unsigned char b
|
---|
231 | ){
|
---|
232 | NodeSet::iterator i = colorSet.find(node);
|
---|
233 | unsigned int color = makeColor(r,g,b);
|
---|
234 |
|
---|
235 | if( i == colorSet.end() ){
|
---|
236 | colorSet.insert(make_pair( node, color )); // color not set for node, set
|
---|
237 | }else{
|
---|
238 | if( i->second == color ) return; // color already set, ignore
|
---|
239 | else i->second = color; // new color, set
|
---|
240 | }
|
---|
241 |
|
---|
242 | ostringstream out;
|
---|
243 | out << SET_NODE_COLOR_TYPE << del
|
---|
244 | << getCommandID() << del
|
---|
245 | << getTimestamp() << del
|
---|
246 | << getNetworkName(network) << del
|
---|
247 | << getNodeNumber(node) << del
|
---|
248 | << makeColor(r, g, b) << del;
|
---|
249 |
|
---|
250 | sendMessage( out.str(), network );
|
---|
251 | }
|
---|
252 |
|
---|
253 | int DddVis::makeColor(unsigned char r, unsigned char g, unsigned char b){
|
---|
254 | return ((r<<16)+(g<<8)+b);
|
---|
255 | }
|
---|
256 |
|
---|
257 | void DddVis::visChangeNodeColor (
|
---|
258 | NETWORK_ID network,
|
---|
259 | NodeID& node,
|
---|
260 | NODE_COLORS color
|
---|
261 | ){
|
---|
262 | unsigned char r = 0;
|
---|
263 | unsigned char g = 0;
|
---|
264 | unsigned char b = 0;
|
---|
265 |
|
---|
266 | switch( color ) {
|
---|
267 | case NODE_COLORS_GREY: r = 128; g = 128; b = 128; break;
|
---|
268 | case NODE_COLORS_GREEN: r = 0; g = 200; b = 0; break;
|
---|
269 | case NODE_COLORS_RED: r = 255; g = 0; b = 0; break;
|
---|
270 | }
|
---|
271 |
|
---|
272 | visChangeNodeColor( network, node, r, g, b );
|
---|
273 | }
|
---|
274 |
|
---|
275 | void DddVis::visChangeLinkColor (
|
---|
276 | NETWORK_ID network,
|
---|
277 | NodeID& srcnode,
|
---|
278 | NodeID& destnode,
|
---|
279 | unsigned char r,
|
---|
280 | unsigned char g,
|
---|
281 | unsigned char b
|
---|
282 | ){
|
---|
283 | ostringstream out;
|
---|
284 | unsigned long edgekey = networkLinks.get( network, NodePair(srcnode, destnode) );
|
---|
285 |
|
---|
286 | out << SET_EDGE_COLOR_TYPE << del
|
---|
287 | << getCommandID() << del
|
---|
288 | << getTimestamp() << del
|
---|
289 | << getNetworkName(network) << del
|
---|
290 | << edgekey << del
|
---|
291 | << makeColor(r, g, b) << del;
|
---|
292 |
|
---|
293 | sendMessage( out.str(), network );
|
---|
294 | }
|
---|
295 |
|
---|
296 | void DddVis::visChangeLinkColor (
|
---|
297 | NETWORK_ID network,
|
---|
298 | NodeID& srcnode,
|
---|
299 | NodeID& destnode,
|
---|
300 | NODE_COLORS color
|
---|
301 | ){
|
---|
302 | unsigned char r = 0;
|
---|
303 | unsigned char g = 0;
|
---|
304 | unsigned char b = 0;
|
---|
305 |
|
---|
306 | switch( color ) {
|
---|
307 | case NODE_COLORS_GREY: r = 128; g = 128; b = 128; break;
|
---|
308 | case NODE_COLORS_GREEN: r = 0; g = 200; b = 0; break;
|
---|
309 | case NODE_COLORS_RED: r = 255; g = 0; b = 0; break;
|
---|
310 | }
|
---|
311 |
|
---|
312 | visChangeLinkColor( network, srcnode, destnode, r, g, b );
|
---|
313 | }
|
---|
314 |
|
---|
315 | }} // namespace ariba, common
|
---|