File: gridkeeper.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,796 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (280 lines) | stat: -rw-r--r-- 6,597 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
/*
 * An optimizer for some wireless simulations
 *
 * Helps most when some nodes are mostly stationary.
 * We hope you can share your experience with the gridkeeper with us.
 *
 * Ported from Sun's mobility code
 */

#include "gridkeeper.h"
#include <sys/param.h> /* For MIN/MAX */

static double d2(double x1, double x2, double y1, double y2)
{
  return ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

GridHandler::GridHandler()
{
}

void GridHandler::handle(Event *e)
{
  MoveEvent *me = (MoveEvent *)e;
  MobileNode **pptr;
  MobileNode * token_ = me->token_;

  if ((pptr = me->leave_)) {
    while (*pptr) {
      if ((*pptr)->address() == token_->address()) break;
      pptr = &((*pptr)->next());
    }
    if (!(*pptr)) abort();
    else {
      *pptr = token_->next();
      token_->next() = 0;
    }
    
  }
  if ((pptr = me->enter_)) {
    if (token_->next()) abort();  // can't be in more than one grid
    token_->next() = *pptr;
    *pptr = token_;
  }
  delete me;

  // dump info in the gridkeeper for debug only
  // dump();

}

GridKeeper* GridKeeper::instance_; 

static class GridKeeperClass : public TclClass {
public:
  GridKeeperClass() : TclClass("GridKeeper") {}
  TclObject* create(int, const char*const*) {
      return (new GridKeeper);
  }
} class_grid_keeper;

GridKeeper::GridKeeper() : size_(0),grid_(NULL), dim_x_(0), dim_y_(0)
{
  gh_ = new GridHandler();
}

GridKeeper::~GridKeeper()
{
  int i;
  for (i = 0; i <= dim_x_; i++) {
    delete [] grid_[i];
  }
  delete [] grid_;
}

int GridKeeper::command(int argc, const char*const* argv)
{
  int i, grid_x, grid_y;
  Tcl& tcl = Tcl::instance();
  MobileNode *mn;

  if (argc == 2) {
    if (strcmp(argv[1], "dump") == 0) {
      dump();
      return (TCL_OK);
    }
  }

  if (argc == 3) {
    if (strcmp(argv[1], "addnode") == 0) {
	mn = (MobileNode *)TclObject::lookup(argv[2]);
	grid_x = aligngrid((int)mn->X(), dim_x_);
        grid_y = aligngrid((int)mn->Y(), dim_y_);
	mn->next() = grid_[grid_x][grid_y];
	grid_[grid_x][grid_y] = mn;
	size_++;
        return (TCL_OK);
    }
  }
  if (argc == 4 && strcmp(argv[1], "dimension") == 0) {
    if (instance_ == 0) instance_ = this;

    dim_x_ = strtol(argv[2], (char**)0, 0);
    dim_y_ = strtol(argv[3], (char**)0, 0);

    if (dim_x_ <= 0 || dim_y_ <= 0) {
      tcl.result("illegal grid dimension");
      return (TCL_ERROR);
    }

    grid_ = new MobileNode **[dim_x_];
    for (i = 0; i < dim_x_; i++) {
      grid_[i] = new MobileNode *[dim_y_];
      bzero((void *)grid_[i], sizeof(MobileNode *)*dim_y_);
    }
    return (TCL_OK);
  }
  return (TclObject::command(argc, argv));
}

void GridKeeper::new_moves(MobileNode *mn)
{
  double x = mn->X(), y = mn->Y(); 
  double ss = mn->speed();
  double vx = (mn->dX())*ss, vy = (mn->dY())*ss;

  double endx, endy, pother, tm;
  int i, j, endi, gother, grid_x, grid_y;
  MoveEvent *me;

  Scheduler& s = Scheduler::instance();

  endx = mn->destX();
  endy = mn->destY();

  if (vx > 0) {
    endi = MIN(dim_x_-1, (int)endx);
    for (i = (int)x+1; i <= endi; i++) {
      tm = (i-x)/vx;
      pother = vy*tm + y;
      j = (int)pother;
      me = new MoveEvent;
      if (j == pother && j != 0 && j != dim_y_) {
	if (vy > 0) gother = j - 1;
	else if (vy < 0) gother = j + 1;
	else gother = j;
      }
      else {
	gother = j;
      }
      me->leave_ = &(grid_[aligngrid(i-1, dim_x_)][aligngrid(gother, dim_y_)]);

      me->grid_x_ = grid_x = aligngrid(i, dim_x_);
      me->grid_y_ = grid_y = aligngrid(j, dim_y_);
      me->enter_ = &(grid_[grid_x][grid_y]);
      me->token_ = mn;
      s.schedule(gh_, me, tm);
    }
  }
  else if (vx < 0) {
    endi = (int)endx;
    for (i = (int)x; i > endi; i--) {
      if (i == dim_x_) continue;
      tm = (i-x)/vx;
      pother = vy*tm + y;
      j = (int)pother;
      me = new MoveEvent;
      if (j == pother && j != 0 && j != dim_y_) {
	if (vy > 0) gother = j - 1;
	else if (vy < 0) gother = j + 1;
	else gother = j;
      }
      else {
	gother = j;
      }
      me->leave_ = &grid_[aligngrid(i, dim_x_)][aligngrid(gother, dim_y_)];

      me->grid_x_ = grid_x = aligngrid(i-1, dim_x_);
      me->grid_y_ = grid_y = aligngrid(j, dim_y_);
      me->enter_ = &grid_[grid_x][grid_y];
      me->token_ = mn;
      s.schedule(gh_, me, tm);
    }
  }
  if (vy > 0) {
    endi = MIN(dim_y_-1, (int)endy);
    for (j = (int)y+1; j <= endi; j++) {
      tm = (j-y)/vy;
      pother = vx*tm + x;
      i = (int)pother;
      me = new MoveEvent;
      if (i == pother && i != 0 && i != dim_x_ && vx != 0) continue;
      me->leave_ = &grid_[aligngrid(i, dim_x_)][aligngrid(j-1, dim_y_)];

      me->grid_x_ = grid_x = aligngrid(i, dim_x_);
      me->grid_y_ = grid_y = aligngrid(j, dim_y_);
      me->enter_ = &grid_[grid_x][grid_y];
      me->token_ = mn;

      s.schedule(gh_, me, tm);
    }
  }
  else if (vy < 0) {
    endi = (int)endy;
    for (j = (int)y; j > endi; j--) {
      if (j == dim_y_) continue;
      tm = (j-y)/vy;
      pother = vx*tm + x;
      i = (int)pother;
      me = new MoveEvent;
      if (i == pother && i != 0 && i != dim_x_ && vx != 0) continue;
      me->leave_ = &grid_[aligngrid(i, dim_x_)][aligngrid(j, dim_y_)];

      me->grid_x_ = grid_x = aligngrid(i, dim_x_);
      me->grid_y_ = grid_y = aligngrid(j-1, dim_y_);
      me->enter_ = &grid_[grid_x][grid_y];
      me->token_ = mn;
      s.schedule(gh_, me, tm);
    }
  }

}

int GridKeeper::get_neighbors(MobileNode* mn, MobileNode **output)
{
  int grid_x, grid_y, index = 0, i, j, ulx, uly, lly, adj;
  MobileNode *pgd;
  double mnx, mny, mnr, sqmnr;
  
  mn->update_position();
  mnx = mn->X();
  mny = mn->Y();

  grid_x = aligngrid((int)mn->X(), dim_x_);
  grid_y = aligngrid((int)mn->Y(), dim_y_);

  mnr = mn->radius();
  sqmnr = mnr * mnr;

  adj = (int)ceil(mnr);

  ulx = MIN(dim_x_-1, grid_x + adj);
  uly = MIN(dim_y_-1, grid_y + adj);
  lly = MAX(0, grid_y - adj);

  for (i = MAX(0, grid_x - adj); i <= ulx; i++) {
    for (j = lly; j <= uly; j++) {
      for (pgd = grid_[i][j]; pgd != 0; pgd = pgd->next()) {
	if (mn->address() == pgd->address()) 
		continue;
	pgd->update_position();
	if (d2(pgd->X(), mnx, pgd->Y(), mny) < sqmnr)
	 output[index++] = pgd;
      }
    }
  }

  return index;
}

void GridKeeper::dump()
{
    int i,j;
    MobileNode *pgd;

    for (i = 0; i< dim_x_; i++) {
      for (j = 0; j < dim_y_; j++) {
	if (grid_[i][j] == 0) continue;
	printf("grid[%d][%d]: ",i,j);
	for (pgd = grid_[i][j]; pgd != 0; pgd = pgd->next()) {
	  printf("%d ",pgd->address());
	}
	printf("\n");
      }
    }
    printf("-------------------------------\n");

}