SUMO - Simulation of Urban MObility

TraCIServerAPI_GUI.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // APIs for getting/setting GUI values via TraCI
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #ifndef NO_TRACI
00031 
00032 #include <fx.h>
00033 #include <utils/gui/windows/GUIMainWindow.h>
00034 #include <utils/gui/windows/GUIGlChildWindow.h>
00035 #include <utils/gui/windows/GUISUMOAbstractView.h>
00036 #include <utils/gui/windows/GUIPerspectiveChanger.h>
00037 #include <utils/foxtools/MFXImageHelper.h>
00038 #include <traci-server/TraCIConstants.h>
00039 #include <guisim/GUINet.h>
00040 #include <guisim/GUIVehicle.h>
00041 #include "TraCIServerAPI_GUI.h"
00042 
00043 #ifdef CHECK_MEMORY_LEAKS
00044 #include <foreign/nvwa/debug_new.h>
00045 #endif // CHECK_MEMORY_LEAKS
00046 
00047 
00048 // ===========================================================================
00049 // used namespaces
00050 // ===========================================================================
00051 using namespace traci;
00052 
00053 
00054 // ===========================================================================
00055 // method definitions
00056 // ===========================================================================
00057 bool
00058 TraCIServerAPI_GUI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
00059                                tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
00060     std::string warning = ""; // additional description for response
00061     // variable & id
00062     int variable = inputStorage.readUnsignedByte();
00063     std::string id = inputStorage.readString();
00064     // check variable
00065     if (variable!=ID_LIST&&variable!=VAR_VIEW_ZOOM&&variable!=VAR_VIEW_OFFSET
00066             &&variable!=VAR_VIEW_SCHEMA&&variable!=VAR_VIEW_BOUNDARY) {
00067         server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "Get GUI Variable: unsupported variable specified", outputStorage);
00068         return false;
00069     }
00070     // begin response building
00071     tcpip::Storage tempMsg;
00072     //  response-code, variableID, objectID
00073     tempMsg.writeUnsignedByte(RESPONSE_GET_GUI_VARIABLE);
00074     tempMsg.writeUnsignedByte(variable);
00075     tempMsg.writeString(id);
00076     // process request
00077     if (variable==ID_LIST) {
00078         std::vector<std::string> ids = getMainWindow()->getViewIDs();
00079         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00080         tempMsg.writeStringList(ids);
00081     } else {
00082         GUISUMOAbstractView* v = getNamedView(id);
00083         if (v==0) {
00084             server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
00085             return false;
00086         }
00087         switch (variable) {
00088         case VAR_VIEW_ZOOM:
00089             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00090             tempMsg.writeDouble(v->getChanger().getZoom());
00091             break;
00092         case VAR_VIEW_OFFSET:
00093             tempMsg.writeUnsignedByte(POSITION_2D);
00094             tempMsg.writeDouble(v->getChanger().getXPos());
00095             tempMsg.writeDouble(v->getChanger().getYPos());
00096             break;
00097         case VAR_VIEW_SCHEMA: {
00098             FXComboBox& c = v->getColoringSchemesCombo();
00099             tempMsg.writeUnsignedByte(TYPE_STRING);
00100             tempMsg.writeString((std::string)c.getItem(c.getCurrentItem()).text());
00101             break;
00102         }
00103         case VAR_VIEW_BOUNDARY: {
00104             tempMsg.writeUnsignedByte(TYPE_BOUNDINGBOX);
00105             Boundary b = v->getVisibleBoundary();
00106             tempMsg.writeDouble(b.xmin());
00107             tempMsg.writeDouble(b.ymin());
00108             tempMsg.writeDouble(b.xmax());
00109             tempMsg.writeDouble(b.ymax());
00110             break;
00111         }
00112         default:
00113             break;
00114         }
00115     }
00116     server.writeStatusCmd(CMD_GET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
00117     server.writeResponseWithLength(outputStorage, tempMsg);
00118     return true;
00119 }
00120 
00121 
00122 bool
00123 TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00124                                tcpip::Storage& outputStorage) throw(TraCIException, std::invalid_argument) {
00125     std::string warning = ""; // additional description for response
00126     // variable
00127     int variable = inputStorage.readUnsignedByte();
00128     if (variable!=VAR_VIEW_ZOOM&&variable!=VAR_VIEW_OFFSET&&variable!=VAR_VIEW_SCHEMA&&variable!=VAR_VIEW_BOUNDARY
00129             &&variable!=VAR_SCREENSHOT&&variable!=VAR_TRACK_VEHICLE
00130        ) {
00131         server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Change GUI State: unsupported variable specified", outputStorage);
00132         return false;
00133     }
00134     // id
00135     std::string id = inputStorage.readString();
00136     GUISUMOAbstractView* v = getNamedView(id);
00137     if (v==0) {
00138         server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "View '" + id + "' is not known", outputStorage);
00139         return false;
00140     }
00141     // process
00142     int valueDataType = inputStorage.readUnsignedByte();
00143     switch (variable) {
00144     case VAR_VIEW_ZOOM:
00145         if (valueDataType!=TYPE_DOUBLE) {
00146             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The zoom must be given as a double.", outputStorage);
00147             return false;
00148         }
00149         v->setViewport(inputStorage.readDouble(), v->getChanger().getXPos(), v->getChanger().getYPos());
00150         break;
00151     case VAR_VIEW_OFFSET: {
00152         if (valueDataType!=POSITION_2D) {
00153             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The view port must be given as a position.", outputStorage);
00154             return false;
00155         }
00156         v->setViewport(v->getChanger().getZoom(), inputStorage.readDouble(), v->getChanger().getYPos());
00157         v->setViewport(v->getChanger().getZoom(), v->getChanger().getXPos(), inputStorage.readDouble());
00158     }
00159     break;
00160     case VAR_VIEW_SCHEMA:
00161         if (valueDataType!=TYPE_STRING) {
00162             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme must be specified by a string.", outputStorage);
00163             return false;
00164         }
00165         if (!v->setColorScheme(inputStorage.readString())) {
00166             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The scheme is not known.", outputStorage);
00167             return false;
00168         }
00169         break;
00170     case VAR_VIEW_BOUNDARY: {
00171         if (valueDataType!=TYPE_BOUNDINGBOX) {
00172             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "The boundary must be specified by a bounding box.", outputStorage);
00173             return false;
00174         }
00175         const SUMOReal xmin = inputStorage.readDouble();
00176         const SUMOReal ymin = inputStorage.readDouble();
00177         const SUMOReal xmax = inputStorage.readDouble();
00178         const SUMOReal ymax = inputStorage.readDouble();
00179         Boundary b(xmin, ymin, xmax, ymax);
00180         v->centerTo(b);
00181         break;
00182     }
00183     case VAR_SCREENSHOT: {
00184         if (valueDataType!=TYPE_STRING) {
00185             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Making a snapshot requires a file name.", outputStorage);
00186             return false;
00187         }
00188         std::string filename = inputStorage.readString();
00189         std::string error = v->makeSnapshot(filename);
00190         if (error!="") {
00191             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, error, outputStorage);
00192             return false;
00193         }
00194     }
00195     break;
00196     case VAR_TRACK_VEHICLE: {
00197         if (valueDataType!=TYPE_STRING) {
00198             server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Tracking requires a string vehicle ID.", outputStorage);
00199             return false;
00200         }
00201         std::string id = inputStorage.readString();
00202         if (id=="") {
00203             v->stopTrack();
00204         } else {
00205             SUMOVehicle* veh = MSNet::getInstance()->getVehicleControl().getVehicle(id);
00206             if (veh==0) {
00207                 server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_ERR, "Could not find vehicle '" + id + "'.", outputStorage);
00208                 return false;
00209             }
00210             if (!static_cast<GUIVehicle*>(veh)->hasActiveAddVisualisation(v, GUIVehicle::VO_TRACKED)) {
00211                 v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
00212                 static_cast<GUIVehicle*>(veh)->addActiveAddVisualisation(v, GUIVehicle::VO_TRACKED);
00213             }
00214         }
00215     }
00216     default:
00217         break;
00218     }
00219     server.writeStatusCmd(CMD_SET_GUI_VARIABLE, RTYPE_OK, warning, outputStorage);
00220     return true;
00221 }
00222 
00223 
00224 GUIMainWindow*
00225 TraCIServerAPI_GUI::getMainWindow() throw() {
00226     FXWindow* w = FXApp::instance()->getRootWindow()->getFirst();
00227     while (w!=0&&dynamic_cast<GUIMainWindow*>(w)==0) {
00228         w = w->getNext();
00229     }
00230     if (w==0) {
00231         // main window not found
00232         return 0;
00233     }
00234     return dynamic_cast<GUIMainWindow*>(w);
00235 }
00236 
00237 
00238 GUISUMOAbstractView* const
00239 TraCIServerAPI_GUI::getNamedView(const std::string& id) throw() {
00240     GUIMainWindow* mw = static_cast<GUIMainWindow*>(getMainWindow());
00241     if (mw==0) {
00242         return 0;
00243     }
00244     GUIGlChildWindow* c = static_cast<GUIGlChildWindow*>(mw->getViewByID(id));
00245     if (c==0) {
00246         return 0;
00247     }
00248     return c->getView();
00249 }
00250 
00251 
00252 #endif
00253 
00254 
00255 /****************************************************************************/
00256 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines