python_init.cc

00001 // Copyright 2008 Michael Marsh, University of Maryland.
00002 //
00003 // This file is part of pydtn.
00004 //
00005 // pydtn is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // pydtn is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with pydtn.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // The views and conclusions contained in the software and documentation
00019 // are those of the authors and should not be interpreted as representing
00020 // official policies, either expressed or implied, of the University
00021 // of Maryland.
00022 //
00023 // pydtn extends and embeds the Python interpreter, which is
00024 // Copyright 2001-2006 Python Software Foundation, All Rights Reserved,
00025 // and is released under the PSF License Agreement.
00026 //
00027 // RANLUX random number generation uses the Boost library,
00028 // Copyright 1994-2006 by various authors (details in individual files),
00029 // which is released under the Boost Software License, Version 1.0.
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} // sentinel
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 

Generated on Mon Mar 24 11:15:45 2008 for Pydtn Simulator by  doxygen 1.5.4