File: tags.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,780 kB
  • ctags: 27,490
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (396 lines) | stat: -rw-r--r-- 9,272 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Author: Satish Kumar, kkumar@isi.edu



extern "C" {
#include <stdarg.h>
#include <float.h>
};


#include "tags.h"
#include "random.h"
#include <string.h>


#define MAX_P1 10
#define MAX_P2 10

// Split into 10 rectangles at each level. Have two levels for now.


static class TagDbaseClass:public TclClass
{
  public:
  TagDbaseClass ():TclClass ("TagDbase")
  {
  }
  TclObject *create (int, const char *const *)
  {
    return (new tags_database ());
  }
} class_tags_database;




void tags_database::
trace (char *fmt,...)
{
  va_list ap; // Define a variable ap that will refer to each argument in turn

  if (!tracetarget_)
    return;

  // Initializes ap to first argument
  va_start (ap, fmt); 
  // Prints the elements in turn
  vsprintf (tracetarget_->buffer (), fmt, ap); 
  tracetarget_->dump ();
  // Does the necessary clean-up before returning
  va_end (ap); 
}




int 
tags_database::command (int argc, const char *const *argv)
{
  if (argc == 7)
    {
      if (strcmp (argv[1], "create_database") == 0)
        {
	  double x_min = atof(argv[2]);
	  double x_max = atof(argv[3]);
	  double y_min = atof(argv[4]);
	  double y_max = atof(argv[5]);
	  int num_tags = atoi(argv[6]);
	  
	  num_tags_ = num_tags;
	  sensed_tag_list_ = new int[num_tags];
	  freq_qry_tag_list_ = new int[num_tags];
	  create_tags_database(x_min,x_max,y_min,y_max,num_tags);
          return (TCL_OK);
        }
    }
  else if (argc == 3)
    {
      if (strcasecmp (argv[1], "tracetarget") == 0)
        {
          TclObject *obj;
	  if ((obj = TclObject::lookup (argv[2])) == 0)
            {
              fprintf (stderr, "%s: %s lookup of %s failed\n", __FILE__, argv[1],
                       argv[2]);
              return TCL_ERROR;
            }
          tracetarget_ = (Trace *) obj;
          return TCL_OK;
        }
    }

  return (TclObject::command(argc, argv));
}




void
tags_database::create_tags_database(double x_min, double x_max, double y_min, double y_max, int num_tags)
{
  dbase_node *dbnode;
  int i;

  assert((x_min <= x_max) && (y_min <= y_max));  

  // Creating the data structures for storing the tags information
  tags_db_ = new dbase_node(x_min, x_max, y_min, y_max);

  // Creates child nodes for tags_db_ and partitions x and y ranges
  add_level(x_min, x_max, y_min, y_max, tags_db_);
  
  // Creates child nodes for each of tags_db_'s children
  for(i = 0; i < NUM_RECTANGLES; ++i) {
    dbnode = tags_db_->list_node_[i];
    assert((dbnode->x_min_ <= dbnode->x_max_) && (dbnode->y_min_ <= dbnode->y_max_));
    add_level(dbnode->x_min_, dbnode->x_max_, dbnode->y_min_, dbnode->y_max_, dbnode);
  }

  // Creating the tags and adding them to the database
  tag *newtag = new tag();
  i = 0;
  int p1 = 1, p2 = 1, p3 = 1;
  int max_p1 = MAX_P1, max_p2 = MAX_P2;
  int max_p3 = num_tags/(MAX_P1 * MAX_P2);

  while(i < num_tags) {
    newtag->x_ = x_min + rn_->uniform(x_max-x_min);
    newtag->y_ = y_min + rn_->uniform(y_max-y_min);
    newtag->obj_name_ = (int)((p1 * pow(2,24)) + (p2 * pow(2,16)) + p3) ;
    ++p3;

    if(p3 == max_p3) {
      p3 = 1;
      ++p2;
      if(p2 == max_p2) {
	p2 = 1;
	++p1;
	if(p1 == max_p1) {
	  break;
	}
      }
    }

    Addtag(newtag);

    //    printf("Added object %d.%d.%d at (%f,%f)\n",(newtag->obj_name_ >> 24) & 0xFF,(newtag->obj_name_ >> 16) & 0xFF,(newtag->obj_name_) & 0xFFFF,newtag->x_,newtag->y_);
    ++i;
  }
  delete newtag;
}



