VizBlocks
CommsBehaviours.hpp
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 
14 {
15  VizBlocks* _node;
16 
17 public:
18  SendCapabilities(VizBlocks* node, String name = "SendCapabilities") :
19  Behaviour(name), _node(node)
20  { }
21 
25  String start(String args)
26  {
27  //This is where you do your stuff for a simple behaviour
28  _node->announce_capabilities();
29 
30  return "SendCapabilities behaviour " + _name;
31  }
32 
33 };
34 
35 /*
36  * --------------------------------------------------
37  * ---------------------- Link ----------------------
38  * --------------------------------------------------
39  */
40 
41 class Link : public Behaviour
42 {
43  VizBlocks* _node;
44  String _peerId;
45  const int _timeoutInterval = 5000;
46  unsigned long _t = 0;
47 
48 public:
49  Link(VizBlocks* node, String name = "Link") : Behaviour(name), _node(node)
50  {
51  _background = true;
52  }
53 
57  char* args()
58  {
59  return "<String peerId>";
60  };
61 
65  String start(String args)
66  {
67  _running = true;
68 
69  if (args == name() || args.indexOf(" ")>0)
70  {
71  return "Invalid args (" + args + ") in behaviour " + name();
72  }
73 
74  _t = millis();
75 
76  if (args == _peerId)
77  {
78  return "Link ping from (" + _peerId + ")";
79  }
80 
81  _peerId = args;
82 
83  String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
84  _node->announce(str);
85 
86  return "New link with (" + _peerId + ")";
87  }
88 
92  void update()
93  {
94  if (millis() > (_t+_timeoutInterval))
95  {
96  String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
97  _node->announce(str);
98  _peerId = "";
99  _running = false;
100  }
101  }
102 
103 };
104 
105 /*
106  * --------------------------------------------------
107  * ------------------- PingServer -------------------
108  * --------------------------------------------------
109  */
110 
111 class PingServer : public Behaviour
112 {
113  VizBlocks* _node;
114  String str;
115  const int _interval = 4000;
116  unsigned long _t = 0;
117 
118 public:
119  PingServer(VizBlocks* node, String name = "PingServer");
120  String start(String args);
121  void update();
122 
123 };
124 
125 #endif
SendCapabilities::SendCapabilities
SendCapabilities(VizBlocks *node, String name="SendCapabilities")
Definition: CommsBehaviours.hpp:18
Behaviour::_background
boolean _background
Definition: Behaviours.hpp:15
Behaviour::name
virtual String name()
Behaviour::args
virtual char * args()
PingServer::start
String start(String args)
Behaviour::_name
String _name
Definition: Behaviours.hpp:16
Behaviour
Definition: Behaviours.hpp:9
SendCapabilities
Definition: CommsBehaviours.hpp:14
PingServer
Definition: CommsBehaviours.hpp:112
SendCapabilities::start
String start(String args)
What does this do?
Definition: CommsBehaviours.hpp:25
Behaviour::_running
boolean _running
Definition: Behaviours.hpp:14
VizBlocks
Definition: VizBlocks.hpp:18
_node
ButtonPressed * _node
PingServer::update
void update()
PingServer::PingServer
PingServer(VizBlocks *node, String name="PingServer")