clmsg-discovery.cc

00001 
00002 /*
00003  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00015  *    names of its contributors may be used to endorse or promote products 
00016  *    derived from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00025  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00027  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00028  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 
00044 #include "clmsg-discovery.h"
00045 
00046 #include <iostream>
00047 
00048 ClMessage_t CLMSG_DISCOVERY;
00049 
00050 DiscoveryData::DiscoveryData(const PlugIn* pointer, int layer_id, int id, const char* tcl_name, const char* miracle_tag)
00051 {
00052   module_pointer = pointer;
00053   module_layer_id = layer_id; 
00054   module_id = id;
00055   module_tcl_name = tcl_name;
00056   module_miracle_tag = miracle_tag;
00057   is_valid = true;
00058 }
00059 
00060 DiscoveryData::DiscoveryData() 
00061 { 
00062   DiscoveryData(0, 0, 0, "NULL", "NULL");
00063   is_valid = false;
00064 }
00065 
00066 DiscoveryData::DiscoveryData(const DiscoveryData& copy)
00067 {
00068   module_pointer = copy.module_pointer;
00069   module_layer_id = copy.module_layer_id; 
00070   module_id = copy.module_id;
00071   module_tcl_name = copy.module_tcl_name;
00072   module_miracle_tag = copy.module_miracle_tag;
00073   is_valid = copy.is_valid;
00074 }
00075 
00076 void DiscoveryData::reset()
00077 {
00078   module_pointer = 0;
00079   module_layer_id = 0; 
00080   module_id = 0;
00081   module_tcl_name.clear();
00082   module_miracle_tag.clear();
00083   is_valid = false;
00084 }
00085 
00086 DiscoveryStorage::DiscoveryStorage()
00087 { 
00088   reset();
00089 }
00090 
00091 int DiscoveryStorage::getDataSize()
00092 {
00093  return db_id.size();
00094 }
00095 
00096 void DiscoveryStorage::reset()
00097 {
00098   db_id.clear();
00099   db_layer.clear();
00100   db_tag.clear();
00101   db_name.clear();
00102   db_pointer.clear();
00103 }
00104 
00105 void DiscoveryStorage::addEntry(const PlugIn* pointer, int layer_id, int id, const char* tcl_name, 
00106                                                                                 const char* miracle_tag)
00107 {
00108   string name_s = tcl_name;
00109   string tag_s = miracle_tag;
00110 
00111   db_id[id] = DiscoveryData(pointer, layer_id, id, tcl_name, miracle_tag);
00112 
00113   db_layer[layer_id].insert(id);
00114   db_tag[tag_s].insert(id);
00115   db_name[name_s].insert(id);
00116   db_pointer[pointer] = id;
00117 }
00118 
00119 void DiscoveryStorage::addEntry(DiscoveryData data) 
00120 {
00121   string name_s = data.getTclName();
00122   string tag_s = data.getMiracleTag();
00123 
00124   db_id[data.getId()] = data;
00125   db_layer[data.getLayer()].insert(data.getId());
00126   db_tag[tag_s].insert(data.getId());
00127   db_name[name_s].insert(data.getId());
00128   db_pointer[data.getPointer()] = data.getId();
00129 }
00130 
00131 void DiscoveryStorage::removeEntry(int id)
00132 {
00133   // erase all tables
00134   string name_s = db_id[id].getTclName();
00135   string tag_s = db_id[id].getMiracleTag();
00136 
00137   db_layer[db_id[id].getLayer()].erase(id);
00138   db_tag[tag_s].erase(id);
00139   db_name[name_s].erase(id);
00140   db_pointer.erase( db_id[id].getPointer() );
00141   db_id.erase(id);
00142 }
00143 
00144 DiscoveryStorage DiscoveryStorage::findLayer(int layer_id)
00145 {
00146   DiscoveryStorage result_query;
00147   // if no match found we return a empty storage
00148   if ( db_layer.find(layer_id) != db_layer.end() ) {
00149     for ( SetIt iter = db_layer[layer_id].begin(); iter != db_layer[layer_id].end(); iter++ ) {
00150         // inserting DiscoveryData found into reply
00151         result_query.addEntry(db_id[*iter]);
00152     }
00153   }
00154   return result_query;
00155 }
00156 
00157 DiscoveryStorage DiscoveryStorage::findTag(const char* tag)
00158 {
00159   string tag_s = tag;
00160 
00161   DiscoveryStorage result_query;
00162   // if no match found we return a empty storage
00163   if ( db_tag.find(tag_s) != db_tag.end() ) {
00164     for ( SetIt iter = db_tag[tag_s].begin(); iter != db_tag[tag_s].end(); iter++ ) {
00165         // inserting DiscoveryData found into reply
00166         result_query.addEntry(db_id[*iter]);
00167     }
00168   }
00169   return result_query;
00170 }
00171 
00172 DiscoveryStorage DiscoveryStorage::findTclName(const char* tcl_name)
00173 {
00174   string name_s = tcl_name;
00175 
00176   DiscoveryStorage result_query;
00177   // if no match found we return a empty storage
00178   if ( db_name.find(name_s) != db_name.end() ) {
00179     for ( SetIt iter = db_name[name_s].begin(); iter != db_name[name_s].end(); iter++ ) {
00180         // inserting DiscoveryData found into reply
00181         result_query.addEntry(db_id[*iter]);
00182     }
00183   }
00184   return result_query;
00185 }
00186 
00187 DiscoveryData DiscoveryStorage::findPointer(const PlugIn* pointer)
00188 {
00189   DiscoveryData result_query;
00190   result_query.reset();
00191   // if no match found we return a empty DiscoveryData
00192   if ( db_pointer.find(pointer) == db_pointer.end() ) return result_query;
00193   else return( result_query = db_id[ db_pointer[pointer] ] );
00194 }
00195 
00196 void DiscoveryStorage::printData()
00197 {
00198   cout << "Storage size = " << getSize() << endl;
00199 
00200   for (DBIt it = db_id.begin(); it != db_id.end(); it++) {
00201  
00202            int id = (*it).first;
00203       cout << "Module id = " << id << endl 
00204            << "Module layer = " << db_id[id].getLayer() << endl
00205            << "Module tcl name = " << db_id[id].getTclName() << endl 
00206            << "Module miracle tag = " << db_id[id].getMiracleTag() << endl
00207            << "Module pointer = " << hex << db_id[id].getPointer() << endl << endl;
00208   }
00209 }
00210 
00211 
00212 ClMsgDiscovery::ClMsgDiscovery()
00213   : ClMessage(CLMSG_DISCOVERY_VERBOSITY, CLMSG_DISCOVERY)
00214 {
00215   sender_data.reset();
00216 }
00217 
00218 ClMessage* ClMsgDiscovery::copy()
00219 {
00220   // Supporting only synchronous messages!!!
00221   assert(0);
00222 }
00223 
00224 void ClMsgDiscovery::addData(const PlugIn* pointer, int layer_id, int id, const char* tcl_name, 
00225                                                                                    const char* miracle_tag)
00226 {
00227   reply_storage.addEntry(pointer, layer_id, id, tcl_name, miracle_tag);
00228 }
00229 
00230 void ClMsgDiscovery::addSenderData(const PlugIn* pointer, int layer_id, int id, const char* tcl_name, const char* miracle_tag)
00231 {
00232   sender_data.setPointer(pointer);
00233   sender_data.setLayer(layer_id);
00234   sender_data.setId(id);
00235   sender_data.setTclName(tcl_name);
00236   sender_data.setMiracleTag(miracle_tag);
00237   sender_data.setValid();
00238 }
00239 
00240 void ClMsgDiscovery::removeData(int id)
00241 {
00242   reply_storage.removeEntry(id);
00243 }
00244 
00245 void ClMsgDiscovery::removeSenderData()
00246 {
00247   sender_data.reset();
00248 }
00249 
00250 DiscoveryData ClMsgDiscovery::getSenderData()
00251 {
00252   return(sender_data);
00253 }
00254 
00255 DiscoveryStorage ClMsgDiscovery::findLayer(int layer_id)
00256 {
00257   return(reply_storage.findLayer(layer_id));
00258 }
00259 
00260 DiscoveryStorage ClMsgDiscovery::findTag(const char* tag)
00261 {
00262   return(reply_storage.findTag(tag));
00263 }
00264 
00265 DiscoveryStorage ClMsgDiscovery::findTclName(const char* tcl_name)
00266 {
00267   return(reply_storage.findTclName(tcl_name));
00268 }
00269 
00270  
00271 DiscoveryData ClMsgDiscovery::findPointer(const PlugIn* pointer)
00272 {
00273   return(reply_storage.findPointer(pointer));
00274 }
00275 
00276 DiscoveryStorage ClMsgDiscovery::copyStorage()
00277 {
00278    reply_storage.addEntry(sender_data.getPointer(), sender_data.getLayer(), sender_data.getId(), sender_data.getTclName()
00279                                                                         , sender_data.getMiracleTag());
00280    return reply_storage;
00281 }
00282 
00283 void ClMsgDiscovery::printReplyData()
00284 {
00285   reply_storage.printData();
00286 }
00287 
00288 int ClMsgDiscovery::getDBSize()
00289 {
00290   return(reply_storage.getSize());
00291 }

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2