-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestReceiver.java
156 lines (113 loc) · 3.7 KB
/
TestReceiver.java
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
/**
*
* Receiver part of the RBO test.
*
* Copyright (C) 2009 Marcin Kik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* The author can be reached at Marcin.Kik@pwr.wroc.pl
*
*/
import java.util.Random;
public class TestReceiver extends Thread{
TextOutputScroll out=new TextOutputScroll("RECEIVER");
Random random;
TinyChannel channel;
int searchesDone=0;
TinyRadio radio=new TinyRadio();
static final int QUEUE_CAPACITY=100;
TinyTaskQueue tinyTaskQueue=new TinyTaskQueue(QUEUE_CAPACITY);
TinyScheduler tinyScheduler=new TinyScheduler();
//...
TinyBoot tinyBoot=new TinyBoot(new BootEvents());
class BootEvents implements TinyBootEvents{
public void booted()
{
generateSearchedKey();
out.println("booted: searching for key: "+searchedKey);
rboReceiver.search(searchedKey);
//...
}
}
RboReceiver rboReceiver=new RboReceiver(new TestRboRevceiverEvents());
class TestRboRevceiverEvents implements RboReceiverEvents{
public RboMessage searchDone(RboMessage message, byte error)
{
// report results
searchesDone++;
out.println(""+searchesDone+".searchDone: ERR:"+error+" MSG: "+message.toString());
// update parameters
logSequenceLength=message.header.getLogSequenceLength();
//generate the next key
generateSearchedKey();
// post the task of searching new key
TinyTask task=new TinyTask(){
public void run()
{
out.println("task: searching for key: "+searchedKey);
rboReceiver.search(searchedKey);
}
};
// ... post to the queue
tinyTaskQueue.enqueue(task);
// ...
return message; // return the buffer to rboReceiver
}
}
public TestReceiver(TinyChannel rboChannel, long seed)
{
channel=rboChannel;
random=new Random(seed);
out.println("seed ="+seed+"\n");
// WIRING
// wire the task queue
rboReceiver.tinyTaskQueue= tinyTaskQueue;
rboReceiver.timeoutTimer.tinyTaskQueue= tinyTaskQueue;
rboReceiver.sleepingTimer.tinyTaskQueue= tinyTaskQueue;
rboReceiver.receive.tinyTaskQueue= tinyTaskQueue;
rboReceiver.splitControl.tinyTaskQueue= tinyTaskQueue;
tinyBoot.tinyTaskQueue=tinyTaskQueue;
tinyScheduler.tinyTaskQueue=tinyTaskQueue;
//wire the radio
rboReceiver.receive.radio=radio;
rboReceiver.splitControl.radio=radio;
//wire the channel
// rboReceiver.radio.channel=channel;
rboReceiver.receive.channel=channel;
rboReceiver.receive.start(); // listen to the channel
// ...
}
byte logSequenceLength=5; // ...
int searchedKey;
// auxiliary functions and procedures
void generateSearchedKey()
{
searchedKey= random.nextInt( (1<<(logSequenceLength+1))+1 ); // between 0 and 2^(logSequenceLength+1)
}
public void run()
{
out.setVisible(true);
tinyBoot.postBootedTask();
tinyScheduler.taskLoop();
}
public static void main(String[] args)
{
// testing receiver only
TestReceiver receiver=new TestReceiver(new TinyChannel(), System.currentTimeMillis());
receiver.start();
}
}