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.
- Add a element to parse in the Xml file
- Prepare where how to store your new data, and how to access it. In my case I used SSimWorld.h
- Variable
- Accessor: get/set
- Modify WorldXMLReader.cpp, add the new Tags in the parser (in the big if/else)
- Use the set accessor
- Add in WordSimulator.h, a ID and a name for your event
- Be carefull to REQUEST_SIZE
- Add a request to Winmain.h (viewer side)
- Add to WordSimulator.cpp a case when answer to your request
- Use the get accessor
- Convert datas in string
- Use the socket: s
- send first the size of the message (size is store in char[6])
- 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;
}
- Viewer side, Add to Winmain.cpp a case to send your request
- Use the function: sendRequest(),
- the socket mSock to receive the contain
- Parse the contain
- 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