00001 #ifndef _LIVECONTEXT_H
00002 #define _LIVECONTEXT_H
00003
00004 #include "../dllinclude.h"
00005
00006 #include <omniORB4/poa.h>
00007
00008 #include <OpenTracker/common/CommonNodeFactory.h>
00009 #include <OpenTracker/OpenTracker.h>
00010 #include <OpenTracker/core/Context.h>
00011 #include <OpenTracker/core/Module.h>
00012 #include <OpenTracker/core/NodeFactoryContainer.h>
00013
00014 #include <OpenTracker/skeletons/OTGraph.hh>
00015
00016 #include <OpenTracker/core/OtLogger.h>
00017
00018 #include <xercesc/dom/DOM.hpp>
00019 #include <xercesc/dom/DOMWriter.hpp>
00020 #include <xercesc/util/XMLString.hpp>
00021 #include <xercesc/util/PlatformUtils.hpp>
00022
00023 #if defined(XERCES_NEW_IOSTREAMS)
00024 #include <iostream>
00025 #else
00026 #include <iostream.h>
00027 #endif
00028
00029 XERCES_CPP_NAMESPACE_USE
00030
00031 using namespace std;
00032
00033 namespace ot {
00034
00035 typedef std::pair<Node *, Node *> Edge;
00036 typedef std::vector<Edge> EdgeVector;
00037
00038 typedef std::map<std::string, Node*> NodeMap;
00039
00040 class OPENTRACKER_API LiveContext : public Context,
00041 public PortableServer::RefCountServantBase {
00042 private:
00043 CORBAModule* corba_module;
00044 int no_nodes;
00045
00046 NodeMap nodes;
00047 EdgeVector edges;
00048
00049 string generateNewId() {
00050 char node_string[5];
00051 sprintf(node_string,"%d", no_nodes++);
00052 return string("Node") + string(node_string);
00053 };
00054
00055 public:
00056 LiveContext() : Context(1), no_nodes(0) {
00057 cerr << "LiveContext:: Constructor" << endl;
00058
00059 corba_module = (CORBAModule*) modules["CORBAConfig"];
00060 StringTable st;
00061
00062 st.put("endPoint", "giop:tcp:localhost:9999");
00063 corba_module->init(st, NULL);
00064 parseConfigurationString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE OpenTracker SYSTEM \"opentracker.dtd\"><OpenTracker> <configuration/></OpenTracker>");
00065 };
00066
00067 virtual ~LiveContext() {
00068 cerr << "LiveContext:: Destructor" << endl;
00069 };
00070
00071 ModuleMap getModules() {
00072 return modules;
00073 };
00074
00075 OTGraph::Node_ptr get_node(const char* id) {
00076 PortableServer::POA_var poa = corba_module->getPOA();
00077 PortableServer::ObjectId_var node_id = PortableServer::string_to_ObjectId(CORBA::string_dup(id));
00078 return OTGraph::Node::_duplicate(OTGraph::Node::_narrow(poa->id_to_reference(node_id)));
00079 };
00080
00081 Node* getNodeFromRef(const OTGraph::Node_var& node_ref) {
00082 PortableServer::POA_var poa = corba_module->getPOA();
00083 PortableServer::ServantBase* node_servant = poa->reference_to_servant(OTGraph::Node::_duplicate(node_ref));
00084 Node* node = dynamic_cast<Node*>(node_servant);
00085 node->_remove_ref();
00086 return node;
00087 }
00088
00089 OTGraph::Node_var getRefFromNode(Node* node) {
00090 string id(node->get("ID"));
00091 PortableServer::ObjectId_var node_id = PortableServer::string_to_ObjectId(id.c_str());
00092 OTGraph::Node_var node_ref = OTGraph::Node::_narrow((corba_module->getPOA())->id_to_reference(node_id));
00093 return node_ref;
00094 }
00095
00096 OTGraph::Node_var activateNode(Node* node) {
00097 PortableServer::POA_var poa = corba_module->getPOA();
00098 CORBA::String_var string_id = CORBA::string_dup(node->get("ID").c_str());
00099 PortableServer::ObjectId_var corba_id = PortableServer::string_to_ObjectId(string_id);
00100 poa->activate_object_with_id(corba_id, node);
00101 OTGraph::Node_var node_ref = OTGraph::Node::_narrow(poa->id_to_reference(corba_id));
00102 node->_remove_ref();
00103 std::cout << "Node " << node->get("ID") << " activated with reference count " << node->_refcount_value() << std::endl;
00104 return node_ref;
00105 }
00106
00107 void deactivateNode(Node* node) {
00108 string id(node->get("ID"));
00109 PortableServer::ObjectId_var node_id = PortableServer::string_to_ObjectId(id.c_str());
00110 std::cout << "Node " << node->get("ID") << " deactivating with reference count " << node->_refcount_value() << std::endl;
00111 (corba_module->getPOA())->deactivate_object(node_id);
00112
00113 }
00114
00115 OTGraph::Node_var create_node(const char* _name, const OTGraph::StringTable& _attributes) {
00116 string name(_name);
00117 StringTable attributes(_attributes);
00118 if (!attributes.containsKey("ID")) {
00119 attributes.put("ID", generateNewId());
00120 cerr << "generated ID = " << attributes.get("ID") << endl;
00121 } else {
00122 cerr << "ID in attributes = " << attributes.get("ID") << endl;
00123 }
00124 lock();
00125 Node* value = createNode(name, attributes);
00126 value->type = name;
00127 value->name = value->get("ID");
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 nodes[value->name] = value;
00141 OTGraph::Node_var node_ref = activateNode(value);
00142 unlock();
00143 return node_ref;
00144 }
00145
00146 void connect_nodes(const OTGraph::Node_var& upstreamNode, const OTGraph::Node_var& downstreamNode) {
00147 lock();
00148 Node::Ptr upstream_node = getNodeFromRef(upstreamNode);
00149 Node* downstream_node = getNodeFromRef(downstreamNode);
00150 downstream_node->addChild(*upstream_node);
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 edges.push_back(Edge(((Node*) upstream_node), downstream_node));
00165 unlock();
00166 }
00167
00168
00169 void disconnect_nodes(const OTGraph::Node_var& upstreamNode, const OTGraph::Node_var& downstreamNode) {
00170 lock();
00171 Node* upstream_node = getNodeFromRef(upstreamNode);
00172 Node* downstream_node = getNodeFromRef(downstreamNode);
00173
00174 NodeVector::iterator parent =
00175 std::find( upstream_node->parents.begin(), upstream_node->parents.end(), downstream_node);
00176 if (parent != upstream_node->parents.end()) {
00177 upstream_node->parents.erase(parent);
00178 }
00179 NodeVector::iterator child =
00180 std::find( downstream_node->children.begin(), downstream_node->children.end(), upstream_node);
00181 if (child != downstream_node->children.end()) {
00182
00183 downstream_node->children.erase(child);
00184 }
00185 Edge edge(upstream_node, downstream_node);
00186 EdgeVector::iterator result = std::find( edges.begin(), edges.end(), edge );
00187 if( result != edges.end())
00188 {
00189 edges.erase( result );
00190 }
00191 unlock();
00192 }
00193
00194 OTGraph::NodeVector* get_nodes() {
00195 OTGraph::NodeVector_var node_refs = new OTGraph::NodeVector;
00196 int len = nodes.size();
00197 node_refs->length(len);
00198 int i = 0;
00199 for (NodeMap::iterator it=nodes.begin(); it != nodes.end(); it++) {
00200 node_refs[i] = getRefFromNode(it->second);
00201 i++;
00202 }
00203 return node_refs._retn();
00204 };
00205
00206 OTGraph::EdgeVector* get_edges() {
00207 OTGraph::EdgeVector_var _edges = new OTGraph::EdgeVector;
00208 int len = edges.size();
00209 _edges->length(len);
00210 for (int i=0; i < edges.size(); i++) {
00211 OTGraph::Edge _edge;
00212 _edge.sender = getRefFromNode(edges[i].first);
00213 _edge.receiver = getRefFromNode(edges[i].second);
00214 _edges[i] = _edge;
00215 }
00216 return _edges._retn();
00217 };
00218
00219 void remove_node(const OTGraph::Node_var& target_ref) {
00220 lock();
00221 Node* target = getNodeFromRef(target_ref);
00222 std::cout << "Node " << target->get("ID") << " of type " << target->getType() << " being removed with reference count " << target->_refcount_value() << std::endl;
00223
00224
00225 NodeVector::iterator child_it = target->children.begin();
00226 while ( child_it != target->children.end())
00227 {
00228 Node* child = (Node *) *child_it;
00229 target->removeChild(*child);
00230 child_it++;
00231 }
00232
00233 NodeVector::iterator parent_it = target->parents.begin();
00234 while ( parent_it != target->parents.end())
00235 {
00236 Node * parent = (Node *) (*parent_it);
00237 parent->removeChild(*target);
00238 parent_it++;
00239 }
00240
00241
00242 string id(target->get("ID"));
00243 NodeMap::iterator result = nodes.find(id);
00244 if( result != nodes.end())
00245 {
00246 nodes.erase( result );
00247 }
00248
00249
00250 Module * mod = getModuleFromNode(target);
00251 if (mod != NULL) {
00252 logPrintI("calling mod->removeNode\n");
00253 mod->removeNode(target);
00254 logPrintI("called mod->removeNode\n");
00255 } else {
00256 logPrintE("unable to find module associated with node %s\n",id.c_str());
00257 }
00258
00259
00260 deactivateNode(target);
00261
00262
00263 unlock();
00264 }
00265
00266 void run()
00267 {
00268
00269 int stopflag = stop();
00270 while ( stopflag == 0 )
00271 {
00272 stopflag=loopOnce();
00273 }
00274
00275 };
00276
00277
00278 char* getXMLString() {
00279 return CORBA::string_dup("<foo/>");
00280 }
00281
00282 protected:
00283 ConfigNode* configNode;
00284 };
00285 }
00286 #endif //_LIVECONTEXT_H