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 "BundleCollector.h"
00035 #include "pydtn/WrapNode.h"
00036 #include "pydtn/parsers.h"
00037 #include "simlpy/interpreter_hooks.h"
00038 #include "simlpy/Clock.h"
00039 
00040 #include <iostream>
00041 #include <fstream>
00042 
00102 #ifndef PyMODINIT_FUNC
00105 #define PyMODINIT_FUNC extern "C" void
00106 #endif
00107 
00109 static std::ostream* _stream = &(std::cout);
00110 
00114 static char storeprofile_setup_doc[] = "Storage profiling configuration.\n\
00115    \n\
00116    This method should be called if stable storage profiling is desired.\n\
00117    \n\
00118    Calling syntax:\n\
00119    \n\
00120    pydtn.storeprofile.setup(<fname>)\n\
00121    \n\
00122       <fname> is either a filename or one of 'stdout' or 'stderr'.\n\
00123       In the former case, the profiling information is written\n\
00124       to the file named.  In the latter cases, the information is\n\
00125       written to the appropriate stream.\n";
00126 
00134 static PyObject*
00135 storeprofile_setup( PyObject* self, PyObject* args )
00136 {
00137    const char* fname;
00138    if ( ! PyArg_ParseTuple(args, "s", &fname) )
00139    {
00140       std::cerr << "Incorrect argument list" << std::endl;
00141       return 0;
00142    }
00143    std::string fnstr( fname ); // This lets us use operator==.
00144 
00145    if ( "stdout" == fnstr )
00146    {
00147       _stream = &(std::cout);
00148    }
00149    else if ( "stderr" == fnstr )
00150    {
00151       _stream = &(std::cerr);
00152    }
00153    else
00154    {
00155       _stream = new std::ofstream(fname);
00156    }
00157 
00158    Py_INCREF(Py_None);
00159    return Py_None;
00160 }
00161 
00165 static char storeprofile_collect_doc[] = "Add profiling to a node.\n\
00166    \n\
00167    Calling syntax:\n\
00168    \n\
00169    pydtn.storeprofile.collect(<n>)\n\
00170    \n\
00171       <n> is a node for which we want to collect a profile.\n";
00172 
00179 static PyObject*
00180 storeprofile_collect( PyObject* self, PyObject* args )
00181 {
00182    if ( ! PyTuple_Check( args ) )
00183    {
00184       PyErr_SetString(PyExc_TypeError,"collect expects a list of arguments");
00185       return 0;
00186    }
00187    if ( 0 == PyTuple_Size( args ) )
00188    {
00189       PyErr_SetString(PyExc_TypeError,
00190                       "collect expects a non-empty argument list");
00191       return 0;
00192    }
00193    PyObject* p = PyTuple_GetItem( args, 0 );
00194    WrapNode* wn = parse_node(p);
00195    if ( 0 == wn )
00196    {
00197       PyErr_SetString(PyExc_TypeError,
00198                       "collect expects a node");
00199       return 0;
00200    }
00201    wn->addCollector( new StoreProfile::BundleCollector( *_stream ) );
00202    Py_INCREF(Py_None);
00203    return Py_None;
00204 }
00205 
00208 static PyMethodDef ExampleMethods[] =
00209 {
00210    {"setup",storeprofile_setup,METH_VARARGS,storeprofile_setup_doc},
00211    {"collect",storeprofile_collect,METH_VARARGS,storeprofile_collect_doc},
00212    {0,0,0,0} // sentinel
00213 };
00214 
00218 PyMODINIT_FUNC
00219 init_storeprofile(void)
00220 {
00221    (void) Py_InitModule("_storeprofile",ExampleMethods);
00222 }
00223 

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