00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "Python.h"
00032 #include "structmember.h"
00033
00034 #include "MobileNode.h"
00035 #include "RandomWaypointNode.h"
00036 #include "parsers.h"
00037 #include "simlpy/interpreter_hooks.h"
00038 #include "simlpy/Clock.h"
00039 #include "simlpy/SimulationException.h"
00040
00041 #include <iostream>
00042 #include <fstream>
00043
00044 using namespace Mobility;
00045
00068 #ifndef PyMODINIT_FUNC
00071 #define PyMODINIT_FUNC extern "C" void
00072 #endif
00073
00077 static char mobility_set_waypoint_doc[] =
00078 "Set a waypoint for a mobile node.\n\
00079 \n\
00080 Calling syntax:\n\
00081 \n\
00082 pydtn.mobility.set_waypoint(<mnode>,<time>,<pause>,<pos>)\n\
00083 \n\
00084 <mnode> : an existing mobile node\n\
00085 <time> : the time at which <mnode> should reach the waypoint,\n\
00086 given as\n\
00087 [ seconds , microseconds ]\n\
00088 <pause> : the length of time at which <mnode> should pause\n\
00089 once the waypoint has been reached, given as\n\
00090 [ seconds , microseconds ]\n\
00091 <pos> : the waypoint as a list of two or three coordinates\n\
00092 If the list is length 2, then the z coordinate is\n\
00093 taken to be 0. (meters, real-valued)\n\
00094 \n\
00095 This method is useful for scheduling a number of waypoints before\n\
00096 the simulation begins.\n";
00097
00104 static PyObject*
00105 mobility_set_waypoint( PyObject* self , PyObject* args )
00106 {
00107 Entity::ArgList argList;
00108 build_arglist( argList, args );
00109
00110 unsigned int nargs = argList.size();
00111 if ( nargs < 4 )
00112 {
00113 std::cerr << "set_waypoint() takes 4 arguments ("
00114 << nargs << " given)"
00115 << std::endl;
00116 return 0;
00117 }
00118 MobileNode* mn = 0;
00119 try
00120 {
00121 ItemWrapper item = resolve_symbol( argList[0] );
00122 if ( 0 != item.type.find( MobileNode::kIdentifier ) )
00123 {
00124 throw SimulationException( __FILE__ , __LINE__ );
00125 }
00126 mn = (MobileNode*)(item.item);
00127 if ( 0 == mn )
00128 {
00129 throw SimulationException( __FILE__ , __LINE__ );
00130 }
00131 }
00132 catch ( ... )
00133 {
00134 std::cerr << "The first argument of set_waypoint() must be a mobile node"
00135 << std::endl;
00136 throw;
00137 }
00138
00139 Time t = parse_time(argList[1]);
00140 Time d = parse_time(argList[2]);
00141
00142 Position pos = MobileNode::parse_position( argList[3] );
00143
00144 mn->setWaypoint( Presence( t, pos, d ) );
00145
00146 Py_INCREF(Py_None);
00147 return Py_None;
00148 }
00149
00152 static PyMethodDef ExampleMethods[] =
00153 {
00154 {"set_waypoint",mobility_set_waypoint,METH_VARARGS,
00155 mobility_set_waypoint_doc},
00156 {0,0,0,0}
00157 };
00158
00162 PyMODINIT_FUNC
00163 init_mobility(void)
00164 {
00165 add_entity( new Mobility::MobileNode );
00166 add_entity( new Mobility::RandomWaypointNode );
00167 (void) Py_InitModule("_mobility",ExampleMethods);
00168 }
00169