-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.pde
111 lines (97 loc) · 3.04 KB
/
server.pde
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
NetAddressList myNetAddressList = new NetAddressList();
/*
msgs:
/connect - string:name
/request - string:sensorName
/data - string:sensorName
/result -string:sensorName, ....
*/
NetAddress myBroadcastLocation;
String[] sensorNames;
String actSensorName;
SensorReading actualRequest_s;
int timeBetweenRequest = 10; // seconds
int timeLastRequest = 0;
// HashMap<String, SensorReading> collectedData = new HashMap<String, SensorReading>();
ArrayList<SensorReading> collectedData = new ArrayList<SensorReading>();
void setupServer() {
oscP5 = new OscP5(this, port);
sensorNames= sensors.keySet().toArray(new String[0]);
createNewRequest();
}
void server_update() {
int secondsPassed = millis()/1000;
if (secondsPassed - timeLastRequest > timeBetweenRequest) {
processLastRequest();
createNewRequest();
timeLastRequest = secondsPassed;
}
}
void processLastRequest() {
OscBundle bundle = new OscBundle();
println("processing results. number of data: " + collectedData.size());
for (SensorReading sr : collectedData) {
OscMessage m = sr.createMessage("/result");
sr.print();
bundle.add(m);
}
oscP5.send(bundle, myNetAddressList);
collectedData.clear();
}
void createNewRequest() {
actSensorName = sensorNames[(int) (random(sensorNames.length))];
actualRequest_s = sensors.get(actSensorName);
broadcastRequest();
}
void broadcastRequest() {
OscMessage m = new OscMessage("/request");
m.add(actSensorName);
oscP5.send(m, myNetAddressList);
println("sending request: "+actSensorName);
}
void server_oscEvent(OscMessage msg) {
String addrPattern = msg.addrPattern();
if (addrPattern.equals("/connect"))
connect(msg.netAddress().address());
else if (addrPattern.equals("/data")) {
try {
println("some data coming");
SensorReading reading = actualRequest_s.createFromMessage(msg);
if (reading.name.equals(actSensorName))
// collectedData.put(msg.netAddress().address(), reading);
collectedData.add(reading);
}
catch(Exception exc) {
}
}
else if (addrPattern.equals("/disconnect"))
disconnect(msg.netAddress().address());
else {
msg.print();
}
}
void connect(String theIPaddress) {
if (!myNetAddressList.contains(theIPaddress, port)) {
myNetAddressList.add(new NetAddress(theIPaddress, port));
println("### adding "+theIPaddress+" to the list.");
// quick request:
OscMessage m = new OscMessage("/request");
m.add(actSensorName);
oscP5.send(m, new NetAddress(theIPaddress, port));
//
}
else {
println("### "+theIPaddress+" is already connected.");
}
println("### currently there are "+myNetAddressList.list().size()+" remote locations connected.");
}
private void disconnect(String theIPaddress) {
if (myNetAddressList.contains(theIPaddress, port)) {
myNetAddressList.remove(theIPaddress, port);
println("### removing "+theIPaddress+" from the list.");
}
else {
println("### "+theIPaddress+" is not connected.");
}
println("### currently there are "+myNetAddressList.list().size());
}