| 1 |
#ifndef _MANET_GRAPHML |
| 2 |
#define _MANET_GRAPHML |
| 3 |
#define MAXXMLIDLENGTH 255 |
| 4 |
|
| 5 |
#include <manetGraph.h> |
| 6 |
#include <protoQueue.h> |
| 7 |
#include <libxml/encoding.h> |
| 8 |
#include <libxml/xmlwriter.h> |
| 9 |
#include <libxml/xmlreader.h> |
| 10 |
|
| 11 |
#define MY_GRAPHML_ENCODING "UTF-8" |
| 12 |
class ManetGraphMLParser |
| 13 |
{ |
| 14 |
public: |
| 15 |
virtual ~ManetGraphMLParser(); |
| 16 |
const char* GetString(NetGraph::Interface& iface) const; |
| 17 |
const char* GetString(NetGraph::Node& node) const; |
| 18 |
class AttributeKey : public ProtoQueue::Item |
| 19 |
{ |
| 20 |
public: |
| 21 |
struct Domains {enum Domain {INVALID, GRAPH, NODE, EDGE, ALL};}; |
| 22 |
struct Types {enum Type { INVALID, BOOL, INT, LONG, FLOAT, DOUBLE, STRING };}; |
| 23 |
//AttributeKey() : index(NULL), name(NULL), oldkey(NULL), domain(Domains::INVALID), type(Types::INVALID){} |
| 24 |
AttributeKey() : index(NULL), name(NULL), oldindex(NULL), defaultvalue(NULL), domain(Domains::INVALID),type(Types::INVALID){} |
| 25 |
bool Init(const char* theIndex,const char* theName,const char* theType, const char* theDomain = NULL,const char* theOldKey=NULL,const char* theDefault = NULL); |
| 26 |
bool Set(const char* theIndex,const char* theName,const char* theType, const char* theDomain = NULL,const char* theOldKey=NULL,const char* theDefault = NULL); |
| 27 |
~AttributeKey(); |
| 28 |
|
| 29 |
const char* GetIndex() const {return index;} |
| 30 |
const char* GetName() const {return name;} |
| 31 |
const char* GetOldIndex() const {return oldindex;} |
| 32 |
Types::Type GetType() {return type;} |
| 33 |
Domains::Domain GetDomain() {return domain;} |
| 34 |
const char* GetDefault() const {return defaultvalue;} |
| 35 |
private: |
| 36 |
char* index; |
| 37 |
char* name; |
| 38 |
char* oldindex; |
| 39 |
char* defaultvalue; |
| 40 |
Domains::Domain domain; |
| 41 |
Types::Type type; |
| 42 |
|
| 43 |
bool SetDomain(const char* theDomain); |
| 44 |
bool SetType(const char* theType); |
| 45 |
bool SetDefault(const char* theDefault); |
| 46 |
}; |
| 47 |
|
| 48 |
bool Read(const char* path, NetGraph& graph); // load graph from GraphML file |
| 49 |
bool Write(NetGraph& graph, const char* path, char* buffer=NULL, unsigned int* len_ptr = NULL); // make GraphML file from graph |
| 50 |
|
| 51 |
bool SetXMLName(const char* theName); |
| 52 |
|
| 53 |
bool SetAttributeKey(const char* theName,const char* theType, const char* theDomain = NULL, const char* oldIndex = NULL,const char* theDefault = NULL); |
| 54 |
|
| 55 |
bool SetAttribute(const char* theName, const char* theValue); |
| 56 |
bool SetAttribute(NetGraph::Node& node, const char* theName, const char* theValue); |
| 57 |
bool SetAttribute(NetGraph::Link& link, const char* theName, const char* theValue); |
| 58 |
bool SetAttribute(NetGraph::Interface& interface, const char* theName, const char* theValue); |
| 59 |
|
| 60 |
protected: |
| 61 |
bool AddAttributeKey(const char* theName,const char* theType, const char* theDomain = NULL, const char* oldIndex = NULL,const char* theDefault = NULL); |
| 62 |
bool AddAttribute(const char* theName, const char* theValue); |
| 63 |
bool AddAttribute(NetGraph::Node& node, const char* theName, const char* theValue); |
| 64 |
bool AddAttribute(NetGraph::Link& link, const char* theName, const char* theValue); |
| 65 |
bool AddAttribute(NetGraph::Interface& interface, const char* theName, const char* theValue); |
| 66 |
|
| 67 |
bool GetLookup(char* theLookup,unsigned int maxlen); |
| 68 |
bool GetLookup(char* theLookup,unsigned int maxlen,NetGraph::Node& node); |
| 69 |
bool GetLookup(char* theLookup,unsigned int maxlen,NetGraph::Link& link); |
| 70 |
bool GetLookup(char* theLookup,unsigned int maxlen,NetGraph::Interface& interface); |
| 71 |
|
| 72 |
ManetGraphMLParser(); |
| 73 |
|
| 74 |
private: |
| 75 |
const char* FindAttributeIndex(const char* theName); |
| 76 |
AttributeKey* FindAttributeKey(const char* theName); |
| 77 |
AttributeKey* FindAttributeKeyByOldIndex(const char* theOldIndex); |
| 78 |
|
| 79 |
//the next three classes are lists which provide index storage and look ups |
| 80 |
class IndexKeylist : public ProtoSortedQueueTemplate<AttributeKey> |
| 81 |
{ |
| 82 |
const char* GetKey(const ProtoQueue::Item& item) const |
| 83 |
{return static_cast<const AttributeKey&>(item).GetIndex();} |
| 84 |
unsigned int GetKeysize(const ProtoQueue::Item& item) const |
| 85 |
{return 8*strlen(static_cast<const AttributeKey&>(item).GetIndex());} |
| 86 |
} indexkeylist; |
| 87 |
class NamedKeylist : public ProtoSortedQueueTemplate<AttributeKey> |
| 88 |
{ |
| 89 |
const char* GetKey(const ProtoQueue::Item& item) const |
| 90 |
{return static_cast<const AttributeKey&>(item).GetName();} |
| 91 |
unsigned int GetKeysize(const ProtoQueue::Item& item) const |
| 92 |
{return 8*strlen(static_cast<const AttributeKey&>(item).GetName());} |
| 93 |
} namedkeylist; |
| 94 |
class OldIndexKeylist : public ProtoSortedQueueTemplate<AttributeKey> |
| 95 |
{ |
| 96 |
const char* GetKey(const ProtoQueue::Item& item) const |
| 97 |
{return static_cast<const AttributeKey&>(item).GetOldIndex();} |
| 98 |
unsigned int GetKeysize(const ProtoQueue::Item& item) const |
| 99 |
{return 8*strlen(static_cast<const AttributeKey&>(item).GetOldIndex());} |
| 100 |
} oldindexkeylist; |
| 101 |
|
| 102 |
class Attribute : public ProtoQueue::Item |
| 103 |
{ |
| 104 |
public: |
| 105 |
char* lookupvalue; //this string defines which node/port/edge the value belongs to. |
| 106 |
char* index; |
| 107 |
char* value; |
| 108 |
Attribute() : lookupvalue(NULL), index(NULL), value(NULL) {} |
| 109 |
bool Init(const char* theLookupvalue, const char* theIndex,const char* theValue); |
| 110 |
bool Set(const char* theLookupvalue, const char* theIndex, const char* theValue); |
| 111 |
~Attribute(); |
| 112 |
const char* GetLookup() const {return lookupvalue;} |
| 113 |
const char* GetIndex() const {return index;} |
| 114 |
const char* GetValue() const {return value;} |
| 115 |
}; |
| 116 |
class AttributeList : public ProtoSortedQueueTemplate<Attribute> |
| 117 |
{ |
| 118 |
const char* GetKey(const ProtoQueue::Item& item) const |
| 119 |
{return static_cast<const Attribute&>(item).GetLookup();} |
| 120 |
unsigned int GetKeysize(const ProtoQueue::Item& item) const |
| 121 |
{return 8*strlen(static_cast<const Attribute&>(item).GetLookup());} |
| 122 |
public: |
| 123 |
Attribute* FindAttribute(const char *theLookup,const char *theIndex); //we need to use this when searching for a specific Attribute entry |
| 124 |
} attributelist; |
| 125 |
// virtual bool WriteKeys(xmlTextWriter* writerPtr, NetGraph& graph) = 0; |
| 126 |
virtual bool UpdateKeys(NetGraph& graph) = 0; //this will replace the above as all keys will be stored locally |
| 127 |
bool WriteLocalKeys(xmlTextWriter* writerPtr); |
| 128 |
|
| 129 |
virtual NetGraph::Node* CreateNode() = 0; |
| 130 |
// virtual bool WriteNodeAttributes(xmlTextWriter* writerPtr,NetGraph::Node& theNode) = 0; |
| 131 |
virtual bool UpdateNodeAttributes(NetGraph::Node& theNode) = 0; //this will replace the above so keys will be stored locally |
| 132 |
bool WriteLocalAttributes(xmlTextWriter* writerPtr); |
| 133 |
bool WriteLocalNodeAttributes(xmlTextWriter* writerPtr,NetGraph::Node& theNode); |
| 134 |
|
| 135 |
virtual NetGraph::Interface* CreateInterface(NetGraph::Node& node) = 0; |
| 136 |
virtual NetGraph::Interface* CreateInterface(NetGraph::Node& node,ProtoAddress& addr) = 0; |
| 137 |
virtual bool InsertInterface(NetGraph::Interface& theIface) = 0; |
| 138 |
virtual bool AddInterfaceToNode(NetGraph::Node& theNode,NetGraph::Interface& theIface,bool makeDefault = false) = 0; |
| 139 |
virtual bool AddInterfaceToGraph(NetGraph& theGraph,NetGraph::Interface& theIface) = 0; |
| 140 |
virtual bool AddNodeToGraph(NetGraph& theGraph,NetGraph::Node& theNode) = 0; //this is a hook which allows derived classes to be do something with new nodes (like keep track and destroy them) |
| 141 |
//virtual bool InsertInterface(NetGraph::Interface& theIface) = 0; |
| 142 |
// virtual bool WriteInterfaceAttributes(xmlTextWriter* writerPtr,NetGraph::Interface& theInterface) = 0; |
| 143 |
virtual bool UpdateInterfaceAttributes(NetGraph::Interface& theInterface) = 0; //this will replace the above so keys will be stored locally |
| 144 |
bool WriteLocalInterfaceAttributes(xmlTextWriter* writerPtr,NetGraph::Interface& theInterface); |
| 145 |
|
| 146 |
virtual NetGraph::Cost* CreateCost(double value) = 0; |
| 147 |
|
| 148 |
virtual bool Connect(NetGraph::Interface& iface1, NetGraph::Interface& iface2, NetGraph::Cost& cost, bool isDuplex) = 0; |
| 149 |
// virtual bool WriteLinkAttributes(xmlTextWriter* writerPtr,NetGraph::Link& theLink) = 0; |
| 150 |
virtual bool UpdateLinkAttributes(NetGraph::Link& theLink) = 0; //this will replace the above so keys will be stored locally |
| 151 |
bool WriteLocalLinkAttributes(xmlTextWriter* writerPtr,NetGraph::Link& theLInk); |
| 152 |
|
| 153 |
bool ReadXMLNode(xmlTextReader* readerPtr, |
| 154 |
NetGraph& graph, |
| 155 |
char* parentXMLNodeID, |
| 156 |
bool& isDuplex); |
| 157 |
|
| 158 |
|
| 159 |
// Members |
| 160 |
char* XMLName; |
| 161 |
int indexes; |
| 162 |
|
| 163 |
}; // end class ManetGraphMLParser |
| 164 |
|
| 165 |
template <class COST_TYPE = ManetGraph::Cost, class IFACE_TYPE = ManetGraph::Interface, class LINK_TYPE = ManetGraph::Link,class NODE_TYPE = ManetGraph::Node> |
| 166 |
class ManetGraphMLTemplate : public ManetGraphMLParser, public NetGraphTemplate<COST_TYPE, IFACE_TYPE, LINK_TYPE, NODE_TYPE> |
| 167 |
{ |
| 168 |
public: |
| 169 |
ManetGraphMLTemplate() {} |
| 170 |
virtual ~ManetGraphMLTemplate() {} |
| 171 |
//virtual ~ManetGraphMLTemplate(); //we are assuming that all the nodes/interfaces/links in this type of graph were created locally using the parser so we delete them all upon destroy TBD |
| 172 |
|
| 173 |
bool Read(const char* path) // load graph from GraphML file |
| 174 |
{return ManetGraphMLParser::Read(path, *this);} |
| 175 |
|
| 176 |
bool Write(const char* path, char* buffer = NULL, unsigned int* len=NULL) // make GraphML file from graph |
| 177 |
{return ManetGraphMLParser::Write(*this, path,buffer,len);} |
| 178 |
bool Connect(NetGraph::Interface& iface1,NetGraph::Interface& iface2,NetGraph::Cost& theCost,bool isDuplex) |
| 179 |
{return NetGraph::Connect(iface1,iface2,theCost,isDuplex);} |
| 180 |
virtual bool InsertInterface(NetGraph::Interface& theIface) |
| 181 |
{return NetGraph::InsertInterface(theIface);} |
| 182 |
protected: |
| 183 |
// virtual bool WriteKeys(xmlTextWriter* writerPtr, NetGraph& theGraph) |
| 184 |
// {return true;} |
| 185 |
virtual bool UpdateKeys(NetGraph& theGraph) |
| 186 |
{return true;} |
| 187 |
/* virtual bool WriteNodeAttributes(xmlTextWriter* writerPtr, NetGraph::Node& theNode) |
| 188 |
{return true;} |
| 189 |
virtual bool WriteInterfaceAttributes(xmlTextWriter* writerPtr, NetGraph::Interface& theInterface) |
| 190 |
{return true;} |
| 191 |
virtual bool WriteLinkAttributes(xmlTextWriter* writerPtr, NetGraph::Link& theLInk) |
| 192 |
{return true;}*/ |
| 193 |
//bunny new |
| 194 |
virtual bool UpdateGraphAttributes(NetGraph& theGraph) |
| 195 |
{return true;} |
| 196 |
//bunny end new |
| 197 |
|
| 198 |
virtual bool UpdateNodeAttributes(NetGraph::Node& theNode) |
| 199 |
{return true;} |
| 200 |
virtual bool UpdateInterfaceAttributes(NetGraph::Interface& theInterface) |
| 201 |
{return true;} |
| 202 |
virtual bool UpdateLinkAttributes(NetGraph::Link& theLink) |
| 203 |
{return true;} |
| 204 |
private: |
| 205 |
class NetGraph::Node* CreateNode() |
| 206 |
{return static_cast<NetGraph::Node*>(new NODE_TYPE());} |
| 207 |
class NetGraph::Interface* CreateInterface(NetGraph::Node& node) |
| 208 |
{return static_cast<NetGraph::Interface*>(new IFACE_TYPE(static_cast<NODE_TYPE&>(node)));} |
| 209 |
class NetGraph::Interface* CreateInterface(NetGraph::Node& node,ProtoAddress& addr) |
| 210 |
{return static_cast<NetGraph::Interface*>(new IFACE_TYPE(static_cast<NODE_TYPE&>(node),addr));} |
| 211 |
class NetGraph::Cost* CreateCost(double value) |
| 212 |
{return static_cast<NetGraph::Cost*>(new COST_TYPE(value));} |
| 213 |
class NetGraph::Cost* CreateCost() |
| 214 |
{return static_cast<NetGraph::Cost*>(new COST_TYPE());} |
| 215 |
virtual bool AddInterfaceToNode(NetGraph::Node& theNode,NetGraph::Interface& theIface,bool makeDefault) |
| 216 |
{return (static_cast<NODE_TYPE&>(theNode)).AddInterface(theIface,makeDefault);} |
| 217 |
virtual bool AddInterfaceToGraph(NetGraph& theGraph,NetGraph::Interface& theIface) |
| 218 |
{return (static_cast<ManetGraphMLTemplate&>(theGraph)).InsertInterface(theIface);} |
| 219 |
virtual bool AddNodeToGraph(NetGraph& theGraph, NetGraph::Node& theNode) {return true;} //optional virtual function to allow derived classes to do something upon addition of a new node |
| 220 |
}; // end class ManetGraphMLParser |
| 221 |
|
| 222 |
|
| 223 |
// Example, default ManetGraphML |
| 224 |
class ManetGraphML : public ManetGraphMLTemplate<> {}; |
| 225 |
|
| 226 |
#endif // _MANET_GRAPHML |