How to add a request to server from the Viewer

  • This document is written by Mickael Nicolaccini

Example case: Add a new element/ parameter in world xml file and load this parameter using a new request.

  1. Add a element to parse in the Xml file
    1.png
  2. Prepare where how to store your new data, and how to access it. In my case I used SSimWorld.h
    1. Variable
      2a.png
    2. Accessor: get/set
      2b.png
  3. Modify WorldXMLReader.cpp, add the new Tags in the parser (in the big if/else)
    1. Use the set accessor
      3.png
  4. Add in WordSimulator.h, a ID and a name for your event
    1. Be carefull to REQUEST_SIZE
      4.png
  5. Add a request to Winmain.h (viewer side)
    5.png
  6. Add to WordSimulator.cpp a case when answer to your request
    1. Use the get accessor
    2. Convert datas in string
    3. Use the socket: s
      1. send first the size of the message (size is store in char[6])
      2. then send the message
           case REQUEST_VIEWER_GET_MAIN_CAM:
           {
             // std::cout <<"FROM WorldSimulator" << m_posMcam.x() << " " << m_posMcam.y() << " "<< m_posMcam.z() << std::endl ; //DISPLAY DEBUG
             std::ostringstream sstream;
             if(w->posMcam())
             {
               Vector3d m_posMcam  = w->get_posMcam();
               sstream << m_posMcam.x() << " " << m_posMcam.y()  <<  " "  <<  m_posMcam.z() ;
             }
             if(w->dirMcam())
             {
                Vector3d m_lookAtMcam  = w->get_lookAtMcam();
                sstream << " lookAt: " <<m_lookAtMcam.x() << " " << m_lookAtMcam.y()  <<  " "  <<  m_lookAtMcam.z() ;
             }
             else if(w->quartMcam())
             {
               double * quart =w->get_quaternionMcam();
               sstream << " quaternion: " <<quart[0]<< " " << quart[1]  <<  " "  <<  quart[2] <<  " "  <<  quart[3] ;
             }
             std::string msg = sstream.str();
             char bsize[6];
             int nbyte = (int)msg.size() ;
             sprintf(bsize, "%.6d", nbyte);
             msg = std::string(bsize) + msg;
             //std::cout <<"Message From WSim: "<< msg << " size: " << nbyte<<std::endl; // DEBUG DISPLAY
             if(!sendData(s, msg.c_str(), nbyte+6)){
                  std::cout << "fail to send pos cam";
               return false;
             }
             // else
               // std::cout << "send succefully " << std::endl ;
             continue;
           }
  7. Viewer side, Add to Winmain.cpp a case to send your request
    1. Use the function: sendRequest(),
    2. the socket mSock to receive the contain
    3. Parse the contain
    4. Apply the contain
      if(sendRequest(GET_MAIN_CAM)) // get the position and the direction of the main camera from the server 
      {
         mLog->printf(Sgv::LogBase::INFO, "GET_MAIN_CAM succeeded %s", ipString.c_str());
         mLog->info("GET_MAIN_CAM: OK");
         char hbuf[6];
         memset(hbuf, '\0', sizeof(hbuf)); // to avoid memory bug
         if(!mSock->recvData(hbuf, 6)){// receive size of the message
             delete [] &hbuf;
             MessageBox( NULL,"the Loading of the size of the message containning camera parrameters has failed ", _T("Error"), MB_OK); // DEBUG display
             return false;
         }
          int nbyte = atoi(hbuf);
           // MessageBox( NULL,hbuf, _T("Error"), MB_OK); // DEBUG display
          if(nbyte > 0 )
          {
             char *buf(NULL);
             buf = new char[nbyte];
             memset(buf, 0, sizeof(buf)); // to avoid memory bug
             if(!mSock->recvData(buf,nbyte)){// receive size of the message
                 delete [] &buf;
                 MessageBox( NULL,"the Loading of camera parrameters has failed ", _T("Error"), MB_OK);
                 return false;
             }
               // MessageBox( NULL,buf, _T("Error"), MB_OK); // DEBUG display
             applySettingMCam(buf);
             delete [] &buf;
         }
       }
Counter: 2374, today: 1, yesterday: 0

Attach file: file4.png 1404 download [Information] file5.png 1456 download [Information] file3.png 1490 download [Information] file2b.png 1412 download [Information] file2a.png 1398 download [Information] file1.png 1391 download [Information]

Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2013-12-27 (Fri) 21:56:28 (3770d)