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
00027
00028 #include "naxespositionviewer.hpp"
00029
00030 #include <rtt/Logger.hpp>
00031
00032 #ifndef _REENTRANT
00033 #define _REENTRANT
00034 #endif
00035 #include <ace/Reactor.h>
00036 #include <ace/Svc_Handler.h>
00037 #include <ace/Acceptor.h>
00038 #include <ace/Synch.h>
00039 #include <ace/SOCK_Acceptor.h>
00040 #include <ace/SOCK_Connector.h>
00041 #include <ace/INET_Addr.h>
00042 #include <ace/Log_Msg.h>
00043
00044 #include <math.h>
00045 #include <list>
00046 #include <vector>
00047 #include <iostream>
00048
00049 #include <ace/Select_Reactor_Base.h>
00050
00051 namespace OCL {
00052
00053 using namespace RTT;
00054 using namespace std;
00055
00060 #define MAXNROFJOINTS 32
00061
00065 struct DataRecord {
00066 int nrofjoints;
00067 double jointvalues[MAXNROFJOINTS];
00068 bool stop;
00069
00070 DataRecord() {
00071 for (int i=0;i<MAXNROFJOINTS;++i) jointvalues[i] = 0.0;
00072 stop = false;
00073 }
00074 };
00075
00076
00077
00078 class ClientHandler:
00079 public ACE_Svc_Handler <ACE_SOCK_STREAM,ACE_NULL_SYNCH>
00080 {
00081 typedef std::list<ClientHandler*> ClientHandlers;
00082 static std::list<ClientHandler*> clients;
00083 public:
00084 static ACE_Reactor* reactor_instance;
00085
00086 static void send_data(const std::vector<double>& q, bool stop) {
00087 DataRecord data;
00088 data.nrofjoints = q.size();
00089 for (unsigned int i=0;i<q.size();++i) {
00090 data.jointvalues[i] = q[i];
00091 }
00092 data.stop = stop;
00093
00094 for (ClientHandlers::iterator it=clients.begin();it!=clients.end();it++) {
00095 (*it)->peer().send_n(&data,sizeof(data));
00096 }
00097 }
00098
00099 static void delete_all() {
00100 for (ClientHandlers::iterator it=clients.begin();it!=clients.end();it++) {
00101 delete (*it);
00102 *it = 0;
00103 log(Info) << "(Viewer) Connection closed" << endlog();
00104 }
00105 }
00106
00107
00108 virtual int open(void*) {
00109 log(Info) << "(Viewer)Connection established" << endlog();
00110 if (reactor_instance==0) {
00111 log(Info) << "(Viewer) Programming error : reactor_instance should not be 0" << endlog();
00112 }
00113 reactor_instance->register_handler(this, ACE_Event_Handler::READ_MASK);
00114 clients.push_back(this);
00115 return 0;
00116 }
00117 virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask) {
00118 for (ClientHandlers::iterator it=clients.begin();it!=clients.end();it++) {
00119 if (*it==this) {
00120 clients.erase(it);
00121 break;
00122 }
00123 }
00124 log(Info) << "(Viewer)Connection closed" << endlog();
00125 delete this;
00126 return 0;
00127 }
00128 };
00129
00130 typedef ACE_Acceptor<ClientHandler,ACE_SOCK_ACCEPTOR> ClientAcceptor;
00131
00132 std::list<ClientHandler*> ClientHandler::clients;
00133 ACE_Reactor* ClientHandler::reactor_instance = 0;
00134
00135
00136 NAxesPositionViewer::NAxesPositionViewer(const std::string& name,const std::string& propertyfilename)
00137 : TaskContext(name),
00138 _propertyfile(propertyfilename),
00139 portnumber("portnumber","Port number to listen to for clients"),
00140 num_axes("numaxes","Number of axes to observe"),
00141 seperate_ports("seperate_ports","If it is true the input is of the form name0...nameN, otherwise it is a std::vector"),
00142 port_name("port_name","base name of the input"),
00143 clientacceptor(0),
00144 state(0)
00145 {
00146 log(Debug) << "Entering NAxesPositionViewer::NAxesPositionViewer" << endlog();
00150 portnumber.value() = 9999;
00151 num_axes.value() = 6;
00152
00156 properties()->addProperty( &portnumber );
00157 properties()->addProperty( &num_axes );
00158 properties()->addProperty( &seperate_ports);
00159 properties()->addProperty( &port_name);
00160
00161 if (!marshalling()->readProperties(_propertyfile)) {
00162 log(Error) << "Failed to read the property file, continue with default values." << endlog();
00163 }
00164 _num_axes = num_axes.value();
00165
00166 log(Debug) << "creating dataport(s) with base name : " << port_name.value() << endlog();
00170 if (seperate_ports.value()) {
00171 seperateValues.resize(_num_axes);
00172 jointvec.resize(_num_axes);
00173 for (int i=0;i<_num_axes;++i) {
00174 char buf[80];
00175 sprintf(buf,"%s%d",port_name.value().c_str(),i);
00176 seperateValues[i] = new ReadDataPort<double>(buf);
00177 ports()->addPort(seperateValues[i]);
00178 }
00179 } else {
00180 vectorValue = new RTT::ReadDataPort<std::vector<double> >(port_name.value());
00181 jointvec.resize(_num_axes);
00182 ports()->addPort(vectorValue);
00183 }
00184
00185 log(Debug) << "Leaving NAxesPositionViewer::NAxesPositionViewer" << endlog();
00186 }
00187
00188 NAxesPositionViewer::~NAxesPositionViewer()
00189 {
00190
00191
00192 }
00193
00198 bool NAxesPositionViewer::startup() {
00199 state=1;
00200 return true;
00201 }
00202
00206 void NAxesPositionViewer::update() {
00207
00208 if (state==1) {
00209 log(Info) << "(Viewer) startup()" << endlog();
00210 ACE_INET_Addr addr(portnumber);
00211 ClientHandler::reactor_instance = new ACE_Reactor();
00212 clientacceptor=new ClientAcceptor(addr,ClientHandler::reactor_instance);
00213 state=2;
00214 }
00215 if (state==2) {
00216 if (seperate_ports.value()) {
00217 for (unsigned int i=0;i<jointvec.size();i++) {
00218 jointvec[i] = seperateValues[i]->Get();
00219 }
00220 } else {
00221 unsigned int minsize = jointvec.size();
00222 if (vectorValue->Get().size() < minsize)
00223 minsize=vectorValue->Get().size();
00224 for (unsigned int i=0;i<minsize;i++) {
00225 jointvec[i] = vectorValue->Get()[i];
00226 }
00227 }
00228 ACE_Time_Value dt;
00229 dt.set(0.01);
00230 ClientHandler::reactor_instance->handle_events(dt);
00231 ClientHandler::send_data(jointvec,false);
00232 }
00233 }
00234
00238 void NAxesPositionViewer::shutdown() {
00239 log(Info) << "(Viewer) shutdown()" << endlog();
00240 ClientHandler::send_data(jointvec,true);
00241 ClientHandler::delete_all();
00242 delete (ClientAcceptor*)clientacceptor;
00243 delete ClientHandler::reactor_instance;
00244 ClientHandler::reactor_instance=0;
00245 clientacceptor=0;
00246 }
00247
00248
00249 }
00250