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

ArServerInfoDrawings.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 "ArServerInfoDrawings.h"
00029 
00030 AREXPORT ArServerInfoDrawings::ArServerInfoDrawings(ArServerBase *server) :
00031   myNetListDrawingsCB(this, &ArServerInfoDrawings::netListDrawings),
00032   myNetGetDrawingListCB(this, &ArServerInfoDrawings::netGetDrawingList)
00033 {
00034   myServer = server;
00035 
00036   if (myServer != NULL)
00037   {
00038     myServer->addData("listDrawings", 
00039                       "Deprecated; use getDrawingList instead. Gets a list of things that can be drawn",
00040                       &myNetListDrawingsCB, 
00041                       "none", 
00042                       "byte4: numDrawings, <repeats numDrawings> string: name, string: shape, byte4: primaryColor(0RGB), byte4: size, byte4: layer, byte4: defaultRefreshTime(0 == don't request refresh), byte4: secondaryColor(0RGB)", "SensorInfo", "RETURN_SINGLE");
00043      myServer->addData("getDrawingList", 
00044                       "Gets a list of items to be drawn on the map. Each packet contains one drawing.  Terminates on empty packet.",
00045                       &myNetGetDrawingListCB, 
00046                       "none", 
00047           "For each drawing, a packet that contains: string: name, string: shape, byte4: primaryColor(0RGB), byte4: size, byte4: layer, byte4: defaultRefreshTime(0 == don't request refresh), byte4: secondaryColor(0RGB), string: visibility", 
00048           "SensorInfo", "RETURN_UNTIL_EMPTY");
00049  }
00050 
00051 }
00052 
00053 AREXPORT ArServerInfoDrawings::~ArServerInfoDrawings() 
00054 {
00055 
00056 }
00057 
00071 AREXPORT bool ArServerInfoDrawings::addDrawing(ArDrawingData *drawingData, 
00072                                            const char *name,
00073                          ArFunctor2<ArServerClient *, ArNetPacket *> *functor)
00074 {
00075   if (myDrawingDatas.find(name) != myDrawingDatas.end() || 
00076       myDrawingCallbacks.find(name) != myDrawingCallbacks.end())
00077   {
00078     ArLog::log(ArLog::Normal, "ArServerInfoDrawings::addDrawing: Already a drawing of name '%s'", name);
00079     return false;
00080   }
00081   if (myServer == NULL || 
00082       !myServer->addData(name, "", functor, "none", 
00083                         "depends on shape, see MobileEyes docs", "SensorInfo",
00084                          "RETURN_SINGLE"))
00085   {
00086     ArLog::log(ArLog::Normal, 
00087                "ArServerInfoDrawings::addDrawing: Could not add data of name '%s' to server", 
00088                name);
00089     return false;
00090   }
00091   myDrawingDatas[name] = drawingData;
00092   myDrawingCallbacks[name] = functor;
00093   return true;
00094 }
00095 
00096 AREXPORT void ArServerInfoDrawings::netListDrawings(ArServerClient *client, 
00097                                                     ArNetPacket *packet)
00098 {
00099   ArNetPacket sendingPacket;
00100   std::map<std::string, ArDrawingData *, ArStrCaseCmpOp>::iterator it;
00101   
00102   sendingPacket.byte4ToBuf(myDrawingDatas.size());
00103   for (it = myDrawingDatas.begin(); it != myDrawingDatas.end(); it++)
00104   {
00105     sendingPacket.strToBuf((*it).first.c_str());
00106     sendingPacket.strToBuf((*it).second->getShape());
00107     sendingPacket.byte4ToBuf((*it).second->getPrimaryColor().colorToByte4());
00108     sendingPacket.byte4ToBuf((*it).second->getSize());
00109     sendingPacket.byte4ToBuf((*it).second->getLayer());
00110     sendingPacket.uByte4ToBuf((*it).second->getDefaultRefreshTime());
00111     sendingPacket.byte4ToBuf((*it).second->getSecondaryColor().colorToByte4());
00112   }
00113   client->sendPacketTcp(&sendingPacket);
00114 }
00115 
00116 AREXPORT void ArServerInfoDrawings::netGetDrawingList(ArServerClient *client, 
00117                                                     ArNetPacket *packet)
00118 {
00119   ArNetPacket sendingPacket;
00120   
00121   // TODO: Any need to protect the map by a mutex?
00122 
00123   for (std::map<std::string, ArDrawingData *, ArStrCaseCmpOp>::iterator it = 
00124                 myDrawingDatas.begin(); 
00125        it != myDrawingDatas.end(); 
00126        it++)
00127   {
00128     sendingPacket.empty();
00129 
00130     sendingPacket.strToBuf((*it).first.c_str());
00131     sendingPacket.strToBuf((*it).second->getShape());
00132     sendingPacket.byte4ToBuf((*it).second->getPrimaryColor().colorToByte4());
00133     sendingPacket.byte4ToBuf((*it).second->getSize());
00134     sendingPacket.byte4ToBuf((*it).second->getLayer());
00135     sendingPacket.uByte4ToBuf((*it).second->getDefaultRefreshTime());
00136     sendingPacket.byte4ToBuf((*it).second->getSecondaryColor().colorToByte4());
00137     sendingPacket.strToBuf((*it).second->getVisibility());
00138 
00139     client->sendPacketTcp(&sendingPacket);
00140 
00141   } // end for each drawing
00142 
00143   sendingPacket.empty();
00144   client->sendPacketTcp(&sendingPacket);
00145 }
00146 
00147 AREXPORT bool ArServerInfoDrawings::addRangeDevice(ArRangeDevice *rangeDevice)
00148 {
00149   bool ret = true;
00150   char name[512];
00151   if (myServer == NULL)
00152   {
00153     ArLog::log(ArLog::Normal, "ArServerInfoDrawings::addRangeDeviceForDrawing: srever is NULL");
00154     return false;
00155   }
00156   sprintf(name, "%sCurrent", rangeDevice->getName());
00157   // LEAK a little when called, shouldn't be called more than a few
00158   // times and isn't a leak unless its called more than once
00159   if (rangeDevice->getCurrentDrawingData() != NULL && 
00160       !addDrawing(rangeDevice->getCurrentDrawingData(), name, 
00161                   new ArFunctor3C<ArServerInfoDrawings, 
00162                   ArServerClient *, ArNetPacket *, 
00163                   ArRangeDevice *>(this, &ArServerInfoDrawings::netRangeDeviceCurrent, NULL, NULL, rangeDevice)))
00164   {
00165     ArLog::log(ArLog::Normal, 
00166                "ArServerInfoDrawings::addRangeDevice: Could not add data for range device '%s' to server ('%s')", rangeDevice->getName(), name);
00167     ret = false;
00168   }
00169   sprintf(name, "%sCumulative", rangeDevice->getName());
00170   if (rangeDevice->getCumulativeDrawingData() != NULL && 
00171       !addDrawing(rangeDevice->getCumulativeDrawingData(), name, 
00172                   new ArFunctor3C<ArServerInfoDrawings, 
00173                   ArServerClient *, ArNetPacket *, 
00174                   ArRangeDevice *>(this, &ArServerInfoDrawings::netRangeDeviceCumulative, NULL, NULL, rangeDevice)))
00175   {
00176     ArLog::log(ArLog::Normal, 
00177                "ArServerInfoDrawings::addRangeDevice: Could not add data for range device '%s' to server ('%s')", rangeDevice->getName(), name);
00178     ret = false;
00179   }
00180   return ret;
00181 }
00182 
00183 AREXPORT bool ArServerInfoDrawings::addRobotsRangeDevices(ArRobot *robot)
00184 {
00185   std::list<ArRangeDevice *>::iterator it;
00186   bool ret = true;
00187   ArRangeDevice *device;
00188   if (robot == NULL || robot->getRangeDeviceList() == NULL)
00189   {
00190     ArLog::log(ArLog::Terse, "InfoDrawings::addRobotsRangeDevices: Robot or robot's range device list is NULL");
00191     return false;
00192   }
00193   for (it = robot->getRangeDeviceList()->begin();
00194        it != robot->getRangeDeviceList()->end();
00195        it++)
00196   {
00197     device = (*it);
00198     device->lockDevice();
00199     if (!addRangeDevice(device))
00200       ret = false;
00201     device->unlockDevice();
00202   }
00203   return ret;
00204 }
00205 
00206 AREXPORT void ArServerInfoDrawings::netRangeDeviceCurrent(
00207         ArServerClient *client, ArNetPacket *packet, ArRangeDevice *device)
00208 {
00209   ArNetPacket sendPacket;
00210   std::list<ArPoseWithTime *> *readings;
00211   std::list<ArPoseWithTime *>::iterator it;
00212 
00213   device->lockDevice();
00214   readings = device->getCurrentBuffer();
00215   if (readings == NULL)
00216   {
00217     ArLog::log(ArLog::Verbose, "ArServerInfoDrawing::netRangeDeviceCurrent: No current buffer for %s", device->getName());
00218     device->unlockDevice();
00219     sendPacket.byte4ToBuf(0);
00220     client->sendPacketUdp(&sendPacket);
00221     return;
00222   } 
00223   
00224   sendPacket.byte4ToBuf(readings->size());
00225   for (it = readings->begin(); it != readings->end(); it++)
00226   {
00227     sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getX()));
00228     sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getY()));
00229   }
00230   device->unlockDevice();
00231   client->sendPacketUdp(&sendPacket);
00232 
00233 }
00234 
00235 
00236 AREXPORT void ArServerInfoDrawings::netRangeDeviceCumulative(
00237         ArServerClient *client, ArNetPacket *packet, ArRangeDevice *device)
00238 {
00239   ArNetPacket sendPacket;
00240   std::list<ArPoseWithTime *> *readings;
00241   std::list<ArPoseWithTime *>::iterator it;
00242 
00243   device->lockDevice();
00244   readings = device->getCumulativeBuffer();
00245   if (readings == NULL)
00246   {
00247     ArLog::log(ArLog::Verbose, "ArServerInfoDrawing::netRangeDeviceCumulative: No cumulative buffer for %s", device->getName());
00248     device->unlockDevice();
00249     sendPacket.byte4ToBuf(0);
00250     client->sendPacketUdp(&sendPacket);
00251     return;
00252   } 
00253   
00254   sendPacket.byte4ToBuf(readings->size());
00255   for (it = readings->begin(); it != readings->end(); it++)
00256   {
00257     sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getX()));
00258     sendPacket.byte4ToBuf(ArMath::roundInt((*it)->getY()));
00259   }
00260   device->unlockDevice();
00261   client->sendPacketUdp(&sendPacket);
00262 
00263 }

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