Debian Codegraph @

protolib/src/manet/manetGraphML2.cpp

raw
1 #include <manetGraphML2.h>
2 #include <protoDebug.h>
3
4
5 ManetGraphMLParser::AttributeType ManetGraphMLParser::GetAttributeType(const char* typeName)
6 {
7 // Types: "boolean", "int", "long", "float", "double", or "string"
8 char text[8]; // all of our type names are less than 8 chars
9 text[7] = '\0';
10 for (i = 0 i < 7; i++)
11 {
12 if ('\0' == typeName[i])
13 {
14 text[i] = '\0';
15 break;
16 }
17 text[i] = tolower(typeName[i]);
18 }
19 switch (text[0])
20 {
21 case 'b':
22 return ((0 == strcmp(text, "boolean")) ? BOOL : INVALID);
23 case 'i':
24 return ((0 == strcmp(text, "int")) ? INT : INVALID);
25 case 'l':
26 return ((0 == strcmp(text, "long")) ? LONG : INVALID);
27 case 'f':
28 return ((0 == strcmp(text, "float")) ? FLOAT : INVALID);
29 case 'd':
30 return ((0 == strcmp(text, "double")) ? DOUBLE : INVALID);
31 case 's':
32 return ((0 == strcmp(text, "string")) ? STRING : INVALID);
33 default:
34 return INVALID;
35 }
36 } // end ManetGraphMLParser::GetAttributeType()
37
38 ManetGraphMLParser::AttributeDomain ManetGraphMLParser::GetAttributeDomain(const char* domainName)
39 {
40 // Domains: "graph", "node", "edge", "all"
41 char text[8]; // all of our domain names are less than 8 chars
42 text[7] = '\0';
43 for (i = 0 i < 7; i++)
44 {
45 if ('\0' == domainName[i])
46 {
47 text[i] = '\0';
48 break;
49 }
50 text[i] = tolower(domainName[i]);
51 }
52 switch (text[0])
53 {
54 case 'g':
55 return ((0 == strcmp(text, "graph")) ? GRAPH : NONE);
56 case 'n':
57 return ((0 == strcmp(text, "node")) ? NODE : NONE);
58 case 'e':
59 return ((0 == strcmp(text, "edge")) ? EDGE : NONE);
60 case 'a':
61 return ((0 == strcmp(text, "all")) ? ALL : NONE);
62 default:
63 return NONE;
64 }
65 } // end end ManetGraphMLParser::GetAttributeDomain()
66
67 ManetGraphMLParser::AttributeKey::AttributeKey()
68 : attr_name(NULL), attr_id(NULL), attr_type(INVALID), attr_domain(NONE), attr_default(NULL)
69 {
70 }
71
72 ManetGraphMLParser::AttributeKey::~AttributeKey()
73 {
74 Destroy();
75 }
76
77 bool ManetGraphMLParser::AttributeKey::Init(const char* name, const char* id, AttributeType type, AttributeDomain domain)
78 {
79 Destroy(); // just in case
80 if ((INVALID == type) || (NONE == domain))
81 {
82 PLOG(PL_ERROR, "ManetGraphMLParser::AttributeKey::Init() invalid type or domain!\n");
83 return false;
84 }
85
86 if (NULL == (attr_name = new char[strlen(name)+1]))
87 {
88 PLOG(PL_ERROR, "ManetGraphMLParser::AttributeKey::Init() new attr_name error: %s\n", GetErrorString());
89 Destroy();
90 return false;
91 }
92 strcpy(attr_name, name);
93 if (NULL != id)
94 {
95 if (NULL == (attr_id = new char[strlen(id)+1]))
96 {
97 PLOG(PL_ERROR, "ManetGraphMLParser::AttributeKey::Init() new attr_id error: %s\n", GetErrorString());
98 Destroy();
99 return false;
100 }
101 strcpy(attr_id, id);
102 }
103 attr_type = type;
104 attr_domain = domain;
105 return true;
106 } // end ManetGraphMLParser::AttributeKey::Init()
107
108 bool ManetGraphMLParser::AttributeKey::Init(const char* name, const char* id, const char* type, const char* domain)
109 {
110 Destroy(); // just in case
111 AttributeType attrType = GetAttributeType(type);
112 if (INVALID == attrType)
113 {
114 PLOG(PL_ERROR, "ManetGraphMLParser::AttributeKey::Init() error: invalid attribute type \"%s\"\n", type);
115 Destroy();
116 return false;
117 }
118 AttributeDomain attrDomain = GetAttributeDomain(domain);
119 if (NONE == attrDomain)
120 {
121 PLOG(PL_ERROR, "ManetGraphMLParser::AttributeKey::Init() error: invalid attribute domain \"%s\"\n", domain);
122 Destroy();
123 return false;
124 }
125 return Init(name, id, attrType, attrDomain);
126 } // end ManetGraphMLParser::AttributeKey::Init()
127
128 void ManetGraphMLParser::AttributeKey::Destroy()
129 {
130 if (NULL != attr_name)
131 {
132 delete[] attr_name;
133 attr_name = NULL;
134 }
135 if (NULL != attr_id)
136 {
137 delete[] attr_id;
138 attr_id = NULL;
139 }
140 attr_type = INVALID;
141 attr_domain = NONE;
142 if (NULL != attr_default)
143 {
144 delete[] attr_default;
145 attr_default = NULL;
146 }
147 } // end ManetGraphMLParser::AttributeKey::Destroy()
148
149 bool ManetGraphMLParser::AttributeKey::SetDefaultValue(const char* value)
150 {
151 if (NULL != attr_default) delete[] attr_default;
152 if (NULL == (attr_default = new char[strlen(value)+1]))
153 {
154 PLOG(PL_ERROR, "ManetGraphMLParser::AttributeKey::SetDefaultValue() new attr_name error: %s\n", GetErrorString());
155 return false;
156 }
157 return true;
158 } // end ManetGraphMLParser::AttributeKey::SetDefaultValue(const char* value)
159
160
161 ManetGraphMLParser::GenericAttribute::GenericAttribute(AttributeKey& key)
162 : attr_key(key), attr_value(NULL)
163 {
164 }
165
166 ManetGraphMLParser::GenericAttribute::~GenericAttribute()
167 {
168 if (NULL != attr_value)
169 {
170 delete[] attr_value;
171 attr_value = NULL;
172 }
173 }
174
175 bool ManetGraphMLParser::GenericAttribute::SetValue(const char* value)
176 {
177 if (NULL != attr_value) delete[] attr_value;
178 if (NULL != value)
179 {
180 if (NULL == (attr_value = new char[strlen(value)+1]))
181 {
182 PLOG(PL_ERROR, "ManetGraphMLParser::Attribute::SetValue() new attr_value error: %s\n", GetErrorString());
183 return false;
184 }
185 }
186 else
187 {
188 attr_value = NULL; // cue to use attribute's default value
189 }
190 } // end ManetGraphMLParser::Attribute::SetValue()
191
192
193 ManetGraphMLParser::ItemAttribute::ItemAttribute(AttributeKey& key)
194 : Attribute(key), item_key(NULL)
195 {
196 }
197
198 ManetGraphMLParser::ItemAttribute::~ItemAttribute()
199 {
200 if (NULL != item_key)
201 {
202 delete[] item_key;
203 item_key = NULL;
204 }
205 }
206
207 bool ManetGraphMLParser::ItemAttribute::SetItem(const char* itemPtr, unsigned int itemPtrLen)
208 {
209 if (NULL != item_key) delete[] item_key;
210 unsigned int keyLen = itemPtrLen + strlen(attr_key.GetName());
211 if (NULL == (item_key = new char[keyLen]))
212 {
213 PLOG(PL_ERROR, "ManetGraphMLParser::ItemAttribute::SetItem() new item_key error: %s\n", GetErrorString());
214 return false;
215 }
216 memcpy(item_key, &itemPtr, itemPtrLen);
217 memcpy(item_key + itemPtrLen, keyLen - itemPtrLen);
218 return true;
219 } // end ManetGraphMLParser::ItemAttribute::SetItem()
220
221 ManetGraphMLParser::NodeAttribute::NodeAttribute(AttributeKey& key)
222 : ItemAttribute(key)
223 {
224 }
225
226 ManetGraphMLParser::NodeAttribute::~NodeAttribute()
227 {
228 }
229
230 ManetGraphMLParser::InterfaceAttribute::InterfaceAttribute(AttributeKey& key)
231 : ItemAttribute(key)
232 {
233 }
234
235 ManetGraphMLParser::InterfaceAttribute::~InterfaceAttribute()
236 {
237 }
238
239 ManetGraphMLParser::LinkAttribute::LinkAttribute(AttributeKey& key)
240 : ItemAttribute(key)
241 {
242 }
243
244 ManetGraphMLParser::LinkAttribute::~LinkAttribute()
245 {
246 }
247
248 ManetGraphMLParser::ManetGraphMLParser()
249 {
250 }
251
252 ManetGraphMLParser::~ManetGraphMLParser()
253 {
254 }
255
256 // Get existing or create new attribute key
257 ManetGraphMLParser::AttributeKey* ManetGraphMLParser::GetAttributeKey(const char* name, AttributeType type, AttributeDomain domain)
258 {
259 AttributeKey* key = attr_name_list.FindString(name);
260 if (NULL == key)
261 {
262 // We need to create a new key
263 if (NULL == (key = new AttributeKey()))
264 {
265 PLOG(PL_ERROR, "ManetGraphMLParser::GetAttributeKey() new AttributeKey error: %s\n", GetErrorString());
266 return NULL;
267 }
268 // TBD - use succinct "id" for attributes (for now id == name)
269 if (!key->Init(name, NULL, type, GRAPH))
270 {
271 PLOG(PL_ERROR, "ManetGraphMLParser::GetAttributeKey() AttributeKey initialization error\n");
272 return NULL;
273 }
274 if (!attr_name_list.Insert(*key))
275 {
276 PLOG(PL_ERROR, "ManetGraphMLParser::GetAttributeKey() error: unable to add to attr_name_list\n");
277 return NULL;
278 }
279 if (!attr_id_list.Insert(*key))
280 {
281 PLOG(PL_ERROR, "ManetGraphMLParser::GetAttributeKey() error: unable to add to attr_id_list\n");
282 return NULL;
283 }
284 }
285 else
286 {
287 // Validate attribute type and domain
288 if (key->GetType() != type)
289 {
290 PLOG(PL_ERROR, "ManetGraphMLParser::GetAttributeKey() error: non-matching attribute type!\n");
291 return NULL;
292 }
293 if (key->GetDomain() != domain)
294 {
295 PLOG(PL_ERROR, "ManetGraphMLParser::GetAttributeKey() error: non-matching attribute domain!\n");
296 return NULL;
297 }
298 }
299 return key;
300 } // end ManetGraphMLParser::GetAttributeKey()
301
302 Attribute* ManetGraphMLParser::FindItemAttribute(const char* itemPtr,
303 unsigned int itemPtrLen,
304 ProtoTree& attrList,
305 AttributeKey& attrKey)
306 {
307 unsigned int idLen = strlen(attrKey->GetId());
308 unsigned int keyLen = itemPtrLen + idLen;
309 char* itemKey = new char[keyLen];
310 if (NULL == itemKey)
311 {
312 PLOG(PL_ERROR, "ManetGraphMLParser::SetNodeAttribute() new itemKey error: %s\n", GetErrorString());
313 return false;
314 }
315 memcpy(itemKey, &itemPtr, itemPtrLen);
316 memcpy(itemKey + itemPtrLen, idLen);
317 ItemAttribute* attr = static_cast<Attribute*>(attrList.Find(itemKey, keyLen << 3));
318 delete[] itemKey;
319 return attr;
320 } // end ManetGraphMLParser::FindItemAttribute()
321
322 bool ManetGraphMLParser::SetGraphAttribute(const char* name, AttributeType type, const char* value)
323 {
324 // Does the attribute key already exist? (one is created if not)
325 AttributeKey* key = GetAttributeKey(name, type, GRAPH);
326 if (NULL == key)
327 {
328 PLOG(PL_ERROR, "ManetGraphMLParser::SetGraphAttribute() error: GetAttributeKey() failure!\n");
329 return false;
330 }
331 // Does the graph already have this attribute
332 Attribute* attr = graph_attr_list.FindString(name);
333 if (NULL == attr)
334 {
335 if (NULL == (attr = new Attribute(*key)))
336 {
337 PLOG(PL_ERROR, "ManetGraphMLParser::SetGraphAttribute() new Attribute error: %s\n", GetErrorString());
338 return false;
339 }
340 graph_attr_list.Insert(*attr); // ProtoTree insert always succeeds
341 }
342 if (!attr->SetValue(value))
343 {
344 PLOG(PL_ERROR, "ManetGraphMLParser::SetGraphAttribute() error: non-matching attribute type!\n");
345 return false;
346 }
347 return true;
348 } // end ManetGraphMLParser::SetGraphAttribute()
349
350 const Attribute* ManetGraphMLParser::GetGraphAttribute(const char* name)
351 {
352 AttributeKey* attrKey = attr_name_list.FindString(name);
353 return (NULL != attrKey) ? graph_attr_list.FindString(attrKey->GetId()) : NULL;
354 } // end ManetGraphMLParser::GetGraphAttribute()
355
356 bool ManetGraphMLParser::SetNodeAttribute(NetGraph::Node& node,
357 const char* name,
358 AttributeType type,
359 const char* value)
360 {
361 // Does the attribute key already exist? (one is created if not)
362 AttributeKey* attrKey = GetAttributeKey(name, type, NODE);
363 if (NULL == attrKey)
364 {
365 PLOG(PL_ERROR, "ManetGraphMLParser::SetNodeAttribute() error: GetAttributeKey() failure!\n");
366 return false;
367 }
368 // Does the item already have this attribute?
369 NetGraph::Node* nodePtr = &node;
370 NodeAttribute* attr = static_cast<NodeAttribute*>(FindItemAttribute(&nodePtr, sizeof(NetGraph::Node), node_attr_list, *attrKey));
371 if (NULL == attr)
372 {
373 // Need to create a new node attribute
374 if (NULL == (attr = new NodeAttribute(*key)))
375 {
376 PLOG(PL_ERROR, "ManetGraphMLParser::SetNodeAttribute() new Attribute error: %s\n", GetErrorString());
377 return false;
378 }
379 if (!attr->SetNode(node))
380 {
381 PLOG(PL_ERROR, "ManetGraphMLParser::SetNodeAttribute() error: set node failure\n");
382 return false;
383 }
384 node_attr_list.Insert(*attr); // ProtoTree insert always succeeds
385 }
386 if (!attr->SetValue(value))
387 {
388 PLOG(PL_ERROR, "ManetGraphMLParser::SetNodeAttribute() error: set value failure!\n");
389 return false;
390 }
391 return true;
392 } // end ManetGraphMLParser::SetNodeAttribute()
393
394 const Attribute* ManetGraphMLParser::GetNodeAttribute(const NetGraph::Node& node, const char* name)
395 {
396 AttributeKey* attrKey = attr_name_list.FindString(name);
397 const char* nodePtr = *node;
398 return (NULL != attrKey) ? FindItemAttribute(node_attr_list, &nodePtr, sizeof(NetGraph::Node*), *attrKey): NULL;
399 } // end ManetGraphMLParser::GetNodeAttribute()
400
401 bool ManetGraphMLParser::SetInterfaceAttribute(NetGraph::Interface& iface,
402 const char* name,
403 AttributeType type,
404 const char* value)
405 {
406 // Does the attribute key already exist? (one is created if not)
407 AttributeKey* attrKey = GetAttributeKey(name, type, NODE);
408 if (NULL == attrKey)
409 {
410 PLOG(PL_ERROR, "ManetGraphMLParser::SetInterfaceAttribute() error: GetAttributeKey() failure!\n");
411 return false;
412 }
413 // Does the item already have this attribute?
414 NetGraph::Interface* ifacePtr = &iface;
415 InterfaceAttribute* attr = static_cast<InterfaceAttribute*>(FindItemAttribute(&ifacePtr, sizeof(NetGraph::Interface), iface_attr_list, *attrKey));
416 if (NULL == attr)
417 {
418 // Need to create a new iface attribute
419 if (NULL == (attr = new InterfaceAttribute(*key)))
420 {
421 PLOG(PL_ERROR, "ManetGraphMLParser::SetInterfaceAttribute() new Attribute error: %s\n", GetErrorString());
422 return false;
423 }
424 if (!attr->SetInterface(iface))
425 {
426 PLOG(PL_ERROR, "ManetGraphMLParser::SetInterfaceAttribute() error: set iface failure\n");
427 return false;
428 }
429 iface_attr_list.Insert(*attr); // ProtoTree insert always succeeds
430 }
431 if (!attr->SetValue(value))
432 {
433 PLOG(PL_ERROR, "ManetGraphMLParser::SetInterfaceAttribute() error: set value failure!\n");
434 return false;
435 }
436 return true;
437 } // end ManetGraphMLParser::SetInterfaceAttribute()
438
439 const Attribute* ManetGraphMLParser::GetInterfaceAttribute(const NetGraph::Interface& iface, const char* name)
440 {
441 AttributeKey* attrKey = attr_name_list.FindString(name);
442 const char* nodePtr = *node;
443 return (NULL != attrKey) ? FindItemAttribute(node_attr_list, &nodePtr, sizeof(NetGraph::Node*), *attrKey): NULL;
444 } // end ManetGraphMLParser::GetInterfaceAttribute()
445
446 bool ManetGraphMLParser::SetLinkAttribute(NetGraph::Link& link,
447 const char* name,
448 AttributeType type,
449 const char* value)
450 {
451 // Does the attribute key already exist? (one is created if not)
452 AttributeKey* attrKey = GetAttributeKey(name, type, domain);
453 if (NULL == attrKey)
454 {
455 PLOG(PL_ERROR, "ManetGraphMLParser::SetLinkAttribute() error: GetAttributeKey() failure!\n");
456 return false;
457 }
458 // Does the item already have this attribute?
459 NetGraph::Link* linkPtr = &link;
460 LinkAttribute* attr = static_cast<LinkAttribute*>(FindItemAttribute(&linkPtr, sizeof(NetGraph::Link), link_attr_list, *attrKey));
461 if (NULL == attr)
462 {
463 // Need to create a new link attribute
464 if (NULL == (attr = new LinkAttribute(*key)))
465 {
466 PLOG(PL_ERROR, "ManetGraphMLParser::SetLinkAttribute() new Attribute error: %s\n", GetErrorString());
467 return false;
468 }
469 if (!attr->SetLink(link))
470 {
471 PLOG(PL_ERROR, "ManetGraphMLParser::SetLinkAttribute() error: set link failure\n");
472 return false;
473 }
474 link_attr_list.Insert(*attr); // ProtoTree insert always succeeds
475 }
476 if (!attr->SetValue(value))
477 {
478 PLOG(PL_ERROR, "ManetGraphMLParser::SetLinkAttribute() error: set value failure!\n");
479 return false;
480 }
481 return true;
482 } // end ManetGraphMLParser::SetLinkAttribute()
483
484 const Attribute* ManetGraphMLParser::GetLinkAttribute(const NetGraph::Link& link, const char* name)
485 {
486 AttributeKey* attrKey = attr_name_list.FindString(name);
487 const char* linkPtr = *link;
488 return (NULL != attrKey) ? FindItemAttribute(node_attr_list, &linkPtr, sizeof(NetGraph::Link*), *attrKey): NULL;
489 } // end ManetGraphMLParser::GetLinkAttribute()
490
491
492 ///////////////// OLD STUFF //////////////////////////
493
494
495
496
497 bool ManetGraphMLParser::SetGraphId(const char* graphId)
498 {
499 if (NULL != graph_id) delete[] graph_id;
500 if (NULL != graphId)
501 {
502 if (NULL == (graph_id = new char[strlen(graphId) + 1]))
503 {
504 PLOG(PL_ERROR, "ManetGraphMLParser::SetGraphId() new graph_id error: %s\n", GetErrorString());
505 return false;
506 }
507 strcpy(graph_id, graphId);
508 }
509 else
510 {
511 graph_id = NULL;
512 }
513 return true;
514 } // end ManetGraphMLParser::SetGraphId()
515
516
517 bool ManetGraphMLParser::Read(const char* path, NetGraph& graph)
518 {
519 // Iteratively read the file's XML tree and build up "graph"
520 xmlTextReader* reader = xmlReaderForFile(path, NULL, 0);
521 if (NULL == reader)
522 {
523 PLOG(PL_ERROR,"ManetGraphMLParser::Read() error: xmlReaderForFile(%s) failure\n", path);
524 return false;
525 }
526
527 bool isDuplex = true;
528 char parentXMLNodeID[MAXXMLIDLENGTH+1];
529 memset(parentXMLNodeID, 0, MAXXMLIDLENGTH+1);
530
531
532 ProtoXml::IterParser iterParser;
533
534 int result;
535 do
536 {
537 result = xmlTextReaderRead(reader);
538
539 } while ( 1== result)
540
541
542 int result = xmlTextReaderRead(readerPtr);
543 while (1 == result)
544 {
545 if (!ReadXMLNode(readerPtr, graph, parentXMLNodeID, isDuplex))
546 {
547 PLOG(PL_ERROR,"ManetGraphMLParser::Read() error: invalid XML node!\n");
548 break;
549 }
550 result = xmlTextReaderRead(readerPtr);
551 }
552 xmlFreeTextReader(readerPtr);
553 if (0 != result)
554 PLOG(PL_ERROR,"ManetGraphMLParser::Read() error: invalid XML file %s\n", path);
555 xmlCleanupParser();
556 return (0 == result);
557 } // end ManetGraphMLParser::Read()
558
559 bool ManetGraphMLParser::ReadXMLNode(xmlTextReader* readerPtr,
560 NetGraph& graph,
561 char* parentXMLNodeID,
562 bool& isDuplex)
563 {
564 const xmlChar *name, *value;
565 int count, depth, type, isempty;
566
567 type = xmlTextReaderNodeType(readerPtr);
568 if(XML_READER_TYPE_END_ELEMENT==type)
569 return true;
570
571 name = xmlTextReaderConstName(readerPtr);
572 if (name == NULL)
573 name = BAD_CAST "--";
574 value = xmlTextReaderConstValue(readerPtr);
575 depth = xmlTextReaderDepth(readerPtr);
576 isempty = xmlTextReaderIsEmptyElement(readerPtr);
577 count = xmlTextReaderAttributeCount(readerPtr);
578 //printf("processsing depth=%d, type=%d, name=%s, isempty=%d, value=%s, attributes=%d\n",depth,type,name,isempty,value,count);
579 if(!strcmp("graph",(const char*)name))
580 {
581 //printf("processing graph node\n");
582 xmlChar* graphId;
583 graphId = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"id");
584 if(NULL == graphId)
585 {
586 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error missing graph id attribute!\n");
587 return false;
588 }
589 SetXMLName((const char*)graphId);
590 xmlChar* graphEdgeType;
591 graphEdgeType = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"edgedefault");
592 if(!strcmp("directed",(const char*)graphEdgeType))
593 {
594 isDuplex = false;
595 }
596 else
597 {
598 isDuplex = true;
599 }
600 //printf("exiting graph node id = \"%s\" is duplex is %s\n",graphId,isDuplex ? "true" : "false");
601 }
602 else if(!strcmp("node",(const char*)name))
603 {
604 xmlChar* nodeId = NULL;
605 while (xmlTextReaderMoveToNextAttribute(readerPtr)>0)
606 {
607
608 //printf("found attribute %s with name %s\n",xmlTextReaderName(readerPtr),xmlTextReaderConstValue(readerPtr));
609 if(!strcmp((const char*)xmlTextReaderConstName(readerPtr),"id"))
610 {
611 nodeId = xmlTextReaderValue(readerPtr);
612 }
613 }
614 if(NULL == nodeId)
615 {
616 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error missing node id attribute!\n");
617 return false;
618 }
619 if(strlen((const char*)nodeId) > MAXXMLIDLENGTH)
620 {
621 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error the node id value of \"%s\" is too large\n",nodeId);
622 return false;
623 }
624 strcpy(parentXMLNodeID,(const char*)nodeId);
625
626 NetGraph::Interface* interface;
627 ProtoAddress addr;
628 addr.ResolveFromString((const char*)nodeId);
629 if(addr.IsValid())
630 {
631 interface = graph.FindInterface(addr);
632 }
633 else
634 {
635 interface = graph.FindInterfaceByName((const char*)nodeId);
636 }
637 if (NULL == interface)
638 {
639 //Create new node and associated interface
640 NetGraph::Node* node = CreateNode();
641
642 if(NULL == node)
643 {
644 PLOG(PL_ERROR, "NetGraph::ProcessXMLNode: error Creating new node!\n");
645 return -1;
646 }
647 if(addr.IsValid())
648 {
649 interface = CreateInterface(*node,addr);
650 }
651 else
652 {
653 interface = CreateInterface(*node);
654 interface->SetName((const char*)nodeId);
655 }
656 //node->AddInterface(*interface,true);
657 AddInterfaceToNode(*node,*interface,true);
658 //graph.InsertInterface(*interface);
659 AddInterfaceToGraph(graph,*interface);
660 }
661 else
662 {
663 //existing node so update anything we might have on it
664 }
665 //printf("exiting node node\n");
666 }
667 else if(!strcmp("port",(const char*)name))
668 {
669 //printf("processing port node\n");
670 xmlChar* portname;
671 portname = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"name");
672 NetGraph::Interface* portinterface;
673 portinterface = graph.FindInterfaceByString((const char*)portname);
674
675 ProtoAddress portAddr;
676 portAddr.ResolveFromString((const char*)portname);
677 if (NULL == portinterface)
678 {
679 //Find the parent node and associated with this interface
680 NetGraph::Interface* interface;
681 interface = graph.FindInterfaceByString(parentXMLNodeID);
682 ProtoAddress addr;
683 addr.ResolveFromString(parentXMLNodeID);
684 if(NULL == interface)
685 {
686 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error finding the parent \"%s\" interface in the graph for port \"%s\"!\n",parentXMLNodeID,portname);
687 return false;
688 }
689
690 NetGraph::Node& node = interface->GetNode();
691 //NetGraph::Node *node = &interface->GetNode();
692 if(portAddr.IsValid())
693 {
694 portinterface = new NetGraph::Interface(node,portAddr);
695 }
696 else
697 {
698 portinterface = CreateInterface(node);
699 portinterface->SetName((const char*)portname);
700 }
701 //node->SetPosition(x, y);
702 //node.AddInterface(*portinterface,false);
703 AddInterfaceToNode(node,*portinterface,false);
704 //graph.InsertInterface(*portinterface);
705 AddInterfaceToGraph(graph,*interface);
706 //printf(" just added port \"%s\" to node \"%s\"\n",portname,parentXMLNodeID);
707 }
708 else
709 {
710 //portname exists already so we just need to update any info associated with it
711 }
712 //printf("exiting port node\n");
713 /* PLOG(PL_ERROR,"nodeid=%s, depth=%d, type=%d, name=%s, isEmpty=%d, hasValue=%d",
714 parentXMLNodeID,
715 xmlTextReaderDepth(readerPtr),
716 xmlTextReaderNodeType(readerPtr),
717 name,
718 xmlTextReaderIsEmptyElement(readerPtr),
719 xmlTextReaderHasValue(readerPtr));
720 //printf("is a port\n");*/
721 }
722 else if(!strcmp("edge",(const char*)name))
723 {
724 //printf("processing edge node\n");
725 //check to see if this edge is a port type first
726 xmlChar *targetPortName, *sourcePortName;
727 targetPortName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"targetport");
728 sourcePortName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"sourceport");
729 if((NULL != targetPortName) && (NULL != sourcePortName))
730 {
731 //find the target port interface
732 NetGraph::Interface* targetPortInterface;
733 NetGraph::Interface* sourcePortInterface;
734 targetPortInterface = graph.FindInterfaceByString((const char*)targetPortName);
735 sourcePortInterface = graph.FindInterfaceByString((const char*)sourcePortName);
736 if((NULL == sourcePortInterface) || (NULL == targetPortInterface))
737 {
738 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error finding the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourcePortName,targetPortName);
739 return false;
740 }
741 NetGraph::Cost* mycost = CreateCost(1.0);
742 if(!Connect(*sourcePortInterface,*targetPortInterface,*mycost,isDuplex))
743 {
744 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error connecting the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourcePortName,targetPortName);
745 return false;
746 }
747 //printf(" connected interface ports %s to %s\n",sourcePortName,targetPortName);
748 }
749 else //isn't a port type just a normal source target type
750 {
751 xmlChar *targetName, *sourceName;
752 targetName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"target");
753 sourceName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"source");
754 //find the target interface
755 NetGraph::Interface* targetInterface;
756 NetGraph::Interface* sourceInterface;
757 sourceInterface = graph.FindInterfaceByString((const char*)sourceName);
758 targetInterface = graph.FindInterfaceByString((const char*)targetName);
759 if((NULL == sourceInterface) || (NULL == targetInterface))
760 {
761 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error finding the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourceName,targetName);
762 return false;
763 }
764 NetGraph::Cost* mycost = CreateCost(1.0);
765 if(!Connect(*sourceInterface,*targetInterface,*mycost,isDuplex))
766 {
767 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error connecting the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourceName,targetName);
768 return false;
769 }
770 //printf(" connected interface %s to %s\n",sourceName, targetName);
771 }
772 //printf("exiting edge node\n");
773 }
774 else if(!strcmp("key",(const char*)name))
775 {
776 xmlChar* oldkey = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"id");
777 xmlChar* type = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"attr.type");
778 xmlChar* name = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"attr.name");
779 xmlChar* domain = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"for");
780
781 xmlChar* defaultvalue = NULL;
782 xmlNode* myXMLNode = xmlTextReaderExpand(readerPtr);
783 xmlNode* childXMLNode = myXMLNode->children;
784 while(NULL != childXMLNode)
785 {
786 if(!strcmp((char*)childXMLNode->name,"default"))
787 {
788 if(NULL == childXMLNode->children)
789 {
790 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): %s has an empty default value!\n",name);
791 return false;
792 }
793 defaultvalue = childXMLNode->children->content;
794 }
795 childXMLNode = childXMLNode->next;
796 }
797 //printf("reading in attribute name=%s key=%s defaultvalue=%s\n",name,oldkey,defaultvalue);
798 if(!AddAttributeKey((const char*)name,(const char*)type,(const char*)domain,(const char*)oldkey,(const char*)defaultvalue))
799 {
800 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error adding attribute (name=%s,oldkey=%s)\n",name,oldkey);
801 return false;
802 }
803 }
804 else if(!strcmp("data",(const char*)name))
805 {
806 char newlookup[250];//this really should be dynamic and checks on size should take place TBD
807 xmlChar* oldIndex;
808 const char* newIndex;
809 xmlChar* newValue;
810 xmlNode* myxmlnode = xmlTextReaderExpand(readerPtr);
811
812 if(!strcmp((const char*)myxmlnode->parent->name,"node"))
813 {
814 sprintf(newlookup,"node:%s",parentXMLNodeID);
815 } else if(!strcmp((const char*)myxmlnode->parent->name,"port")) {
816 xmlAttr* tempattribute = myxmlnode->parent->properties;
817 const xmlChar *portName = NULL;
818 while(NULL != tempattribute)
819 {
820 if(!strcmp((const char*)tempattribute->name,"name"))
821 portName = tempattribute->children->content;
822 tempattribute= tempattribute->next;
823 }
824 sprintf(newlookup,"node:%s:port:%s",parentXMLNodeID,portName);
825
826 } else if(!strcmp((const char*)myxmlnode->parent->name,"edge")) {
827 xmlAttr* tempattribute = myxmlnode->parent->properties;
828 const xmlChar *targetPortName(NULL), *sourcePortName(NULL), *targetName(NULL), *sourceName(NULL);
829 while(NULL != tempattribute)
830 {
831 if(!strcmp((const char*)tempattribute->name,"sourceport"))
832 sourcePortName = tempattribute->children->content;
833 if(!strcmp((const char*)tempattribute->name,"targetport"))
834 targetPortName = tempattribute->children->content;
835 if(!strcmp((const char*)tempattribute->name,"source"))
836 sourceName = tempattribute->children->content;
837 if(!strcmp((const char*)tempattribute->name,"target"))
838 targetName = tempattribute->children->content;
839 tempattribute= tempattribute->next;
840 }
841 if((NULL == targetName) || (NULL == sourceName))
842 {
843 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error finding target/source for link data\n");
844 return false;
845 }
846 sprintf(newlookup,"edge:source:%s:%s:dest:%s:%s",sourceName,sourcePortName,targetName,targetPortName);
847 } else {
848 PLOG(PL_WARN,"ManetGraphMLParser::ReadXMLNode(): Ignoring data for unknown node type\n");
849 }
850 oldIndex = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"key");
851 AttributeKey* tempakey = FindAttributeKeyByOldIndex((const char*)oldIndex);
852 if(NULL == tempakey)
853 {
854 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): found data with key type %s but it wasn't listed at one of the keys\n",oldIndex);
855 return false;
856 } else {
857 newIndex = tempakey->GetIndex();
858 //printf("%s\n",newIndex);
859 }
860 if(!xmlTextReaderRead(readerPtr))
861 return false;
862 newValue = xmlTextReaderValue(readerPtr);
863 //printf("found data for \"%s\" with key %s with value %s\n",newlookup,oldIndex,newValue);
864
865 Attribute* newAttribute = new Attribute();
866 newAttribute->Init(newlookup,(const char*)newIndex,(const char*)newValue);
867 //newAttribute->Init(newlookup,newIndex,(const char*)newValue);
868 attributelist.Insert(*newAttribute);
869 }
870 else
871 {
872 //ignorning xml node
873 //printf("ignoring xml node %s\n",name);
874 }
875 return true;
876
877 } // end ManetGraphMLParser::ReadXMLNode()
878
879
880
881 bool ManetGraphMLParser::Write(NetGraph& graph, const char* path)
882 {
883 PLOG(PL_INFO,"ManetGraphMLParser::Write: Enter!\n");
884 /* Create a new XmlWriter for DOM, with no compression. */
885 int returnvalue;
886 xmlTextWriter* writerPtr;
887 xmlDoc* docPtr;
888
889 writerPtr = xmlNewTextWriterDoc(&docPtr, 0);
890 if (writerPtr == NULL) {
891 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlwriterPtrDoc: Error creating the xml writer\n");
892 return false;
893 }
894
895 /* Start the docPtrument with the xml default for the version,
896 * encoding UTF-8 and the default for the standalone
897 * declarao*/
898 returnvalue = xmlTextWriterStartDocument(writerPtr, NULL, MY_GRAPHML_ENCODING, NULL);
899 if (returnvalue < 0) {
900 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlwriterPtrDoc: Error at xmlTextWriterStartDocument\n");
901 return false;;
902 }
903
904 /* Start an element named "graphml". Since thist is the first
905 * element, this will be the root element of the docPtrument. */
906 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "graphml");
907 if (returnvalue < 0) {
908 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlwriterPtrDoc: Error at xmlTextWriterStartElement\n");
909 return false;
910 }
911 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "xmlns", BAD_CAST "http://graphml.graphdrawing.org/xmlns");
912 if (returnvalue < 0) {
913 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error at xmlTextWriterWriteAttribute\n");
914 return false;
915 }
916 if(!UpdateKeys(graph))
917 {
918 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error updating key elements in the header\n");
919 return false;
920 }
921 if(!WriteLocalKeys(writerPtr))
922 {
923 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error at writing key elements in header\n");
924 return false;
925 }
926
927 /* We are done with the header so now we go through the actual graph and add each node and edge */
928 /* We are adding each node */
929 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "graph");
930 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error starting XML graph element\n"); return false;}
931 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id",BAD_CAST XMLName);
932 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error setting XML graph attribute id\n"); return false;}
933 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "edgedefault",BAD_CAST "directed");
934 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error setting graph attribute directed\n"); return false;}
935
936 NetGraph::InterfaceIterator it(graph);
937 NetGraph::Interface* iface;
938 //adding the nodes to the xml
939 while (NULL != (iface = it.GetNextInterface()))
940 {
941 //check to see if this is a default "node" interface
942 //if(!iface->IsPort())
943 if(iface == iface->GetNode().GetDefaultInterface())
944 {
945 //Node& node = static_cast<Node&>(iface->GetNode());
946 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "node");
947 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding XML node\n"); return false;}
948 if(iface->GetAddress().IsValid())
949 {
950 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id", BAD_CAST iface->GetAddress().GetHostString());
951 //printf("writing node %s\n",iface->GetAddress().GetHostString());
952 } else {
953 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id", BAD_CAST iface->GetName());
954 //printf("writing node %s\n",iface->GetName());
955 }
956 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding setting node id\n"); return false;}
957
958 //update the node attributes using the virtual function
959 if(!UpdateNodeAttributes(iface->GetNode()))
960 {
961 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the node attributes\n");
962 return false;
963 }
964 //call the local function write the attributes out
965 if(!WriteLocalNodeAttributes(writerPtr,iface->GetNode()))
966 {
967 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing local node attributes\n");
968 return false;
969 }
970 //call the virtual function to allow the node to fill in any attributes if there
971 /* if(!WriteNodeAttributes(writerPtr,iface->GetNode())) //this shouldn't really have to be called if we add/update the attributes correctly TBD
972 {
973 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing node attributes\n");
974 return false;
975 }*/
976
977 //we need to iterate through the any "port" interfaces which we may have
978 NetGraph::Node *theNode = &iface->GetNode();
979 NetGraph::Node::InterfaceIterator nodeIt(*theNode);
980 NetGraph::Interface* portIface;
981 while (NULL != (portIface = static_cast<NetGraph::Interface*>(nodeIt.GetNextInterface())))
982 {
983 //we only want ports
984 if(!(portIface == portIface->GetNode().GetDefaultInterface()))
985 // if(portIface->IsPort())
986 {
987 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "port");
988 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding XML node\n"); return false;}
989 if(portIface->GetAddress().IsValid())
990 {
991 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "name", BAD_CAST portIface->GetAddress().GetHostString());
992 //printf("writing interface %s\n",iface->GetAddress().GetHostString());
993 } else {
994 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "name", BAD_CAST portIface->GetName());
995 //printf("writing node %s\n",iface->GetName());
996 }
997 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding setting node id\n"); return false;}
998 //update attributes to the port/interface using the virutal function
999 if(!UpdateInterfaceAttributes(*portIface))
1000 {
1001 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the interface attributes\n");
1002 return false;
1003 }
1004 //write the attributes to the port/interface
1005 if(!WriteLocalInterfaceAttributes(writerPtr,*portIface))
1006 {
1007 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing local interface attributes\n");
1008 return false;
1009 }
1010 //write any attributes to the port/interface using the virutal function
1011 /* if(!WriteInterfaceAttributes(writerPtr,*portIface))//this needn't be called if we add/update the attributes in the local list TBD
1012 {
1013 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing interface attributes\n");
1014 return false;
1015 }*/
1016 returnvalue = xmlTextWriterEndElement(writerPtr);
1017 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error ending node element\n"); return false;}
1018 }
1019 }
1020 //close up the node node element
1021 returnvalue = xmlTextWriterEndElement(writerPtr);
1022 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error ending node element\n"); return false;}
1023 }
1024 }
1025 //adding the directional links to the xml
1026 it.Reset();
1027 while (NULL != (iface = it.GetNextInterface()))
1028 {
1029 //if(!iface->IsPort())//not a port node so we can just print it normally
1030 if(iface == iface->GetNode().GetDefaultInterface())
1031 {
1032 NetGraph::AdjacencyIterator iteratorN1(*iface);
1033 NetGraph::Interface* nbrIface;
1034 while (NULL != (nbrIface = iteratorN1.GetNextAdjacency()))
1035 {
1036 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "edge");
1037 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding edge\n"); return false;}
1038 if(iface->GetAddress().IsValid()) {
1039 //printf("writing connection %s ->",iface->GetAddress().GetHostString());
1040 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST iface->GetAddress().GetHostString());
1041 } else {
1042 //printf("writing connection %s ->",iface->GetName());
1043 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST iface->GetName());
1044 }
1045 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n"); return false;}
1046 if(nbrIface->GetAddress().IsValid()) {
1047 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrIface->GetAddress().GetHostString());
1048 ////printf("%s\n",nbrIface->GetAddress().GetHostString());
1049 } else {
1050 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrIface->GetName());
1051 ////printf("%s\n",nbrIface->GetName());
1052 }
1053 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n"); return false;}
1054 NetGraph::Link* link = iface->GetLinkTo(*nbrIface);
1055 if(NULL == link)
1056 {
1057 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error getting link which should always exist\n");
1058 return false;
1059 }
1060 //update the link attributes using the virtual function
1061 if(!UpdateLinkAttributes(*link))
1062 {
1063 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the link attributes\n");
1064 return false;
1065 }
1066 //actually write the attributes out
1067 if(!WriteLocalLinkAttributes(writerPtr,*link))
1068 {
1069 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing link attributes\n");
1070 return false;
1071 }
1072 returnvalue = xmlTextWriterEndElement(writerPtr);
1073 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error ending node element\n"); return false;}
1074 }
1075 }
1076 else //it is a port interface so we need to find the "node" interface
1077 {
1078 //lets first find the "src" node interface
1079 NetGraph::Interface* nodeIface = iface->GetNode().GetDefaultInterface();
1080 if(!(nodeIface == nodeIface->GetNode().GetDefaultInterface()))
1081 // if(nodeIface->IsPort())
1082 {
1083 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error the default interface was not the \"node\" interface for the src. You should iterate over them all and find the right one\n");
1084 return false;
1085 }
1086
1087 NetGraph::AdjacencyIterator iteratorN1(*iface);
1088 NetGraph::Interface* nbrIface, *nbrNodeIface;
1089 while (NULL != (nbrIface = iteratorN1.GetNextAdjacency()))
1090 {
1091 //lets find the "nbr" node interface
1092 nbrNodeIface = static_cast<NetGraph::Interface*>(nbrIface->GetNode().GetDefaultInterface());
1093 //if(nbrNodeIface->IsPort())
1094 if(!(nbrNodeIface == nbrNodeIface->GetNode().GetDefaultInterface()))
1095 {
1096 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error the default interface was not the \"node\" interface for the nbr. You should iterate over them all and find the right one\n");
1097 return false;
1098 }
1099 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "edge");
1100 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding edge\n"); return false;}
1101
1102 if(nodeIface->GetAddress().IsValid()) {
1103 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST nodeIface->GetAddress().GetHostString());
1104 } else {
1105 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST nodeIface->GetName());
1106 }
1107 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n"); return false;}
1108 if(nbrNodeIface->GetAddress().IsValid()) {
1109 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrNodeIface->GetAddress().GetHostString());
1110 } else {
1111 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrNodeIface->GetName());
1112 }
1113 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n"); return false;}
1114 if(iface->GetAddress().IsValid()) {
1115 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "sourceport", BAD_CAST iface->GetAddress().GetHostString());
1116 } else {
1117 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "sourceport", BAD_CAST iface->GetName());
1118 }
1119 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n"); return false;}
1120 if(nbrIface->GetAddress().IsValid()) {
1121 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "targetport", BAD_CAST nbrIface->GetAddress().GetHostString());
1122 } else {
1123 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "targetport", BAD_CAST nbrIface->GetName());
1124 }
1125 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n"); return false;}
1126
1127 NetGraph::Link* link = iface->GetLinkTo(*nbrIface);
1128 if(NULL == link)
1129 {
1130 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error getting link which should always exist\n");
1131 return false;
1132 }
1133 //update the link attributes using the virtual function
1134 if(!UpdateLinkAttributes(*link))
1135 {
1136 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the link attributes\n");
1137 return false;
1138 }
1139 //actually write the attributes out
1140 if(!WriteLocalLinkAttributes(writerPtr,*link))
1141 {
1142 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing link attributes\n");
1143 return false;
1144 }
1145 returnvalue = xmlTextWriterEndElement(writerPtr);
1146 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write Error ending node element\n"); return false;}
1147 }
1148 }
1149 }
1150
1151 returnvalue = xmlTextWriterEndDocument(writerPtr);
1152 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write:testXmlwriterPtrDoc: Error at xmlTextWriterEndDocument\n"); return false;}
1153
1154 xmlFreeTextWriter(writerPtr);
1155
1156 xmlSaveFormatFileEnc(path, docPtr, MY_GRAPHML_ENCODING,1);
1157
1158 xmlFreeDoc(docPtr);
1159
1160 return true;
1161 } // end ManetGraphMLParser::Write()
1162
1163 ManetGraphMLParser::Attribute*
1164 ManetGraphMLParser::AttributeList::FindAttribute(const char *theLookup,const char* theIndex)
1165 {
1166 AttributeList::Iterator it(*this,false,theLookup,strlen(theLookup)*8);
1167 Attribute* attr(NULL);
1168 attr = it.GetNextItem();
1169 while(NULL !=attr)
1170 {
1171 if(strcmp(attr->GetLookup(),theLookup))
1172 {
1173 attr = NULL;
1174 //we didn't find the entry
1175 } else {
1176 if(strcmp(attr->GetIndex(),theIndex))
1177 {
1178 //this attribute does not have the index we are looking for
1179 attr = it.GetNextItem();
1180 } else {
1181 return attr;
1182 }
1183 }
1184 }
1185 return attr;
1186 }
1187 bool
1188 ManetGraphMLParser::WriteLocalKeys(xmlTextWriter* writerPtr)
1189 {
1190 int rv = 0;
1191 IndexKeylist::Iterator it(indexkeylist);
1192 AttributeKey* key(NULL);
1193 while(NULL != (key = it.GetNextItem()))
1194 {
1195 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "key");
1196 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id",BAD_CAST key->GetIndex());
1197 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.name",BAD_CAST key->GetName());
1198 switch (key->GetType())
1199 {
1200 case AttributeKey::Types::BOOL:
1201 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "boolean");
1202 break;
1203 case AttributeKey::Types::INT:
1204 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "int");
1205 break;
1206 case AttributeKey::Types::LONG:
1207 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "long");
1208 break;
1209 case AttributeKey::Types::FLOAT:
1210 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "float");
1211 break;
1212 case AttributeKey::Types::DOUBLE:
1213 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "double");
1214 break;
1215 case AttributeKey::Types::STRING:
1216 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "string");
1217 break;
1218 default:
1219 PLOG(PL_ERROR,"ManetGraphMLParser::WriteLocalKeys: type is invalid!\n");
1220 return false;
1221 }
1222 switch (key->GetDomain())
1223 {
1224 case AttributeKey::Domains::GRAPH:
1225 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "graph");
1226 break;
1227 case AttributeKey::Domains::NODE:
1228 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "node");
1229 break;
1230 case AttributeKey::Domains::EDGE:
1231 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "edge");
1232 break;
1233 case AttributeKey::Domains::ALL:
1234 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "all");
1235 break;
1236 default:
1237 break;
1238 }
1239 if (NULL != key->GetDefault())
1240 {
1241 rv += xmlTextWriterWriteElement(writerPtr,BAD_CAST "default", BAD_CAST key->GetDefault());
1242 }
1243 rv += xmlTextWriterEndElement(writerPtr);
1244 }
1245 if(rv < 0)
1246 return false;
1247 return true;
1248 }
1249 bool
1250 ManetGraphMLParser::WriteLocalNodeAttributes(xmlTextWriter* writerPtr,NetGraph::Node& theNode)
1251 {
1252 bool rv = true;
1253 char key[255];//this should be dynamic or checks added TBD
1254 sprintf(key,"node:%s",GetString(theNode));
1255 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1256 Attribute* attr(NULL);
1257 //iterate over items which have the matching keys
1258 attr = it.GetNextItem();
1259 while(NULL != attr)
1260 {
1261 if(strcmp(attr->GetLookup(),key))
1262 {
1263 attr = NULL;
1264 //PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalNodeAttributes():mykey=\"%s\",lookup=\"%s\",key=\"%s\",value=\"%s\"\n",key,attr->GetLookup(),attr->GetIndex(),attr->GetValue());
1265 //attr = it.GetNextItem();
1266 } else {
1267 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1268 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1269 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1270 rv += xmlTextWriterEndElement(writerPtr);
1271 PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalNodeAttributes():key=\"%s\",value=\"%s\"\n",attr->GetIndex(),attr->GetValue());
1272 attr = it.GetNextItem();
1273 }
1274 }
1275 return rv;
1276 }
1277 bool
1278 ManetGraphMLParser::WriteLocalInterfaceAttributes(xmlTextWriter* writerPtr,NetGraph::Interface& theInterface)
1279 {
1280 bool rv = true;
1281 char key[255];//this should be dynamic or checks added TBD
1282 sprintf(key,"node:%s",GetString(theInterface.GetNode()));
1283 sprintf(key,"%s:port:%s",key,GetString(theInterface));
1284 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1285 Attribute* attr(NULL);
1286 attr = it.GetNextItem();
1287 while(NULL != attr)
1288 {
1289 if(strcmp(attr->GetLookup(),key))
1290 {
1291 attr = NULL;
1292 } else {
1293 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1294 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1295 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1296 rv += xmlTextWriterEndElement(writerPtr);
1297 attr = it.GetNextItem();
1298 }
1299 }
1300 return rv;
1301 }
1302 bool
1303 ManetGraphMLParser::WriteLocalLinkAttributes(xmlTextWriter* writerPtr,NetGraph::Link& theLink)
1304 {
1305 bool rv = true;
1306 char key[255];//this should be dynamic or checkes added TBD
1307 sprintf(key,"edge:source:%s",GetString(theLink.GetSrc()->GetNode()));
1308 sprintf(key,"%s:%s",key,GetString(*theLink.GetSrc()));
1309 sprintf(key,"%s:dest:%s",key,GetString(theLink.GetDst()->GetNode()));
1310 sprintf(key,"%s:%s",key,GetString(*theLink.GetDst()));
1311 //printf("%s\n",key);
1312
1313 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1314 Attribute* attr(NULL);
1315 attr = it.GetNextItem();
1316 while(NULL != attr)
1317 {
1318 if(strcmp(attr->GetLookup(),key))
1319 {
1320 attr = NULL;
1321 } else {
1322 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1323 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1324 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1325 rv += xmlTextWriterEndElement(writerPtr);
1326 attr = it.GetNextItem();
1327 }
1328 }
1329 return rv;
1330 }