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 "EpidemicStore.h"
00035 #include "EpidemicApp.h"
00036 #include "EpidemicForwarding.h"
00037 #include "EpidemicCustody.h"
00038 
00039 #include "mobility/MobileNode.h"
00040 #include "mobility/WirelessLink.h"
00041 #include "pydtn/parsers.h"
00042 #include "mobility/parsers.h"
00043 #include "simlpy/interpreter_hooks.h"
00044 #include "simlpy/Clock.h"
00045 
00052 #ifndef PyMODINIT_FUNC
00055 #define PyMODINIT_FUNC extern "C" void
00056 #endif
00057 
00059 static uint32_t epidemic_max_hops = 0;
00060 
00064 static char epidemic_set_max_hops_doc[] =
00065 "Set the maximum number of allowed hops.\n\
00066 \n\
00067 This takes one integer argument, which will set the number\n\
00068 of hops that a bundle may take via epidemic forwarding\n\
00069 before further propagation is limited to the destination\n\
00070 node.  The default value is 0, which is unlimited forwarding.\n";
00071 
00078 static PyObject*
00079 epidemic_set_max_hops( PyObject* self, PyObject* args )
00080 {
00081    if ( ! PyTuple_Check( args ) )
00082    {
00083       PyErr_SetString(PyExc_TypeError,
00084                       "set_max_hops expects a list of arguments");
00085       return 0;
00086    }
00087    if ( 0 == PyTuple_Size( args ) )
00088    {
00089       PyErr_SetString(PyExc_TypeError,
00090                       "set_max_hops expects an argument");
00091       return 0;
00092    }
00093 
00094    try
00095    {
00096       long int val = parse_long( PyTuple_GetItem(args,0) );
00097       epidemic_max_hops = val;
00098    }
00099    catch ( ... )
00100    {
00101       PyErr_SetString(PyExc_TypeError,"integer expected");
00102       return 0;
00103    }
00104 
00105    Py_INCREF(Py_None);
00106    return Py_None;
00107 }
00108 
00112 static char epidemic_attach_doc[] = "Add an application instance to a node.\n\
00113    \n\
00114    Calling syntax:\n\
00115    \n\
00116    pydtn.mobility.epidemic.attach(<n>,<l>)\n\
00117    \n\
00118       <n> is a mobile node to which we want to add an instance.\n\
00119       <l> is the wireless link for the node.\n\
00120    \n\
00121    In general, this will be called automatically at node creation by\n\
00122    pydtn.mobility.node()\n";
00123 
00131 static PyObject*
00132 epidemic_attach( PyObject* self, PyObject* args )
00133 {
00134    if ( ! PyTuple_Check( args ) )
00135    {
00136       PyErr_SetString(PyExc_TypeError,"attach expects a list of arguments");
00137       return 0;
00138    }
00139    if ( PyTuple_Size( args ) < 2 )
00140    {
00141       PyErr_SetString(PyExc_TypeError,
00142                       "attach expects two arguments");
00143       return 0;
00144    }
00145 
00146    Mobility::MobileNode* mn = Mobility::parse_mn( PyTuple_GetItem(args,0) );
00147    if ( 0 == mn ) return 0;
00148 
00149    Mobility::WirelessLink* wl = Mobility::parse_wl( PyTuple_GetItem(args,1) );
00150    if ( 0 == wl ) return 0;
00151 
00152    Epidemic::EpidemicStore* ps =
00153       new Epidemic::EpidemicStore( &(mn->node()), epidemic_max_hops );
00154    Epidemic::EpidemicApp* ma =
00155       new Epidemic::EpidemicApp( ps, &(wl->link()), mn->bundleLifetime().tv );
00156    Epidemic::EpidemicForwarding* fp =
00157       new Epidemic::EpidemicForwarding( &(mn->node()), &(wl->link()), ma );
00158    
00159    mn->setPersistentStore(ps);
00160    mn->addMobileApp(ma);
00161    mn->setForwardingPolicy(fp);
00162    mn->node().setCustodyPolicy( new Epidemic::EpidemicCustody(&(mn->node())) );
00163 
00164 
00165    Py_INCREF(Py_None);
00166    return Py_None;
00167 }
00168 
00171 static PyMethodDef ExampleMethods[] =
00172 {
00173    {"set_max_hops",epidemic_set_max_hops,METH_VARARGS,epidemic_set_max_hops_doc},
00174    {"attach",epidemic_attach,METH_VARARGS,epidemic_attach_doc},
00175    {0,0,0,0} // sentinel
00176 };
00177 
00181 PyMODINIT_FUNC
00182 init_epidemic(void)
00183 {
00184    (void) Py_InitModule("_epidemic",ExampleMethods);
00185 }
00186 

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