2013-11-28 05:06:36 +08:00
|
|
|
# Master
|
|
|
|
|
|
|
|
For this example, we're going to provide a simple demonstration of how Cylon's
|
|
|
|
master functionality works. Cylon can be fed an arbitrary number of robots, and
|
|
|
|
it will then take care of all of them, starting and stopping all of them as
|
|
|
|
needed.
|
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
First, let's load up Cylon:
|
2013-11-28 05:06:36 +08:00
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
var Cylon = require('../..');
|
2013-11-28 05:06:36 +08:00
|
|
|
|
|
|
|
With that in place, now we can start defining our robots. They'll all behave
|
2014-03-27 08:06:26 +08:00
|
|
|
similarly, but have unique characteristics (their name), so let's
|
|
|
|
define an array to hold these names for now. Later, we can instantiate
|
2013-11-28 05:06:36 +08:00
|
|
|
a base robot and change it's attributes as needed.
|
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
var bots = [ 'Huey', 'Dewey', 'Louie' ];
|
2013-11-28 05:06:36 +08:00
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
Now we can define our `MinionBot` class. This will be the base
|
2013-11-28 05:25:30 +08:00
|
|
|
class for all three of the robots.
|
2013-11-28 05:06:36 +08:00
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
var MinionBot = (function() {
|
|
|
|
function MinionBot() {};
|
2013-11-28 05:06:36 +08:00
|
|
|
|
|
|
|
We'll just give our robots some basic work so we can tell they're actually
|
|
|
|
working:
|
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
MinionBot.prototype.work = function(my) {
|
|
|
|
console.log("Robot " + my.name + " is now working!");
|
|
|
|
};
|
|
|
|
|
|
|
|
return MinionBot;
|
|
|
|
|
|
|
|
})();
|
2013-11-28 05:06:36 +08:00
|
|
|
|
|
|
|
And that's all we need for that.
|
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
Next up, we'll create Cylon robots by making an instance of the `MinionBot`
|
2013-11-28 05:25:30 +08:00
|
|
|
class, and modifying the attributes that are unique to each robot. After the
|
2013-11-28 05:06:36 +08:00
|
|
|
customized robot is ready, we'll feed it into Cylon.
|
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
for (var i = 0; i < bots.length; i++) {
|
|
|
|
var robot = new MinionBot;
|
|
|
|
robot.name = bots[i];
|
|
|
|
Cylon.robot(robot);
|
|
|
|
}
|
2013-11-28 05:06:36 +08:00
|
|
|
|
|
|
|
And now Cylon knows about all the robots we care about for this example, and
|
|
|
|
what they do. All that's left is to start them all. The `.start()` method on
|
|
|
|
Cylon triggers the `.start()` command on all the robots we've told Cylon about,
|
|
|
|
so all three robots will start at once.
|
|
|
|
|
2014-03-27 08:06:26 +08:00
|
|
|
Cylon.start();
|