Exemplo n.º 1
0
void JNetwork::Update()
{
    boost::mutex::scoped_lock r(receiveMutex);
    // Checking for some command to execute
    size_t begin_start_tag = received.str().find("<");
    size_t end_start_tag = received.str().find(">");
    string command = received.str().substr(begin_start_tag+1, end_start_tag-(begin_start_tag+1));
    size_t begin_end_tag = received.str().find(command + "/>");
    size_t end_end_tag = received.str().find("/>");
    if(begin_start_tag != string::npos && begin_end_tag != string::npos )
    {
        map<string, CommandStruc>::iterator ite = sCommandMap.find(command);
        if(ite != sCommandMap.end())
        {
            DebugTrace("begin of command received : " + received.str() );
            DebugTrace("begin of command toSend : " + toSend.str() );

            processCmd theMethod = (ite)->second.processCommand;
            stringstream input(received.str().substr(end_start_tag+1, (begin_end_tag-1)-(end_start_tag+1)));
            stringstream output;
            theMethod((ite)->second.object, input, output);
            string aString = received.str().substr(end_end_tag+2, string::npos);
            received.str(aString);
            if(output.str().size())
                sendCommand((ite)->second.command, output.str(), "Response");

            DebugTrace("end of command received : "<< received.str() );
            DebugTrace("end of command toSend : "<< toSend.str() );
        }
    }
}
Exemplo n.º 2
0
void JNetwork::ThreadProc(void* param)
{
  JNetwork* pThis = reinterpret_cast<JNetwork*>(param);
  JSocket* pSocket = NULL;
  if (pThis->serverIP.size()) {
    DebugTrace("Starting Client Thread");
    pThis->socket = new JSocket(pThis->serverIP);
    if(pThis->socket->isConnected())
      pSocket = pThis->socket;
  } else {
    DebugTrace("Starting Server Thread");
    pThis->socket = new JSocket();
    // Wait for some client
    pSocket = pThis->socket->Accept();
  }

  while(pSocket && pSocket->isConnected()) {
    char buff[1024];
    {
      boost::mutex::scoped_lock l(pThis->receiveMutex);
      int len =  pSocket->Read(buff, sizeof(buff));
      if(len) {
        DebugTrace("receiving " << len << " bytes : " << buff);
        pThis->received << buff;
      }
      // Checking for some command to execute
      size_t found = pThis->received.str().find("Command");
      if(found != string::npos)
      {
        map<string, processCmd>::iterator ite = sCommandMap.find((pThis->received.str()).substr(0, found) + "Command");
        if(ite != sCommandMap.end())
        {
          DebugTrace("begin of command received : "<< pThis->received.str() );
          DebugTrace("begin of command toSend : "<< pThis->toSend.str() );

          boost::mutex::scoped_lock l(pThis->sendMutex);
          pThis->toSend << pThis->received.str().substr(0, found) + "Response ";
          pThis->received.str("");
          processCmd theMethod = (ite)->second;
          theMethod(pThis->received, pThis->toSend);

          DebugTrace("end of command received : "<< pThis->received.str() );
          DebugTrace("end of command toSend : "<< pThis->toSend.str() );
        }
      }
      // Checking for some response to execute
      found = pThis->received.str().find("Response");
      if(found != string::npos)
      {
        map<string, processCmd>::iterator ite = sCommandMap.find((pThis->received.str()).substr(0, found) + "Response");
        if(ite != sCommandMap.end())
        {
          DebugTrace("begin of response received : "<< pThis->received.str() );
          DebugTrace("begin of response toSend : "<< pThis->toSend.str() );

          boost::mutex::scoped_lock l(pThis->sendMutex);
          string aString;
          pThis->received >> aString;
          processCmd theMethod = (ite)->second;
          theMethod(pThis->received, pThis->toSend);
          pThis->received.str("");

          DebugTrace("end of response received : "<< pThis->received.str() );
          DebugTrace("end of response toSend : "<< pThis->toSend.str() );
        }
      }
    }
Exemplo n.º 3
0
ValueWrapper SceneManagerCallback_script( const char* symName, const char* method,
                                          boost::python::list& argList, int cascadeEvents )
{
    //int i;
    std::string    theMethod( method );
    cppintrospection::ValueList theArgs;
    //printf("SceneManagerCallback_script: hi! %s, %s, [%s]\n", symName, types, args.c_str());
    t_symbol *s = gensym( symName );

    if (!s->s_thing)
    {
        //std::cout << "oscParser: Could not find referenced object named: " << symName << std::endl;
        return ValueWrapper(0);
    }

    // get osgInrospection::Value from passed UserData by casting as the proper
    // referenced object pointer:
    cppintrospection::Value classInstance;
    if (s->s_type == REFERENCED_STATESET)
        classInstance = cppintrospection::Value(dynamic_cast<ReferencedStateSet*>(s->s_thing));
    else
        classInstance = cppintrospection::Value(dynamic_cast<ReferencedNode*>(s->s_thing));

    // the getInstanceType() method however, gives us the real type being pointed at:
    const cppintrospection::Type &classType = classInstance.getInstanceType();

    if (! classType.isDefined())
    {
        std::cout << "ERROR: oscParser cound not process message '" << symName
                  << ". cppintrospection has no data for that node." << std::endl;
        return ValueWrapper(0);
    }

    size_t nbArgs = boost::python::len( argList );
    double da; float fa; int ia; std::string sa;

    for ( size_t i = 0; i < nbArgs; i++ )
    {

        if ( pyextract( argList[i], &da ) )
            theArgs.push_back( (double) da );
        else if ( pyextract( argList[i], &fa ) )
            theArgs.push_back( (float) fa );
        else if ( pyextract( argList[i], &ia ) )
            theArgs.push_back( (int) ia );
        else
        {
            sa = pyextract( argList[i] );
            if (sa != "" )
                theArgs.push_back( (const char*) sa.c_str() );
        }
    }

    // invoke the method on the node, and if it doesn't work, then just forward
    // the message:
    cppintrospection::Value v;
    bool eventScriptCalled = false;
    if (cascadeEvents)
    {
        if (s->s_type == REFERENCED_NODE)
        {
            //printf("calling eventscript...\n");
            eventScriptCalled = dynamic_cast<ReferencedNode*>(s->s_thing)->callEventScript( theMethod, theArgs );
        }
    }

    if (eventScriptCalled)
    { // cascaded event script called successful, do not invokeMethod, return nothing to the python script
        return ValueWrapper(0);
    }
    else
    { // no cascaded event assigned to theMethod or cascaded event script failed.  call invokeMethod and return result to python script
        if (invokeMethod(classInstance, classType, theMethod, theArgs, v))
        {
            return ValueWrapper(v);
        }
        else
        {
            if (spinApp::Instance().getContext()->isServer())
            {
                lo_message msg = lo_message_new();
                lo_message_add_string(msg, theMethod.c_str());
                for ( size_t i = 0; i < nbArgs; i++ )
                {
                    if ( pyextract( argList[i], &da ) ) lo_message_add_float(msg, (float) da );
                    else if ( pyextract( argList[i], &fa ) ) lo_message_add_float(msg, (float) fa );
                    else if ( pyextract( argList[i], &ia ) ) lo_message_add_float(msg, (float) ia );
                    else
                    {
                        sa = pyextract( argList[i] );
                        if (sa != "" ) lo_message_add_string(msg, (const char*) sa.c_str() );
                    }
                }
                std::string path = "/SPIN/" + spinApp::Instance().getSceneID() + "/" + s->s_name;
                std::vector<lo_address>::iterator addrIter;
                for (addrIter = spinApp::Instance().getContext()->lo_txAddrs_.begin(); addrIter != spinApp::Instance().getContext()->lo_txAddrs_.end(); ++addrIter)
                {
                    lo_send_message_from((*addrIter), spinApp::Instance().getContext()->lo_infoServ_, path.c_str(), msg);
                }
            }
        }

    }
    //pthread_mutex_unlock(&sceneMutex);
    return ValueWrapper(0);
}