In code, those 2 objects are available as:
use Skynet\Data\SkynetRequest; $request = new SkynetRequest();
and
use Skynet\Data\SkynetResponse; $response = new SkynetResponse();
In Event Listeners by easy to use:
$this->request
and
$this->response
Every Event Listener has access to them and can manipulate sended/received via those objects data. Request object creates, parses and manipulates on sending to another cluster data. This object offers easy access to all fields sending to another clusters. Every of those fields are params with pairs "key:value" whitch can be receive by another cluster and assigned to their request object. Skynet has some default built-in Event Listeners works on requesting and requested data and you can easily create your own listeners. By the way, this is an idea of Skynet - to create listeners and work with them under framework.
Look at an example:
Code below creates new request object and sets field called 'foo' with value 'bar':
use Skynet\Data\SkynetRequest; $request = new SkynetRequest(); $request->set('foo', 'bar');
Code below will receive value of field 'foo' from request object:
$foo = $request->get('foo');
This is the simpliest usage and it's operates on one single key:valuerequest field.
With Skynet you can create more specified fields as arrays of params. Code below creates an array of two parameters in single request:
use Skynet\Data\SkynetRequest; $request = new SkynetRequest(); $request->set('foo', array('key1' => 'bar1', 'key2' => 'bar2'));
At next, you can receive array like that by:
$foo = $request->get('foo'); $key1 = $foo['key1']; $key2 = $foo['key2'];
If a request field is not set then:
$foo = $request->get('FIELD_KEY');
always returns NULL, so you can use this to check if any field exists in request, like below:
if($request->get('FIELD_KEY') !== null) { /* do something */ }
Code above sets single parameter (imagine that this is a single variable) whitch will be send to clusters, but you will also use Skynet to sending commands (they are documented in next sections).
Commands can take more that one parameter per command, e.g. if you using command @opt_set then you can specifiy more that one field to one command.
Take a look at example:
use Skynet\Data\SkynetRequest; $request = new SkynetRequest(); $request->set('@reg_set', array('key1' => 'bar1', 'key2' => 'bar2');
And now:
key1:"bar1" and key2:"bar2"
are still a single parameter. So, with Skynet you can send more parameter where every parameter is an array, let's see an example:
use Skynet\Data\SkynetRequest; $request = new SkynetRequest(); $param1 = array('key1' => 'bar1', 'key2' => 'bar2'); $param2 = array('key3' => 'bar3', 'key4' => 'bar4'); $request->set('@reg_set', array($param1, $param2);
Every parametr can be set from 3 different ways:
- in PHP code (as above)
- via webconsole
- via CLI mode
Code above is equals to setting parameters from webconsole, e.g.:
key1:"value1"; key2:"value2";
and equals to the same in CLI mode:
php skynet.php -b -send "key1:'value1'; key2:'value2';"
The Request object works in 2 ways:
- on "sender" side you are using request object to create request witch will be send to other clusters
- on "receiver" side cluster receives request from sender and operates on sended data by own request object just like above.
So, if you sets a field called foo on sender cluster:
use Skynet\Data\SkynetRequest; $request = new SkynetRequest(); $request->set('foo', 'bar');
After send this request to receiver then receiver will be have access to field foo by own request object:
use Skynet\Data\SkynetRequest; $request = new SkynetRequest(); $foo = $request->get('foo');
You will see more about using Request in Event Listeners section.
At this time you should know that in Event Listeners you have simpliest access to request object by:
$this->request
When request is prepared (it can have lot of fields, above we are only sending one for an example) then you can send request to one specified cluster or whole clusters network.