-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncAgent.java
More file actions
54 lines (44 loc) · 1.36 KB
/
Copy pathIncAgent.java
File metadata and controls
54 lines (44 loc) · 1.36 KB
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
package test;
import test.TopicManagerSingleton.TopicManager;
public class IncAgent implements Agent {
private double value = 0;
private String[] subs;
private String[] pubs;
private TopicManagerSingleton.TopicManager tm;
public IncAgent(String[] subs, String[] pubs) {
this.subs = subs;
this.pubs = pubs;
// Get TopicManager instance
this.tm = TopicManagerSingleton.get();
// Subscribe to input topic
if (subs != null && subs.length > 0) {
tm.getTopic(subs[0]).subscribe(this);
}
}
@Override
public String getName() {
return "IncAgent";
}
@Override
public void reset() {
value = 0;
}
@Override
public void callback(String topic, Message msg) {
// Update value based on input topic
if (subs != null && subs.length > 0 && topic.equals(subs[0])) {
value = msg.asDouble;
value++;
if (pubs != null && pubs.length > 0) {
tm.getTopic(pubs[0]).publish(new Message(value));
}
}
}
@Override
public void close() {
// Unsubscribe from topic if needed
if (subs != null && subs.length > 0) {
tm.getTopic(subs[0]).unsubscribe(this);
}
}
}