Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

ArServerHandlerPopup.cpp

Go to the documentation of this file.
00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 #include "ArExport.h"
00028 #include "ArServerHandlerPopup.h"
00029 
00030 ArServerHandlerPopup::PopupData::PopupData(
00031         ArServerHandlerPopupInfo *popupInfo, ArTypes::Byte4 id,
00032         ArFunctor2<ArTypes::Byte4, int> *callback) 
00033 {
00034   myPopupInfo = new ArServerHandlerPopupInfo(*popupInfo); 
00035   myID = id;
00036   myCallback = callback; 
00037   myStarted.setToNow();
00038 }
00039 
00040 ArServerHandlerPopup::PopupData::~PopupData()
00041 {
00042   delete myPopupInfo;
00043 }
00044 
00045 AREXPORT ArServerHandlerPopup::ArServerHandlerPopup(ArServerBase *server) :
00046   myNetPopupClickedCB(this, &ArServerHandlerPopup::netPopupClicked),
00047   myServerCycleCB(this, &ArServerHandlerPopup::serverCycleCallback)
00048 {
00049   myServer = server;
00050   myServer->addData(
00051           "popupCreate", 
00052           "Tells the gui to create a new popup",
00053           NULL, 
00054           "none (you can't send this command to the server)",
00055           "byte4: id of this particular popup; string: ignoreIdentifier; string: title; string: message; byte: popupType (0 = NoIcon, 1 = Information, 2 = Warning, 3 = Critical, 4 = Question); string: button0Label; string: button1Label; string: button2Label; byte: defaultButton; byte: escapeButton",
00056           "PopupGui", "RETURN_SINGLE");
00057   myServer->addData(
00058           "popupClose",
00059           "Closes a popup that was created",
00060           NULL,
00061           "none (you can't send this command to the server)",
00062           "byte4: id of the popup to close; string: string to display",
00063           "PopupGui", "RETURN_SINGLE");
00064   myServer->addData(
00065           "popupClicked",
00066           "Returns the clicked data of a popup ",
00067           &myNetPopupClickedCB,
00068           "byte4: id of the popup that was clicked; byte: buttonClicked",
00069           "no response",
00070           "PopupGui", "RETURN_NONE");
00071   myServer->addCycleCallback(&myServerCycleCB);
00072   myLastTimeCheck.setToNow();
00073   myLastID = 0;
00074 
00075 } 
00076 
00077 
00078 AREXPORT ArServerHandlerPopup::~ArServerHandlerPopup()
00079 {
00080 
00081 }
00082 
00083 
00110 AREXPORT ArTypes::Byte4 ArServerHandlerPopup::createPopup(
00111         ArServerHandlerPopupInfo *popupInfo, 
00112         ArFunctor2<ArTypes::Byte4, int> *callback)
00113 {
00114   ArNetPacket sendingPacket;
00115   PopupData *popupData;
00116   ArTypes::Byte4 retID;
00117 
00118   myDataMutex.lock();
00119   // first find a good id
00120   myLastID++;
00121   if (myLastID < 0)
00122     myLastID = 1;
00123   //printf("Checking if id %u is good\n", myLastID);
00124   while (myMap.find(myLastID) != myMap.end())
00125   {
00126     myLastID++;
00127     if (myLastID < 0)
00128       myLastID = 1;
00129   }
00130   //printf("Got id %d\n", myLastID);
00131   popupData = new PopupData(popupInfo, myLastID, callback);
00132   myMap[myLastID] = popupData;
00133   sendingPacket.byte4ToBuf(myLastID);
00134   sendingPacket.strToBuf(popupData->myPopupInfo->getIgnoreIdentifier());
00135   sendingPacket.strToBuf(popupData->myPopupInfo->getTitle());
00136   sendingPacket.strToBuf(popupData->myPopupInfo->getMessage());
00137   sendingPacket.byteToBuf((ArTypes::Byte) 
00138                           popupData->myPopupInfo->getPopupType());
00139   sendingPacket.strToBuf(popupData->myPopupInfo->getButton0Label());
00140   sendingPacket.strToBuf(popupData->myPopupInfo->getButton1Label());
00141   sendingPacket.strToBuf(popupData->myPopupInfo->getButton2Label());
00142   sendingPacket.byteToBuf(popupData->myPopupInfo->getDefaultButtonNumber());
00143   sendingPacket.byteToBuf(popupData->myPopupInfo->getEscapeButtonNumber());
00144 
00145   /*
00146   printf("!!! %s %s %s\n", 
00147          popupData->myPopupInfo->getButton0Label(),
00148          popupData->myPopupInfo->getButton1Label(),
00149          popupData->myPopupInfo->getButton2Label());
00150   */
00151   myServer->broadcastPacketTcp(&sendingPacket, "popupCreate");
00152   retID = myLastID;
00153   myDataMutex.unlock();
00154   return retID;
00155 }
00156 
00157 AREXPORT void ArServerHandlerPopup::closePopup(ArTypes::Byte4 id, 
00158                                                 const char *closeMessage)
00159 {
00160   ArNetPacket sendingPacket;
00161   PopupData *popupData;
00162   std::map<ArTypes::Byte4, PopupData *>::iterator it;
00163 
00164   myDataMutex.lock();
00165   if ((it = myMap.find(id)) == myMap.end())
00166   {
00167     ArLog::log(ArLog::Verbose, 
00168        "Cannot close popup %u as it doesn't exist (anymore at least)", 
00169                id);
00170     myDataMutex.unlock();
00171   }
00172   else
00173   {
00174     popupData = (*it).second;
00175     sendingPacket.byte4ToBuf(id);
00176     sendingPacket.strToBuf(closeMessage);
00177     myMap.erase(id);
00178     myDataMutex.unlock();
00179     delete popupData;
00180     if (popupData->myCallback != NULL)
00181       popupData->myCallback->invoke(popupData->myID, -2);
00182     myServer->broadcastPacketTcp(&sendingPacket, "popupClose");
00183   }
00184 }
00185 
00186 AREXPORT void ArServerHandlerPopup::netPopupClicked(ArServerClient *client,
00187                                                      ArNetPacket *packet)
00188 {
00189   ArNetPacket sendingPacket;
00190   PopupData *popupData;
00191   std::map<ArTypes::Byte4, PopupData *>::iterator it;
00192 
00193   ArTypes::Byte4 id;
00194   ArTypes::Byte button;
00195 
00196   id = packet->bufToByte4();
00197   button = packet->bufToByte();
00198 
00199   myDataMutex.lock();
00200   if ((it = myMap.find(id)) == myMap.end())
00201   {
00202     ArLog::log(ArLog::Verbose, 
00203        "Cannot close popup %u for client %s as it doesn't exist (anymore at least)", 
00204                id, client->getIPString());
00205     myDataMutex.unlock();
00206   }
00207   else
00208   {
00209     popupData = (*it).second;
00210     sendingPacket.byte4ToBuf(id);
00211     if (button == 0)
00212       sendingPacket.strToBuf(popupData->myPopupInfo->getButton0Pressed()); 
00213     else if (button == 1)
00214       sendingPacket.strToBuf(popupData->myPopupInfo->getButton1Pressed()); 
00215     else if (button == 2)
00216       sendingPacket.strToBuf(popupData->myPopupInfo->getButton2Pressed()); 
00217     else 
00218       sendingPacket.strToBuf("Popup closed because of odd click");
00219     myMap.erase(id);
00220     myDataMutex.unlock();
00221     if (popupData->myCallback != NULL)
00222       popupData->myCallback->invoke(popupData->myID, button);
00223     delete popupData;
00224     myServer->broadcastPacketTcp(&sendingPacket, "popupClose");
00225   }
00226 
00227   
00228 }
00229 
00230 AREXPORT void ArServerHandlerPopup::serverCycleCallback(void)
00231 {
00232   std::map<ArTypes::Byte4, PopupData *>::iterator it;
00233   ArNetPacket sendingPacket;
00234   PopupData *popupData;
00235   int timeout;
00236 
00237   std::list<ArTypes::Byte4> doneIDs;
00238   std::list<PopupData *> donePopups;
00239   myDataMutex.lock();
00240   // only check it if we haven't checked it lately
00241   if (myLastTimeCheck.mSecSince() > 1000)
00242   {
00243     myLastTimeCheck.setToNow();
00244     for (it = myMap.begin(); it != myMap.end(); it++)
00245     {
00246       popupData = (*it).second;
00247       if ((timeout = popupData->myPopupInfo->getTimeout()) > 0 && 
00248           popupData->myStarted.secSince() >= timeout)
00249       {
00250         sendingPacket.empty();
00251         sendingPacket.byte4ToBuf((*it).first);
00252         sendingPacket.strToBuf(popupData->myPopupInfo->getTimeoutString()); 
00253         myServer->broadcastPacketTcp(&sendingPacket, "popupClose");
00254         doneIDs.push_back((*it).first);
00255         donePopups.push_back(popupData);
00256 
00257       }
00258     }
00259   }
00260   while (doneIDs.begin() != doneIDs.end())
00261   {
00262     myMap.erase((*doneIDs.begin()));
00263     doneIDs.pop_front();
00264   } 
00265   myDataMutex.unlock();
00266 
00267   std::list<PopupData *>::iterator donePopupIt;
00268   while ((donePopupIt = donePopups.begin()) != donePopups.end())
00269   {
00270     popupData = (*donePopupIt);
00271     if (popupData->myCallback != NULL)
00272       popupData->myCallback->invoke(popupData->myID, -1);
00273     delete popupData;
00274     donePopups.pop_front();
00275   } 
00276 }
00277 
00278 
00279 AREXPORT ArServerHandlerPopupInfo::ArServerHandlerPopupInfo(
00280         const char *ignoreIdentifier, const char *title, const char *message, 
00281         ArServerHandlerPopup::PopupType popupType,  
00282         ArTypes::Byte defaultButtonNumber, ArTypes::Byte escapeButtonNumber,
00283         int timeoutInSeconds, const char *timeoutString,
00284         const char *button0Label, const char *button0Pressed,
00285         const char *button1Label, const char *button1Pressed,
00286         const char *button2Label, const char *button2Pressed)
00287 {
00288   if (ignoreIdentifier != NULL)
00289     myIgnoreIdentifier = ignoreIdentifier;
00290   else
00291     myIgnoreIdentifier = "";
00292   if (title != NULL) 
00293     myTitle = title;
00294   else
00295     myTitle = "";
00296   if (message != NULL)
00297     myMessage = message;
00298   else
00299     myMessage = "";
00300   myPopupType = popupType; 
00301   myDefaultButtonNumber = defaultButtonNumber;
00302   myEscapeButtonNumber = escapeButtonNumber;
00303   myTimeout = timeoutInSeconds;
00304   if (timeoutString != NULL) 
00305     myTimeoutString = timeoutString;
00306   else
00307     myTimeoutString = "";
00308 
00309   if (button0Label != NULL)
00310     myButton0Label = button0Label;
00311   else
00312     myButton0Label = "";
00313   if (button0Pressed != NULL)
00314     myButton0Pressed = button0Pressed;
00315   else
00316     myButton0Pressed = "";
00317   if (button1Label != NULL)
00318     myButton1Label = button1Label;
00319   else
00320     myButton1Label = "";
00321   if (button1Pressed != NULL)
00322     myButton1Pressed = button1Pressed;
00323   else
00324     myButton1Pressed = "";
00325   if (button2Label != NULL)
00326     myButton2Label = button2Label;
00327   else
00328     myButton2Label = "";
00329   if (button2Pressed != NULL)
00330     myButton2Pressed = button2Pressed;
00331   else
00332     myButton2Pressed = "";
00333 
00334   /*
00335     printf("@@ 0l %s 0p %s 1l %s 1p %s 2l %s 2p%s\n", 
00336          myButton0Label.c_str(), myButton0Pressed.c_str(), 
00337          myButton1Label.c_str(), myButton1Pressed.c_str(), 
00338          myButton2Label.c_str(), myButton2Pressed.c_str());
00339   */
00340 }
00341 
00342 AREXPORT ArServerHandlerPopupInfo::~ArServerHandlerPopupInfo()
00343 {
00344 
00345 }
00346 
00347 AREXPORT ArServerHandlerPopupInfo::ArServerHandlerPopupInfo(
00348         const ArServerHandlerPopupInfo &popupInfo)
00349 {
00350   myIgnoreIdentifier = popupInfo.myIgnoreIdentifier;
00351   myTitle = popupInfo.myTitle;
00352   myMessage = popupInfo.myMessage;
00353   myPopupType = popupInfo.myPopupType;
00354   myDefaultButtonNumber = popupInfo.myDefaultButtonNumber; 
00355   myEscapeButtonNumber = popupInfo.myEscapeButtonNumber;
00356   myTimeout = popupInfo.myTimeout;
00357   myTimeoutString = popupInfo.myTimeoutString;
00358   myButton0Label = popupInfo.myButton0Label;
00359   myButton0Pressed = popupInfo.myButton0Pressed;
00360   myButton1Label = popupInfo.myButton1Label;
00361   myButton1Pressed = popupInfo.myButton1Pressed;
00362   myButton2Label = popupInfo.myButton2Label;
00363   myButton2Pressed = popupInfo.myButton2Pressed;
00364   /*
00365   printf("## 0l %s 0p %s 1l %s 1p %s 2l %s 2p%s\n", 
00366          myButton0Label.c_str(), myButton0Pressed.c_str(), 
00367          myButton1Label.c_str(), myButton1Pressed.c_str(), 
00368          myButton2Label.c_str(), myButton2Pressed.c_str());
00369   */
00370 }
00371 
00372 
00373 AREXPORT ArServerHandlerPopupInfo &ArServerHandlerPopupInfo::operator=(
00374         const ArServerHandlerPopupInfo &popupInfo)
00375 {
00376   if (this != &popupInfo)
00377   {
00378     myIgnoreIdentifier = popupInfo.myIgnoreIdentifier;
00379     myTitle = popupInfo.myTitle;
00380     myMessage = popupInfo.myMessage;
00381     myPopupType = popupInfo.myPopupType;
00382     myDefaultButtonNumber = popupInfo.myDefaultButtonNumber; 
00383     myEscapeButtonNumber = popupInfo.myEscapeButtonNumber;
00384     myTimeout = popupInfo.myTimeout;
00385     myTimeoutString = popupInfo.myTimeoutString;
00386     myButton0Label = popupInfo.myButton0Label;
00387     myButton0Pressed = popupInfo.myButton0Pressed;
00388     myButton1Label = popupInfo.myButton1Label;
00389     myButton1Pressed = popupInfo.myButton1Pressed;
00390     myButton2Label = popupInfo.myButton2Label;
00391     myButton2Pressed = popupInfo.myButton2Pressed;
00392   }
00393   return *this;
00394 }

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0