Skip to content
Snippets Groups Projects
CommsBehaviours.cpp 1.58 KiB
Newer Older
SendCapabilities::SendCapabilities(VizBlocks* node, String name = "SendCapabilities") :
  Behaviour(name), _node(node)
{ }

Matthew's avatar
Matthew committed
int SendCapabilities::start(String args)
{
	 //This is where you do your stuff for a simple behaviour
	_node->announce_capabilities();

	return "SendCapabilities behaviour " + _name;
Matthew's avatar
Matthew committed
PingServer::PingServer(VizBlocks* node, String name = "PingServer") : Behaviour(name), _node(node)
{
	_background = true;
}

void PingServer::update()
{
	if (millis() > (_t+_interval))
	{
		_t = millis();
		_node->announce(str);
	}
Matthew's avatar
Matthew committed

int PingServer::start(String args)
{
	_background = true;
	_running = true;
	_t = millis();
	str = "{\"id\":\"" + String(_node->getId()) + "\",\"PingServer\":{}}";
	_node->announce(str);

	return "Pinging server";
Matthew's avatar
Matthew committed

Link::	Link(VizBlocks* node, String name = "Link") : Behaviour(name), _node(node)
	{
		_background = true;
	}


Matthew's avatar
Matthew committed
void Link::update()
{
	if (millis() > (_t+_timeoutInterval))
	{
		String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
		_node->announce(str);
		_peerId = "";
		_running = false;
	}
Matthew's avatar
Matthew committed
int Link::start(String args)
{
	_running = true;

	if (args == name() || args.indexOf(" ")>0)
	{
		return "Invalid args (" + args + ") in behaviour " + name();
	}
Matthew's avatar
Matthew committed
	_t = millis();
Matthew's avatar
Matthew committed
	if (args == _peerId)
	{
		return "Link ping from (" + _peerId + ")";
	}
Matthew's avatar
Matthew committed
	_peerId = args;
Matthew's avatar
Matthew committed
	String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
	_node->announce(str);

	return "New link with (" + _peerId + ")";
Matthew's avatar
Matthew committed

char * Link::args()
{
	return "<String peerId>";