added check for connected when triggered method is called. can't trigger
[urisagit/Stem.git] / Design / cell_notes
CommitLineData
4536f655 1
2 Stem Cell Design Notes
3
4Stem Cells are the fundamental working unit in a Stem application.
5Cells are Perl objects that have 3 primary characteristics: First, they
6must be registered with a Stem address. Second, they must have public
7methods that can take an incoming message as an argument. And third, a
8cell must be able to generate messages. There are three major types of
9Stem Cells: class, object and cloned Cells. These are described in
10further detail below.
11
12Class Cells are created by a Stem class which registers itself (using
13the Stem::Route::register_class routine at module load time) and are
14always registered using its class name. Class Cells are typically
15created by modules which manage some global resource and don't need to
16have multiple object instances created. A common reason for this is a
17module which has a 'status_cmd' (or similar) method that is used to
18get the status of the whole module. The Class Cell registration makes
19those methods accessible via messages. Some Stem classes such as
20Stem::Conf, Stem::Portal, Stem::Msg are Class Cells. Some modules can be
21a Class Cell and also create Object Cells. Class Cells can optionally
22be registered with aliases. The aliases make it easier to send a command
23message from the terminal (using Stem::TtyMsg) to a class Cell
24(Stem::Route is aliased to 'reg', Stem::Cron is aliased to 'cron').
25
26Object Cells are objects that are created by a class's constructor and
27are then registered with the Stem::Route::register_cell routine. The
28registration takes the object and a required name (unique to this Hub).
29Most often an Object Cell is created by a configuration but any module
30can construct an object and register it. Since configurations can be
31loaded from files and executed anywhere, Stem Cells can be configured at
32any time during the existance of the current Stem system.
33
34Cloned Cells are only created by existing parent Object Cells. (Parent
35Cells are Object Cells set up to create Cloned Cells). When the parent
36Cell gets some form of trigger (typically a socket connection or a
37special command message), it makes a clone of itself and does whatever
38special work the cloned object needs. The parent Cell owns a Stem::Id
39object which it calls to generate a unique (within this Cell) Target
40name which it uses to register the cloned Cell. So the new Cell has a
41unique Cell/Target name pair which can be used in messages directed at
42it. In a typical case, the new Cloned Cell will send a message elsewhere
43informing some other object about its existance; e.g., The Stem::SockMsg
44class can be configured to clone itself when a socket connection is made and
45then it will send a 'pipe_start' command message out. In an Inetd
46configuration that message would be directed to a parent Stem::Proc
47Object Cell which will clone itself and fork a process. This clone will
48respond to the SockMsg message with its new target address, thereby
49setting up a Stem pipe between the socket and process. When either the
50process exits or the socket is closed, the cloned Cells are notified and
51they clean up and unregister themselves.
52
53You can always find the current set of Cells in a Hub by sending a
54'status' command message to the Class Cell Stem::Route. This is also
55registered with the alias of 'reg'. So from the terminal (if your Hub
56has configured in Stem::TtyMsg) you can type:
57
58reg status
59
60to get the registered Cells in this Hub or
61
62:hubname:reg status
63
64to get the Cells in a remote Hub.
65
66
67When a Stem message is delivered in a Hub, its 'to' address is looked up
68in the Hub's registry and the message is delivered to the destination
69Cell via a method. A Cell must have some well known methods that handle
70incoming messages. These method names have well defined formats and
71uses. In general there are three groups of incoming message methods. All
72of these delivery methods get passed the message itself as their sole
73argument.
74
75The first group of delivery methods are those that handle command
76messages. These are named for the command (the 'cmd' field of a command
77message) with '_cmd' appended. So a foo command message will be
78delivered to the foo_cmd method if it exists in the destination Cell. If
79a command message method returns a value, it is automatically sent back
80to the originating Cell in a response message.
81
82The second group handles all other message types. They are named for the
83message type with a suffix of '_in'. So a 'foo' type message would be
84delivered to the 'foo_in' method if it exists in the destination Cell. A
85very common message type is 'data' and it gets delivered to the
86'data_in' method.
87
88The final group has the single method 'msg_in' which is used if no other
89method can handle the message. This is the default message delivery
90method. You can have a Cell with just this method and it should be
91designed to handle all expected message types.
92
93The use of specific delivery methods is not critical but it encourages
94cleaner Cell design by having methods focus on doing work and not on
95deciding how to handle different message types. This is in keeping with
96the Stem design philosophy of doing as much common work as possible
97behind the scenes, while leaving only the problem specific work to the
98Cell.
99
100There are no design considerations for sending messages from a Cell. It
101just creates a message object with the class call Stem::Msg->new and
102then dispatches it. If the message doesn't have the 'from' address set,
103it will default to the address of the current Cell (if it is known). If
104the code that generates a new message is not a registered Cell, then you
105must specify the 'from' address as one can't be deduced.
106
107For more on Cell addresses see registry_notes and message_notes.