00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033
00034
00042 #ifndef _PHANTOMMIDDLEWAREMODULE_H
00043 #define _PHANTOMMIDDLEWAREMODULE_H
00044
00045 #ifdef USE_PHANTOMMIDDLEWARE
00046
00047 #include "../dllinclude.h"
00048
00049 #include <vector>
00050
00051 #include <phantom/Exceptions.hh>
00052
00053 #include <OpenTracker/OpenTracker.h>
00054 #include <OpenTracker/network/PhantomMiddlewareSink.h>
00055 #include <OpenTracker/network/PhantomMiddlewareSource.h>
00056 #include <ace/Task.h>
00057
00058 namespace ot {
00059
00060 typedef std::vector<PhantomMiddlewareSink*> PhantomMiddlewareSinkVector;
00061 typedef std::multimap<int, PhantomMiddlewareSource*> PidSourceMultiMap;
00062 typedef PidSourceMultiMap::iterator PidSourceMultiMapIterator;
00063 typedef std::map<std::string, PidSourceMultiMap*> GroupMapping;
00064 typedef std::pair<std::string, PidSourceMultiMap*> GroupMappingPair;
00065 typedef std::pair<int, PhantomMiddlewareSource*> PidSourcePair;
00066
00067 class OPENTRACKER_API PhantomListener : public ACE_Task_Base
00068 {
00069 public:
00070 PhantomListener (const std::string& group) : _stop(false) {
00071 pid_node_mapping = new PidSourceMultiMap;
00072 msr = new Phantom::Utils::MulticastSocketReceiver(group.c_str());
00073 };
00074
00075 virtual ~PhantomListener () {
00076 delete pid_node_mapping;
00077 delete msr;
00078 };
00079
00080 void Stop() {
00081 lock();
00082 _stop = true;
00083 unlock();
00084 };
00085
00086 void Start() {
00087 lock();
00088 _stop = false;
00089 unlock();
00090 };
00091
00092 bool isStopping() {
00093 lock();
00094 bool stopping = _stop;
00095 unlock();
00096 return stopping;
00097 };
00098
00099 void addNode(PhantomMiddlewareSource* node, int pid) {
00100 cerr << "adding Node with pid " << pid << endl;
00101 lock();
00102 pid_node_mapping->insert(PidSourcePair(pid, node));
00103 unlock();
00104 };
00105
00106 void removeNode(PhantomMiddlewareSource* node) {
00107 int pid;
00108 sscanf(node->get("pid").c_str(), " %i", &pid );
00109 lock();
00110
00111 std::pair<PidSourceMultiMapIterator, PidSourceMultiMapIterator> range_it = pid_node_mapping->equal_range(pid);
00112 PidSourceMultiMapIterator it = range_it.first;
00113 while (it != range_it.second) {
00114 PidSourceMultiMapIterator _it = it;
00115 it++;
00116 if (_it->second == node) {
00117 pid_node_mapping->erase(_it);
00118 delete node;
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127 unlock();
00128 }
00129
00130 void pushEvent() {
00131 lock();
00132 for (PidSourceMultiMap::const_iterator pidsource_it = (*pid_node_mapping).begin(); pidsource_it != (*pid_node_mapping).end(); ++pidsource_it )
00133 {
00134 pidsource_it->second->push();
00135 }
00136 unlock();
00137 }
00138
00139 virtual int svc (void)
00140 {
00141 while (isStopping() == false) {
00142 Phantom::Utils::UCharMessageReader ucm;
00143 (*msr) >> ucm;
00144
00145 unsigned char ver;
00146 short eid;
00147 unsigned char seq;
00148 long t1, t2;
00149 int discard;
00150
00151 ucm >> ver >> eid >> seq >> t1 >> t2 >> discard;
00152 int pid;
00153 float x,y,z, theta;
00154 char source[32];
00155 std::string src;
00156 if (eid != 13 && eid !=14) {
00157 try {
00158 ucm >> pid >> x >> y >> z;
00159 ucm >> theta;
00160 if (eid == 24) {
00161 ucm >> source;
00162 }
00163 src = source;
00164 lock();
00165 std::pair<PidSourceMultiMapIterator, PidSourceMultiMapIterator> range_it = pid_node_mapping->equal_range(pid);
00166 for (PidSourceMultiMapIterator it = range_it.first; it != range_it.second; ++it) {
00167 if (eid == (it->second)->getEventId()) {
00168 (it->second)->setEvent(x, y, z, theta, t1, t2, eid, source);
00169 }
00170 }
00171 unlock();
00172 } catch (PhantomException) {
00173 std::cerr << "Caught Phantom Exception: probably attempting to extract from empty buffer" << std::endl;
00174 }
00175 }
00176 }
00177 return 0;
00178 };
00179
00180 protected:
00181 inline void lock() {lock_.acquire();};
00182 inline void unlock() {lock_.release();};
00183 bool _stop;
00184
00185
00186 private:
00187 PidSourceMultiMap* pid_node_mapping;
00188 Phantom::Utils::MulticastSocketReceiver* msr;
00189 };
00190
00191 typedef std::map<std::string, PhantomListener*> GroupListenerMap;
00192
00201 class OPENTRACKER_API PhantomMiddlewareModule : public Module, public NodeFactory
00202 {
00203
00204 protected:
00206 PhantomMiddlewareSinkVector sinks;
00207
00208 GroupListenerMap listeners;
00209
00210
00211 public:
00213 PhantomMiddlewareModule() : Module(), NodeFactory()
00214 {
00215
00216 };
00218 virtual ~PhantomMiddlewareModule();
00219
00220 virtual void init(StringTable& attributes, ConfigNode * localTree);
00221
00223
00232 virtual Node * createNode( const std::string& name, StringTable& attributes);
00233
00234 virtual void clear();
00238 virtual void close();
00239
00242 virtual void start();
00243
00248 virtual void pushEvent();
00249
00250
00251
00252 virtual void removeNode(Node *);
00253 private:
00254 };
00255
00256 OT_MODULE(PhantomMiddlewareModule);
00257
00258 }
00259
00260 #endif //USE_PHANTOMMIDDLEWARE
00261
00262 #endif