Send/receive parameters

Most basic (but most important) feature is remote sending and receiving parameters by request and response objects - everything in Skynet is controlled by this.
With Skynet you can setting single parameters like:

foo:"bar";

or more at once:

foo:"bar1";
foo2:"bar2";
foo3:"bar3";

All parameters sending and receiving by Skynet are encrypted by default. You can disable data encryption in config file (not recommended) or implements you own algorythms for encryption (at default only simple base64 is included). All of parameters are sending by one single request (or response) - Skynet is packing them into one packet. How it works?

You can set parameters by three different ways:

- from PHP code via Event Listeners
- from webconsole
- from CLI

Sending parameters from Event Listener:


Let's imagine that we have 2 clusters: skynet1.php and skynet2.php and skynet1.php must send information about foo to skynet2.php.
To do this, skynet1.php must prepare request to be send, sets parameter (field) and assign to them value with time and sends prepared request to skynet2.php
Skynet use special classes called Event Listeners for operating on requesting and receiving data, so all we need to do is to assign our parameter foo to event:

onRequest()

This event is launches two times on both sides - when request is creating (on sender cluster) and when request is received (on receiver). As you see, every event is fired 2 times in 2 different context - as sender and as a receiver.

This two contexts are named in Skynet as:

- beforeSend
- afterReceive

Information about actual context is getting as argument in every event.
Depends on this information you can prepare code for 'sender side' and for 'receiver side'.

To prepare request (on sender cluster) with our parameter foo we must use context "beforeSend", becose this context is used when request is under preparation.

http://someserver.com/skynet1.php or http://someserver.com/src/SkynetUser/MyListener.php:
public function onRequest($context)
{
if($context == "beforeSend")
{
$this->request->set('foo', 'bar');
}
}

That's it. We creates new parameter (field) called foo and set this to bar.
Request will be send when Skynet will be broadcasting another clusters (or via single connection if you specify it). Sended parameter will be available on receiver cluster request object from context afterReceive:

http://anotherserver.com/skynet2.php or http://anotherserver.com/src/SkynetUser/MyListener.php:
public function onRequest($context)
{
if($context == "afterReceive")
{
$foo = $this->request->get('foo');
}
}

And now the receiver cluster can prepare response for request above. We will use for that onResponse() event witch works in two context too:

http://anotherserver.com/skynet2.php or http://anotherserver.com/src/SkynetUser/MyListener.php:
public function onResponse($context)
{
if($context == "beforeSend")
{
$foo = $this->request->get('foo');
if($foo !== null)
{
$this->response->set('thanks', 'Thank you!');
}
}
}

That's it. Request receiver (skynet2.php) will now respond with parameter (field) thanks setted to Thank you!.
Now, when a request sender cluster (skynet1.php) receives the response, parameter thanks will be available in its response object in context afterReceive:

http://someserver.com/skynet1.php or http://someserver.com/src/SkynetUser/MyListener.php:
public function onResponse($context)
{
if($context == "afterReceive")
{
$responseThanks = $this->response->get('thanks');
if($responseThanks !== null)
{
/* Nice. */
}
}
}

If you want to specify only one cluster receiver when preparing request you should use command:

@to "CLUSTER_ADDRESS";

Example for specify skynet2.php cluster as only receiver of our request:
http://someserver.com/skynet1.php or http://someserver.com/src/SkynetUser/MyListener.php:
public function onRequest($context)
{
if($context == "beforeSend")
{
$this->request->set('foo', 'bar');
$this->request->set('@to', 'http://localhost/skynet/skynet2.php');
}
}

The @to command specifying receiver cluster. Only this cluster will receive prepared request as above.

Another way is to send parameters via webconsole and CLI mode - those two methods are described more complexed in section "Console & CLI Mode".

Sending parameters from webconsole:


If you want send parameter via webconsole type:

param:"value";

and click on "Send request".
In our case this could be, e.g.:

foo:"bar";

Remember that if you sending more than one parameter via webconsole, you must set every parameter in new line and separate them by ";", e.g.:

foo:"bar"; 
foo2:"bar2";
foo3:"bar3";

Then when you click on "Send request" three parameters (fields) will be send in single request.
All parameters values should be quoted by ["] char.

Sending parameters from CLI mode:


In PHP CLI mode, when you launch Skynet this way you will need to use:

php skynet.php -send "foo:'bar'; foo2:'bar2';"
Full API Documentation is included in Skynet packages available on GitHub.