rmst_source.cc

Go to the documentation of this file.
00001 //
00002 // rmst_source.cc  : RmstSource Class Methods
00003 // authors         : Fred Stann
00004 //
00005 // Copyright (C) 2003 by the University of Southern California
00006 // $Id: rmst_source.cc,v 1.4 2005/09/13 04:53:47 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_source.hh"
00044 #include <unistd.h>
00045 
00046 #ifdef NS_DIFFUSION
00047 static class RmstSrcClass : public TclClass {
00048 public:
00049   RmstSrcClass() : TclClass("Application/DiffApp/RmstSource") {}
00050     TclObject* create(int , const char*const* ) {
00051         return(new RmstSource());
00052     }
00053 } class_rmst_source;
00054 
00055 void RmstSendDataTimer::expire(Event *e) {
00056   a_->send();
00057 }
00058 
00059 void RmstSource::send()
00060 {
00061   int sleep_interval;
00062   bool sent_first_blob = false;
00063 
00064   if (num_subscriptions_ > 0){
00065       if (!sent_first_blob){
00066         sendBlob();
00067         sent_first_blob = true;
00068         sleep_interval = 100;
00069       }
00070       else
00071         printf("RMST-SRC::sees subscriptions\n");
00072     }
00073     else{
00074       printf("RMST-SRC::sees no subscriptions\n");
00075       sleep_interval = 10;
00076     }
00077   // re-schedule the timer 
00078   sdt_.resched(sleep_interval);
00079 }
00080 
00081 int RmstSource::command(int argc, const char*const* argv) 
00082 {
00083   if (argc == 2) {
00084     if (strcmp(argv[1], "subscribe") == 0) {
00085       run();
00086       return TCL_OK;
00087     }
00088    }
00089   return DiffApp::command(argc, argv);
00090 }
00091 #endif // NS_DIFFUSION
00092 
00093 
00094 void RmstSrcReceive::recv(NRAttrVec *data, NR::handle my_handle)
00095 {
00096   NRSimpleAttribute<char*> *rmst_target_attr = NULL;
00097   NRSimpleAttribute<int> *nr_class = NULL;
00098   NRSimpleAttribute<int> *tsprt_ctl_attr = NULL;
00099 
00100   timeval cur_time;
00101 
00102   printf("RMST-SRC::recv got an attr vector.");
00103   GetTime(&cur_time);
00104   printf("  time: sec = %d\n", (unsigned int) cur_time.tv_sec);
00105 
00106   nr_class = NRClassAttr.find(data);
00107   tsprt_ctl_attr = RmstTsprtCtlAttr.find(data);
00108 
00109   if (nr_class){
00110     switch (nr_class->getVal()){
00111 
00112     case NRAttribute::INTEREST_CLASS:
00113       if (tsprt_ctl_attr && (tsprt_ctl_attr->getVal() == RMST_RESP)){
00114         printf("  Source received an INTEREST message\n");
00115         src_->num_subscriptions_++;
00116       }
00117       break;
00118 
00119     case NRAttribute::DISINTEREST_CLASS:
00120       src_->num_subscriptions_--;
00121       rmst_target_attr = RmstTargetAttr.find(data);
00122       if (rmst_target_attr){
00123         printf("  Source received a DISINTEREST for %s\n",
00124             rmst_target_attr->getVal());
00125       }
00126       else
00127         printf("  Source received a Disinterest message for unknown Interest!\n");
00128       break;
00129 
00130     default:
00131       printf("  Source received an unknown or inappropriate class!(%d)!\n",
00132           nr_class->getVal());
00133       break;
00134     }
00135   }
00136 
00137   if (tsprt_ctl_attr){
00138     switch (tsprt_ctl_attr->getVal()){
00139     case RMST_RESP:
00140       break;
00141     case RMST_CONT:
00142       printf("  Source received a RMST_CONT message\n");
00143       if(src_->blobs_to_send_ > 0){
00144         printf ("  Source sending another blob\n");
00145         src_->sendBlob();
00146       }
00147       else
00148         printf ("  Source done sending blobs\n");
00149       break;
00150     default:
00151       printf("  Source received an unexpected RmstTsprtCtlAttr (%d)!\n",
00152            tsprt_ctl_attr->getVal());
00153       break;
00154     }
00155   }
00156 }
00157 
00158 #ifdef NS_DIFFUSION
00159 RmstSource::RmstSource() : blobs_to_send_(4), sdt_(this)
00160 #else
00161 RmstSource::RmstSource(int argc, char **argv) : blobs_to_send_(4)
00162 #endif // NS_DIFFUSION
00163 {
00164   mr = new RmstSrcReceive(this);
00165 
00166 #ifndef NS_DIFFUSION
00167   parseCommandLine(argc, argv);
00168   dr_ = NR::createNR(diffusion_port_);
00169 #endif // NS_DIFFUSION
00170   
00171   ck_val_ = 100;
00172 }
00173 
00174 #ifndef NS_DIFFUSION
00175 int main(int argc, char **argv)
00176 {
00177   RmstSource *app;
00178 
00179   app = new RmstSource(argc, argv);
00180   app->run();
00181 
00182   return 0;
00183 }
00184 #endif // NS_DIFFUSION
00185 
00186 void RmstSource::run()
00187 {
00188 #ifndef NS_DIFFUSION
00189   int sleep_interval;
00190   bool sent_first_blob = false;
00191 #endif // !NS_DIFFUSION
00192   
00193   // Let diffusion know what kinds of interests we "latch." 
00194   subscribe_handle_ = setupRmstInterest();
00195   // Let diffusion know what we intend to publish.
00196   send_handle_ = setupRmstPublication();
00197 
00198 #ifndef NS_DIFFUSION
00199   while(1){
00200     if (num_subscriptions_ > 0){
00201       if (!sent_first_blob){
00202         sendBlob();
00203         sent_first_blob = true;
00204         sleep_interval = 100;
00205       }
00206       else
00207         printf("RMST-SRC::sees subscriptions\n");
00208     }
00209     else{
00210       printf("RMST-SRC::sees no subscriptions\n");
00211       sleep_interval = 10;
00212     }
00213     sleep(sleep_interval);
00214   } //while loop
00215 #else
00216   send();
00217 #endif // !NS_DIFFUSION
00218 }
00219 
00220 
00221 handle RmstSource::setupRmstInterest()
00222 {
00223   NRAttrVec attrs;
00224 
00225   printf("RMST-SRC::sets up local subscription for PCM_SAMPLEs\n");
00226   attrs.push_back(NRClassAttr.make(NRAttribute::NE, NRAttribute::DATA_CLASS));
00227   attrs.push_back(NRScopeAttr.make(NRAttribute::IS,
00228                    NRAttribute::NODE_LOCAL_SCOPE));
00229   attrs.push_back(RmstTargetAttr.make(NRAttribute::IS, "PCM_SAMPLE"));
00230   attrs.push_back(RmstTsprtCtlAttr.make(NRAttribute::IS, RMST_RESP));
00231   attrs.push_back(RmstTsprtCtlAttr.make(NRAttribute::IS, RMST_CONT));
00232 
00233   handle h = dr_->subscribe(&attrs, mr);
00234   ClearAttrs(&attrs);
00235   return h;
00236 }
00237 
00238 handle RmstSource::setupRmstPublication()
00239 {
00240   NRAttrVec attrs;
00241 
00242   printf("RMST-SRC::publishes PCM_SAMPLE\n");
00243   attrs.push_back(NRClassAttr.make(NRAttribute::IS, NRAttribute::DATA_CLASS));
00244   attrs.push_back(RmstTargetAttr.make(NRAttribute::IS, "PCM_SAMPLE"));
00245 
00246   handle h = dr_->publish(&attrs);
00247   ClearAttrs(&attrs);
00248   return h;
00249 }
00250 
00251 
00252 char* RmstSource::createBlob (int ck_val)
00253 {
00254   char *tmpPtr = new char[2500];
00255 
00256   for (int i = 0; i < 50; i++){
00257     sprintf(&tmpPtr[i*50], "PCM FragNo: %d of ck_val %d", i, ck_val); 
00258   }
00259   return tmpPtr;
00260 }
00261 
00262 void RmstSource::sendBlob() {
00263   char *blob;
00264   int retval;
00265   NRAttrVec src_attrs;
00266 
00267   // Retrieve rmsb from the local cache to get pointer to it.
00268   blob = createBlob(ck_val_);
00269   ck_val_++;
00270   src_attrs.push_back(RmstDataAttr.make(NRAttribute::IS, blob, 2500));
00271   retval = ((DiffusionRouting *)dr_)->sendRmst(send_handle_,
00272                            &src_attrs, PAYLOAD_SIZE);
00273   blobs_to_send_--;
00274   delete blob;
00275 }

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