00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00088 #include "../include/IPSA/ipsaclasses.h"
00089 #include "../include/IPSA/network/TCPSocket.h"
00090 #include "../include/IPSA/viewer/NetworkViewer.h"
00091 #include "../include/IPSA/viewer/instructions/NVInstructions.h"
00092
00093 #include <Inventor/threads/SbThread.h>
00094
00095 #include <cstdio>
00096 #include <iostream>
00097 #include <sstream>
00098 #include <vector>
00099
00107 NetworkViewer::NetworkViewer(std::string filename, SO_WINDOW parent, const char *name, SbBool embed)
00108 : IpsaViewer(filename, parent, name, embed),
00109 networkThread(NULL),
00110 LISTEN_PORT(12345)
00111 {
00112 instructionMap.clear();
00113
00114
00115 commands.clear();
00116
00117
00118 tcp_socket = new CTCPSocket("", LISTEN_PORT, true);
00119
00120 networkReceiveSensor.setFunction(NetworkViewer::ReadDatagrams);
00121 networkReceiveSensor.setData(this);
00122 networkReceiveSensor.setInterval(0.0001);
00123
00124 this->setTitle("IPSRV");
00125
00126 this->setupNetworkThread();
00127 }
00128
00129
00134 NetworkViewer::~NetworkViewer()
00135 {
00136 delete tcp_socket;
00137 tcp_socket = NULL;
00138 SbThread::destroy(networkThread);
00139 }
00140
00141
00153 void NetworkViewer::ReadDatagrams(void* data, SoSensor*)
00154 {
00155 std::vector< std::vector<std::string> > instructions;
00156 NetworkViewer* networkViewer = (NetworkViewer*) data;
00157 if (NULL == networkViewer)
00158 return;
00159
00160 if (!networkViewer->tcp_socket->isConnected())
00161 return;
00162
00163 char recvbuf[4096];
00164 int ret = networkViewer->tcp_socket->receive(recvbuf, 4096 * sizeof(char));
00165
00166 if (ret == SOCKET_ERROR)
00167 {
00168
00169 return;
00170 }
00171 else if (ret == 0)
00172 {
00173
00174 printf("IPSRV - Remote connection was closed, waiting for a new one.\n");
00175 networkViewer->tcp_socket->closeSocket();
00176 networkViewer->setupNetworkThread();
00177 return;
00178 }
00179 else
00180 {
00181 for (int i=0; i<ret; i++)
00182 {
00183 if (recvbuf[i] != 0x0A)
00184 {
00185 if (recvbuf[i] != 0x0D)
00186 networkViewer->commands += recvbuf[i];
00187 }
00188 else
00189 {
00190
00191
00192 std::string::size_type j = networkViewer->commands.find_first_not_of(";");
00193 while ((j != networkViewer->commands.npos) && (j != networkViewer->commands.size() ) )
00194 {
00195 std::string::size_type k = networkViewer->commands.find(";",j);
00196 if (k == networkViewer->commands.npos)
00197 k = networkViewer->commands.size();
00198
00199 std::string cmd(networkViewer->commands.substr(j,k-j));
00200 std::vector<std::string> singleInstr;
00201 std::string::size_type l = cmd.find_first_not_of(" ");
00202 while ( (l != cmd.npos) && (l != cmd.size()) )
00203 {
00204 std::string::size_type m = cmd.find(" ",l);
00205 if (m == cmd.npos)
00206 m = cmd.size();
00207 singleInstr.push_back(cmd.substr(l,m-l));
00208 l = cmd.find_first_not_of(" ",m);
00209 }
00210 instructions.push_back(singleInstr);
00211 j = networkViewer->commands.find_first_not_of(";",k);
00212 }
00213 networkViewer->executeCommands(instructions);
00214 networkViewer->commands.clear();
00215 }
00216 }
00217 }
00218 }
00219
00220
00225 void NetworkViewer::setupNetworkThread()
00226 {
00227 if (NULL != networkThread)
00228 SbThread::destroy(networkThread);
00229
00230 networkThread = SbThread::create(NetworkViewer::NetworkSetup, (void*)this);
00231 }
00232
00233
00241 void NetworkViewer::executeCommands(std::vector< std::vector<std::string> >& Instr)
00242 {
00243 std::ostringstream answer;
00244 for(unsigned int i = 0; i < Instr.size(); i++)
00245 {
00246 bool foundinstr = (instructionMap.end() != instructionMap.find(Instr[i][0]));
00247 if(!foundinstr)
00248 {
00249 printf("ERROR: instruction \"%s\" doesn't exist!\n", Instr[i][0].c_str());
00250 continue;
00251 }
00252
00253 answer.str().clear();
00254 unsigned int numberOfReturnValues;
00255 instructionMap[ (Instr[i][0]) ]->findAndExecute(Instr[i], answer, numberOfReturnValues);
00256
00257
00258
00259 if (0 >= answer.str().length())
00260 answer << std::endl;
00261 if (-1 == tcp_socket->send(answer.str()))
00262 printf("Error could not send via socket!\n");
00263 else answer.str("");
00264 }
00265 }
00266
00267
00282 void* NetworkViewer::NetworkSetup(void* data)
00283 {
00284 NetworkViewer* networkViewer = static_cast<NetworkViewer*> (data);
00285 if (NULL == networkViewer)
00286 return NULL;
00287
00288 if (networkViewer->tcp_socket->isConnected())
00289 return NULL;
00290
00291 printf("IPSRV - Inventor Physics Simulation Remote Viewer\n");
00292 printf("Establishing server at TCP/IP connection localhost:port %d.\n", networkViewer->tcp_socket->PORT);
00293 printf("Type <help> for instruction list.\n\n");
00294
00295 networkViewer->networkReceiveSensor.unschedule();
00296
00297 if (!networkViewer->tcp_socket->openSocket())
00298 {
00299 printf("IPSRV - failed to establish a connection\n");
00300 return NULL;
00301 }
00302
00303 networkViewer->networkReceiveSensor.schedule();
00304
00305 return NULL;
00306 }