VizBlocks
CommsBehaviours.h
Go to the documentation of this file.
1 #ifndef COMMS_BEHAVIOUR_h
2 #define COMMS_BEHAVIOUR_h
3 #include "Arduino.h"
4 #include "Behaviours.h"
5 #include <VizBlocks.h>
6 
7 /*
8  * --------------------------------------------------
9  * ---------------- SendCapabilities ----------------
10  * --------------------------------------------------
11  */
12 
13 class SendCapabilities : public Behaviour {
14  VizBlocks* _node;
15 
16 public:
17  SendCapabilities(VizBlocks* node, String name = "SendCapabilities") :
18  Behaviour(name), _node(node){ }
19 
20  String start(String args) {
21  //This is where you do your stuff for a simple behaviour
22  _node->announce_capabilities();
23  return "SendCapabilities behaviour " + _name;
24  }
25 
26 };
27 
28 /*
29  * --------------------------------------------------
30  * ---------------------- Link ----------------------
31  * --------------------------------------------------
32  */
33 
34 class Link : public Behaviour {
35  VizBlocks* _node;
36  String _peerId;
37  const int _timeoutInterval = 5000;
38  unsigned long _t = 0;
39 
40 public:
41  Link(VizBlocks* node, String name = "Link") : Behaviour(name), _node(node) { _background = true; }
42 
43  char* args() {return "<String peerId>"; };
44 
45  String start(String args) {
46  _running = true;
47  if (args == name() || args.indexOf(" ")>0) {
48  return "Invalid args (" + args + ") in behaviour " + name();
49  }
50 
51  _t = millis();
52 
53  if (args == _peerId) {
54  return "Link ping from (" + _peerId + ")";
55  }
56 
57  _peerId = args;
58 
59  String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
60  _node->announce(str);
61 
62  return "New link with (" + _peerId + ")";
63  }
64 
65  void update() {
66  if (millis() > (_t+_timeoutInterval)) {
67  String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
68  _node->announce(str);
69  _peerId = "";
70  _running = false;
71  }
72  }
73 
74 };
75 
76 /*
77  * --------------------------------------------------
78  * ------------------- PingServer -------------------
79  * --------------------------------------------------
80  */
81 
82 class PingServer : public Behaviour {
83  VizBlocks* _node;
84  String str;
85  const int _interval = 4000;
86  unsigned long _t = 0;
87 
88 public:
89  PingServer(VizBlocks* node, String name = "PingServer") : Behaviour(name), _node(node) { _background = true; }
90 
91  String start(String args) {
92  _background = true;
93  _running = true;
94  _t = millis();
95  str = "{\"id\":\"" + String(_node->getId()) + "\",\"PingServer\":{}}";
96  _node->announce(str);
97  return "Pinging server";
98  }
99 
100  void update() {
101  if (millis() > (_t+_interval)) {
102  _t = millis();
103  _node->announce(str);
104  }
105  }
106 
107 };
108 
109 #endif
SendCapabilities::SendCapabilities
SendCapabilities(VizBlocks *node, String name="SendCapabilities")
Definition: CommsBehaviours.h:17
Behaviour::_background
boolean _background
Definition: Behaviours.h:10
Behaviour::name
virtual String name()
Definition: Behaviours.h:28
VizBlocks::getId
char * getId()
Definition: VizBlocks.h:291
Behaviour::args
virtual char * args()
Definition: Behaviours.h:30
PingServer::start
String start(String args)
Definition: CommsBehaviours.h:91
Behaviour::_name
String _name
Definition: Behaviours.h:11
Behaviour
Definition: Behaviours.h:4
VizBlocks::announce
void announce(String doc)
Definition: VizBlocks.h:270
SendCapabilities
Definition: CommsBehaviours.h:13
PingServer
Definition: CommsBehaviours.h:82
SendCapabilities::start
String start(String args)
Definition: CommsBehaviours.h:20
Behaviours.h
Behaviour::_running
boolean _running
Definition: Behaviours.h:9
VizBlocks
Definition: VizBlocks.h:15
VizBlocks::announce_capabilities
void announce_capabilities()
Definition: VizBlocks.h:276
VizBlocks.h
PingServer::update
void update()
Definition: CommsBehaviours.h:100
PingServer::PingServer
PingServer(VizBlocks *node, String name="PingServer")
Definition: CommsBehaviours.h:89