00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "tag.hh"
00044
00045
00046
00047 #ifdef NS_DIFFUSION
00048 static class TagFilterClass : public TclClass {
00049 public:
00050 TagFilterClass() : TclClass("Application/DiffApp/TagFilter") {}
00051 TclObject * create(int argc, const char*const* argv) {
00052 return(new TagFilter());
00053 }
00054 } class_tag_filter;
00055
00056 int TagFilter::command(int argc, const char*const* argv) {
00057 if (argc == 2) {
00058 if (strcmp(argv[1], "start") == 0) {
00059 run();
00060 return (TCL_OK);
00061 }
00062 }
00063 return (DiffApp::command(argc, argv));
00064 }
00065 #endif // NS_DIFFUSION
00066
00067 void TagFilterReceive::recv(Message *msg, handle h)
00068 {
00069 app_->recv(msg, h);
00070 }
00071
00072 void TagFilter::recv(Message *msg, handle h)
00073 {
00074 if (h != filter_handle_){
00075 DiffPrint(DEBUG_ALWAYS, "Error: TagFilter::recv received message for handle %ld when subscribing to handle %ld !\n", h, filter_handle_);
00076 return;
00077 }
00078
00079 ProcessMessage(msg);
00080
00081 ((DiffusionRouting *)dr_)->sendMessage(msg, h);
00082 }
00083
00084 void TagFilter::ProcessMessage(Message *msg)
00085 {
00086 char *original_route;
00087 char *new_route;
00088 int len, total_len;
00089 NRSimpleAttribute<char *> *route = NULL;
00090
00091
00092 if (!id_)
00093 return;
00094
00095 route = RouteAttr.find(msg->msg_attr_vec_);
00096 if (!route){
00097 DiffPrint(DEBUG_ALWAYS, "Error: Can't find the route attribute !\n");
00098 return;
00099 }
00100
00101 original_route = route->getVal();
00102 len = strlen(original_route);
00103
00104 if (len == 0){
00105 total_len = strlen(id_);
00106
00107
00108 new_route = new char[(total_len + 1)];
00109 strcpy(new_route, id_);
00110 if (new_route[total_len] != '\0')
00111 DiffPrint(DEBUG_ALWAYS, "Warning: String must end with NULL !\n");
00112 }
00113 else{
00114
00115
00116
00117 total_len = len + strlen(id_) + 1;
00118 new_route = new char[(total_len + 1)];
00119 strcpy(new_route, original_route);
00120 new_route[len] = ':';
00121 strcpy(&new_route[len+1], id_);
00122 if (new_route[total_len] != '\0'){
00123 DiffPrint(DEBUG_ALWAYS, "Warning: String must end with NULL !\n");
00124 }
00125 }
00126
00127
00128 DiffPrint(DEBUG_ALWAYS, "Tag Filter: Original route : %s\n", original_route);
00129 DiffPrint(DEBUG_ALWAYS, "Tag Filter: New route : %s\n", new_route);
00130
00131 route->setVal(new_route);
00132
00133
00134 delete [] new_route;
00135 }
00136
00137 handle TagFilter::setupFilter()
00138 {
00139 NRAttrVec attrs;
00140 handle h;
00141
00142
00143 attrs.push_back(RouteAttr.make(NRAttribute::EQ_ANY, ""));
00144
00145 h = ((DiffusionRouting *)dr_)->addFilter(&attrs, TAG_FILTER_PRIORITY,
00146 filter_callback_);
00147
00148 ClearAttrs(&attrs);
00149 return h;
00150 }
00151
00152 void TagFilter::run()
00153 {
00154 #ifdef NS_DIFFUSION
00155
00156 filter_handle_ = setupFilter();
00157 DiffPrint(DEBUG_ALWAYS, "Tag Filter subscribed to *, received handle %d\n",
00158 (int) filter_handle_);
00159 DiffPrint(DEBUG_ALWAYS, "Tag Filter initialized !\n");
00160 #else
00161
00162 while (1){
00163 sleep(1000);
00164 }
00165 #endif // NS_DIFFUSION
00166 }
00167
00168 void TagFilter::getNodeId()
00169 {
00170 DiffPrint(DEBUG_ALWAYS, "Tag Filter: getNodeID function not yet implemented !\n");
00171 DiffPrint(DEBUG_ALWAYS, "Tag Filter: Please set scadds_addr to the node id !\n");
00172 exit(-1);
00173
00174 }
00175
00176 #ifdef NS_DIFFUSION
00177 TagFilter::TagFilter()
00178 #else
00179 TagFilter::TagFilter(int argc, char **argv)
00180 #endif // NS_DIFFUSION
00181 {
00182 char *id_env = NULL;
00183 char buffer[BUFFER_SIZE];
00184 int flag;
00185 int node_id;
00186
00187 id_ = NULL;
00188 node_id = 0;
00189
00190
00191 #ifndef NS_DIFFUSION
00192 parseCommandLine(argc, argv);
00193 dr_ = NR::createNR(diffusion_port_);
00194 #endif // !NS_DIFFUSION
00195
00196 filter_callback_ = new TagFilterReceive(this);
00197
00198
00199 id_env = getenv("scadds_addr");
00200 if (id_env){
00201 node_id = atoi(id_env);
00202 flag = snprintf(&buffer[0], BUFFER_SIZE, "%d", node_id);
00203 if (flag == -1 || flag == BUFFER_SIZE){
00204 DiffPrint(DEBUG_ALWAYS, "Error: Buffer too small !\n");
00205 exit(-1);
00206 }
00207 id_ = strdup(&buffer[0]);
00208 }
00209 else{
00210 getNodeId();
00211 }
00212 #ifndef NS_DIFFUSION
00213
00214 filter_handle_ = setupFilter();
00215 DiffPrint(DEBUG_ALWAYS, "Tag Filter subscribed to *, received handle %ld\n",
00216 filter_handle_);
00217 DiffPrint(DEBUG_ALWAYS, "Tag Filter initialized !\n");
00218 #endif // !NS_DIFFUSION
00219 }
00220
00221 #ifndef USE_SINGLE_ADDRESS_SPACE
00222 int main(int argc, char **argv)
00223 {
00224 TagFilter *app;
00225
00226
00227 app = new TagFilter(argc, argv);
00228 app->run();
00229
00230 return 0;
00231 }
00232 #endif // !USE_SINGLE_ADDRESS_SPACE