void
tags_database::add_level(double x_min, double x_max, double y_min, double y_max, dbase_node *dbnode)
{
  double x1, x2, y1, y2, x_partition_size;
  int i;

// Just partitioning along x-range for now
  x_partition_size = (x_max-x_min)/NUM_RECTANGLES;
  x2 = x_min;
  y1 = y_min;
  y2 = y_max;
  for(i = 0; i < NUM_RECTANGLES; ++i) {
    x1 = x2;                    // x_min for partition
    x2 = x1 + x_partition_size; // x_max for partition
    
    // The last partition should cover the remaining ranges
    if (i == (NUM_RECTANGLES - 1)) {
      x2 = x_max;
    }
    dbnode->list_node_[i] = new dbase_node(x1,x2,y1,y2);
  }
}



void
tags_database::Addtag(const tag* tag_)
{
  dbase_node *dbnode = tags_db_;
  int i, found = FALSE;
  tag *new_tag;

  while(dbnode->list_node_[0] != NULL) {
    for(i = 0; i < NUM_RECTANGLES; ++i) {
      if(((dbnode->list_node_[i])->x_min_ <= tag_->x_) && ((dbnode->list_node_[i])->x_max_ >= tag_->x_)) {
	found = TRUE;
	break;
      }
    }
    assert(found);
    dbnode = dbnode->list_node_[i];
  }
  
  if(dbnode->tags_list_ == NULL) {
    dbnode->tags_list_ = new tag;
    new_tag = dbnode->tags_list_;
  }
  else {
    new_tag = dbnode->tags_list_;
    while(new_tag->next_ != NULL) {
      new_tag = new_tag->next_;
    }
    new_tag->next_ = new tag;
    new_tag = new_tag->next_;
  }

  // tags do not have any attributes for now
  new_tag->x_ = tag_->x_;
  new_tag->y_ = tag_->y_;
  new_tag->obj_name_ = tag_->obj_name_;
}



void
tags_database::Deletetag(const tag *tag_)
{
  dbase_node *dbnode = tags_db_;
  int i, found = FALSE;
  tag *old_tag;

  while(dbnode->list_node_[0] != NULL) {
    for(i = 0; i < NUM_RECTANGLES; ++i) {
      if(((dbnode->list_node_[i])->x_min_ <= tag_->x_) && ((dbnode->list_node_[i])->x_max_ >= tag_->x_)) {
	found = TRUE;
	break;
      }
    }
    assert(found);
    dbnode = dbnode->list_node_[i];
  }
  
  assert(dbnode->tags_list_ != NULL);

  //old_tag will point to the tag entry that is to be deleted
  found = FALSE;
  old_tag = dbnode->tags_list_;
  tag *prev_tag = old_tag; // Should have previous tag in list
  while(old_tag != NULL) {
    if ((old_tag->x_== tag_->x_) && (old_tag->y_ == tag_->y_) && (old_tag->obj_name_ == tag_->obj_name_)) {
      found = TRUE;
      break;
    }
    prev_tag = old_tag;
    old_tag = old_tag->next_;
  }
  
  assert(found);
  prev_tag->next_ = old_tag->next_;
  delete old_tag;
}



compr_taglist *
tags_database::Gettags(double x, double y, double r)
{
  dbase_node *dbnode = tags_db_;
  compr_taglist *tptr;
  int i, found = FALSE;
  vtags_ = NULL;
  
  // This interior node should have child nodes
  //  assert(dbnode->list_node_[0] != NULL);
  search_tags_dbase(x,y,r,dbnode);

  tptr = vtags_;
  while(tptr) {
    found = FALSE;
    for(i = 0; i < num_sensed_tags_; ++i) {
      if(tptr->obj_name_ == sensed_tag_list_[i]) {
	found = TRUE;
	break;
      }
    }
    if(!found) {
      //      int r = Random::uniform(4);

      // 20 % of objects stored in frequently queried objects list; others
      // in sensed_tag_list_;
      //      if(r == 0) {
      //	freq_qry_tag_list_[num_freq_qry_tags_] = tptr->obj_name_;
      //	++num_freq_qry_tags_;   	
      //      }
      //      else {
	sensed_tag_list_[num_sensed_tags_] = tptr->obj_name_;
	++num_sensed_tags_;
	//      }
    }
    tptr = tptr->next_;
  }
  assert(num_sensed_tags_ <= num_tags_);
  return(vtags_);
}


int
tags_database::get_random_tag()
{

  // Objects in freq_qry_tags_ are queried 10 times as often as those in
  // sensed_tag_list_
  //  int r = Random::uniform(10);
  //  if(r == 0) {
    int i = rn_->uniform(num_sensed_tags_);
    return(sensed_tag_list_[i]);
  //  }
  //  else {
  //    int i = rn_->uniform(num_freq_qry_tags_);
  //    return(freq_qry_tag_list_[i]);
    //  }
}




void
tags_database::search_tags_dbase(double x, double y, double r, dbase_node *dbnode)
{
  int i, found = FALSE, removed_tag = 0;
  compr_taglist **apt_tags;
  tag *prev_tag, *next_tag;
  tag *dbase_tags;
  dbase_node *child_dbnode;

  // If this is a leaf interior node, lookup the taglist for appropriate tags
  if(dbnode->list_node_[0] == NULL) {

    apt_tags = &vtags_;
    while((*apt_tags) != NULL)
      apt_tags = &((*apt_tags)->next_);
    
    dbase_tags = dbnode->tags_list_;
    prev_tag = dbase_tags;
    while(dbase_tags) {
      removed_tag = 0;
      double xpos = (dbase_tags->x_ - x) * (dbase_tags->x_ - x);
      double ypos = (dbase_tags->y_ - y) * (dbase_tags->y_ - y);

      if((xpos + ypos) < (r*r)) {
	*apt_tags = new compr_taglist;
	(*apt_tags)->obj_name_ = dbase_tags->obj_name_;
	apt_tags = &((*apt_tags)->next_);

	// Delete tag from the list so that only one sensor observes 
	// a particular tag
	removed_tag = 1;
	if(prev_tag == dbase_tags) {
	  dbnode->tags_list_ = dbase_tags->next_;
	  prev_tag = dbase_tags->next_;
	}
	else
	  prev_tag->next_ = dbase_tags->next_;

	next_tag = dbase_tags->next_;
	delete dbase_tags;
      }
      if(!removed_tag) {
	prev_tag = dbase_tags;
	dbase_tags = dbase_tags->next_;
      }
      else {
	dbase_tags = next_tag;
      }
    }
    return;
  }
  
  for(i = 0; i < NUM_RECTANGLES; ++i) {
    found = FALSE;

    // Check if x-r is in the x-range of this node
    if(((dbnode->list_node_[i])->x_min_ <= (x - r)) && ((dbnode->list_node_[i])->x_max_ >= (x - r)))
      found = TRUE;

    // Check if x+r is in the x-range of this node
    if(((dbnode->list_node_[i])->x_min_ <= (x + r)) && ((dbnode->list_node_[i])->x_max_ >= (x + r)))
      found = TRUE;

    // Check if the range (x-r,x+r) covers the x-range of this node
    if(((dbnode->list_node_[i])->x_min_ >= (x - r)) && ((dbnode->list_node_[i])->x_max_ <= (x + r)))
      found = TRUE;
    if(found) {
      child_dbnode = dbnode->list_node_[i];
      search_tags_dbase(x,y,r,child_dbnode);
    }
  }
}