added checking of result when building demos
[urisagit/Stem.git] / Design / registry_notes
CommitLineData
4536f655 1
2 Stem Cell Registry and Message Address Design Notes
3
4The heart of Stem is the messaging subsystem and the heart of that is
5the registry. This is where all knowledge of how to address cells is
6located. Each cell gets registered by it name and optionally its target
7and messages are directed to it via its names. The decisions made by the
8registry when delivering a message are described here as well as the API
9and other related issues and modules.
10
11
12Stem Message Addresses
13
14Stem messages are sent to registered cells by using an address triplet:
15the hub name, the cell name and the target name.
16
17A hub is a single process running Stem. Its name must be unique among all
18Stem hubs in a single connected net. A hub consists of a set of objects
19and Stem cells. It contains the message registry, the core Stem system
20and it will load other modules on demand.
21
22A Stem cell is a single object in a Stem hub which has registered itself
23under a name and can receive messages via its methods. Not all objects
24in Stem are cells, but all Stem cells are objects. Cells are commonly
25registered by the Stem::Config system or by a parent cell spawning
26targeted cells. Only one cell can be registered in a hub for a given
27cell name. One unusual trick is that a whole class can register itself
28as a cell by using its class name as the object and some fixed string as
29the name (sometimes that is the class name as well). There can only be
30one cell by that class and name but there can be aliases for any cell
31name. That is used by cells which must be implemented with class level
32data.
33
34The target is the last part of an address and is optional. A given cell
35could be registered with a cell name and target and it can send and
36receive messages with its own globally unique address. The cell name is
37either the parent's cell name or a fixed one for the particular class
38(the Stem::Log::Filter class does this). The target name is commonly
39either a Stem::Id value or a name from a configuration. Another use for
40the target is a cell such as Stem::Switch which uses it to address its
41input/output maps. The use of the target is defined by the design of the
42cell.
43
44Message Delivery
45
46The first step in delivering a message is finding out which cell it goes
47to. This is done by looking up the cell that matches the hub/name/target
48address in the message. This is a multistep procedure with the following
49rules:
50
51If the hub name of the message is set and it is not the name of this
52hub, locate the portal that can send to that hub and deliver the message
53to that portal. Portal names are in a different namespace as regular
54cells but portals can also be registered as targeted cells so they can
55have commands sent to them. See more on Portals below.
56
57If the message has a cell name and an optional target name, the cell is
58looked up in the local registry. Cells with just a cell name don't share
59the namespace with cells that have cell and target names. If the cell is
60found the message is delivered by a method. (See how that is chosen
61below.)
62
63If the cell is not found locally it is sent out via a portal with the
64alias DEFAULT. This portal should be connected to a hub which would know
65how to direct the message to the proper destination cell. Typically a
66Stem hub that is a TCP client to a more central server hub will just
67have its portal to the server aliased to DEFAULT.
68
69If the message has the local hub name and couldn't be delivered, it is
70logged and thrown away. Optionally a delivery failure message could be
71sent back to the originator. But this is not the Internet and bounces
72can be automatically fixed in Stem.
73
74NOTE: This brings up the whole subject of message routing. I have been
75thinking about this issue for a while and it is not as tricky as the
76Internet because of several things. First, we can cheat. Stem is
77completely in charge of its routing so it can be smart about itself and
78not deal with worst case situations like the net. A hub can be
79configured to distribute routing information that supports the network
80topology. The discovery of the network and its topology can also be
81automated by a booting Stem network, even from a virgin boot. Remote
82Stem hubs could be installed with minimal (and not customized)
83configurations which will cause itself to connect to a server hub and
84download the real configuration. This simplifies deployment of Stem to a
85new set of boxes. Much more on this subject will be in another design
86notes file.
87
88
89Choosing the Cell Method
90
91Once the destination cell of a message is determined, you then have to
92find out its best method to call to deliver that message. Stem's
93messages can be delivered via a generic method (e.g. 'msg_in') which is
94expected to take any type of message, or via specific methods
95(e.g. 'data_in') which handle selected messages. Here are the rules for
96determining the cell method to call.
97
98If the message type is 'cmd' with a command 'foo' and there is a cell
99method 'foo_cmd', the message is delivered via that method. If a command
100message is delivered via a command method and a value is returned, that
101value is sent back to the 'from' address in a response message.
102
103For all other message types, if the Cell has a method that is the type
104name with '_in' appended to it, that method is used for delivery,
105e.g.; if the message type is 'data', and if the cell has a method named
106'data_in', that is called with the message as its sole argument.
107
108If the message is not delivered by any of those special methods, it will
109be delivered to the generic method 'msg_in'. This method should exist in
110every cell (except those that have the special methods cover all their
111message types). The method delivery lookup simplifies writing Cells by
112moving the internal dispatching code from the Cell to the registry.
113
114
115
116Stem::Id is a simple module designed to manage a set of unique IDs for
117its owner object, i.e.; it is used by the Stem::SockMsg modules
118to register all of its accepted/connected sockets with unique targets.
119
120Stem::Portal is the class that send messages between hubs over
121pipes. These pipes can be direct sockets or indirect through a secure
122transport such as ssh or stunnel. It receives messages vis the 'send'
123method which are then converted to a string form and written out the
124pipe. The stringify format is currently Data::Dumper but it can be set
125via the configuration of the portal to use Storable, XML or something
126else. Each stringified message is prefixed with 1 or 2 lines containing
127its size and format. Incoming message strings are converted back into
128internal messages and then delivered locally by calling dispatch on
129them. Portals can use any communications channel as long as it gets read
130and write handles. This means that new security and transport protocols
131can be integrated easily into the portal.