rmst_sink.cc

Go to the documentation of this file.
00001 //
00002 // rmst_sink.cc    : RmstSink Class Methods
00003 // authors         : Fred Stann
00004 //
00005 // Copyright (C) 2003 by the University of Southern California
00006 // $Id: rmst_sink.cc,v 1.3 2005/09/13 04:53:46 tomh Exp $
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License,
00010 // version 2, as published by the Free Software Foundation.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this program; if not, write to the Free Software Foundation, Inc.,
00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020 //
00021 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
00042 
00043 #include "rmst_sink.hh"
00044 
00045 #ifdef NS_DIFFUSION
00046 static class RmstSnkClass : public TclClass {
00047 public:
00048   RmstSnkClass() : TclClass("Application/DiffApp/RmstSink") {}
00049     TclObject* create(int , const char*const* ) {
00050         return(new RmstSink());
00051     }
00052 } class_rmst_sink;
00053 
00054 int RmstSink::command(int argc, const char*const* argv) 
00055 {
00056   if (argc == 2) {
00057     if (strcmp(argv[1], "subscribe") == 0) {
00058       run();
00059       return TCL_OK;
00060     }
00061    }
00062   return DiffApp::command(argc, argv);
00063 }
00064 
00065 #endif // NS_DIFFUSION
00066 
00067 void RmstSnkReceive::recv(NRAttrVec *data, NR::handle my_handle)
00068 {
00069   NRSimpleAttribute<int> *rmst_id_attr = NULL;
00070   NRSimpleAttribute<void *> *data_buf_attr = NULL;
00071   timeval cur_time;
00072   int rmst_no;
00073   int size;
00074   void *blob_ptr;
00075 
00076   printf("RMST-SNK::recv callback got an NRAttrVec.\n");
00077   GetTime (&cur_time);
00078   printf("  time: sec = %d\n", (unsigned int) cur_time.tv_sec);
00079 
00080   rmst_id_attr = RmstIdAttr.find(data);
00081   data_buf_attr = RmstDataAttr.find(data);
00082 
00083   rmst_no = rmst_id_attr->getVal();
00084   blob_ptr = data_buf_attr->getVal();
00085   size = data_buf_attr->getLen();
00086 
00087   printf("  Got a blob, rmstId = %d, size = %d\n", rmst_no, size);
00088   snk_->recv((void *)blob_ptr, size);
00089 }
00090 
00091 void RmstSink::run() {
00092   rmst_handle_ = setupInterest();
00093 #ifndef NS_DIFFUSION
00094   while (1){
00095       sleep (1000);
00096   }
00097 #endif // !NS_DIFFUSION
00098 }  
00099 
00100 void RmstSink::recv(void *blob, int size) {
00101   int i;
00102   char *tmpPtr = (char*)blob;
00103   printf("  Sink received a large blob - size = %d\n", size);
00104   for(i=0; i<size; i+=50){
00105     printf("%s\n", &tmpPtr[i]);
00106   }
00107   no_rec_++;
00108   if(no_rec_ >= blobs_to_rec_){
00109     printf("  Sink unsubscribes\n");
00110     dr_->unsubscribe(rmst_handle_);
00111   }
00112 }
00113 
00114 handle RmstSink::setupInterest()
00115 {
00116   NRAttrVec attrs;
00117 
00118   printf("RMST-SNK::subscribing to all PCM_SAMPLEs\n");
00119 
00120   attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::INTEREST_CLASS));
00121   attrs.push_back(RmstTsprtCtlAttr.make(NRAttribute::EQ, RMST_RESP));
00122   attrs.push_back(RmstTargetAttr.make(NRAttribute::EQ, "PCM_SAMPLE"));
00123 
00124   handle h = dr_->subscribe(&attrs, mr);
00125 
00126   ClearAttrs(&attrs);
00127   return h;
00128 }
00129 
00130 #ifdef NS_DIFFUSION
00131 RmstSink::RmstSink() : blobs_to_rec_(4) {
00132 #else
00133 RmstSink::RmstSink(int argc, char **argv) : blobs_to_rec_(4) {
00134 #endif // NS_DIFFUSION
00135   
00136   mr = new RmstSnkReceive(this);  
00137   
00138 #ifndef NS_DIFFUSION
00139   parseCommandLine(argc, argv);
00140   dr_ = NR::createNR(diffusion_port_);
00141 #endif // !NS_DIFFUSION
00142   no_rec_ = 0;
00143 }
00144 
00145 #ifndef NS_DIFFUSION
00146 int main(int argc, char **argv){
00147   RmstSink *app;
00148 
00149   app = new RmstSink(argc, argv);
00150   app->run();
00151   return 0;
00152 }
00153 #endif // !NS_DIFFUSION

Generated on Tue Mar 6 16:47:50 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6