The Message-Driven Finite-State Machine

An MFM comprises one or more identical data sets, each of which is serviced by a common set of message processing functions. Each such data set alone is known as an 'instance' of the MFM concerned. The diagram shows an example of a four-state three-instance MFM:

At a given time, an MFM 'instance' can only be in one of a small (finite) number of discrete 'states'. Hence the term Finite-State in its name. For example, an instance of a typical three-state MFM could at any given time be in its initialisation state, its main state or its termination state.

The particular processing which is required to be done when an MFM instance is in a given state is performed by a corresponding message processing function [MPF]. Thus, each MFM state has an associated 'C' function which does the processing required when an instance of the MFM is in that state.

A message processing function receives a message, does some processing on it, updates the MFM's instance data, and then outputs the results as another message. A ROBOS-based application is therefore driven entirely by messages which are continually received into and dispatched from a single central message queue. At the heart of a Robos is therefore a message queue and a message dispatcher function which together form a central Message Exchange.

Generic Structure of an MFM

MFMs are written one MFM per source file. An MFM source file contains the following parts:

Part 1: A definition of the structure of its instance data followed by a declaration of storage for all its data instances:

struct instance_data {
  int machine_state;          //Current machine-state
  ...
  ...
  ...
  ...
  ...
} instances[16];              //Declare 16 instances
If, for a particular application, the maximum number of data instances is unknown or fluid, storage can be allocated dynamically as and when required. This would be done by a call similar to the following:
struct instance_data _far 
  *hInst = (struct instance_data _far *)
           _fmalloc(sizeof(struct instance_data));
Part 2: A prototyped declaration of the message processing functions corresponding to its possible machine-states:
int _far MPF00100(void _far *pd_in) {     // State 0
  .....
  .....
  .....
  .....
}

int _far MPF00101(void _far *pd_in) {     // State 1
  .....
  .....
  .....
  .....
}

int _far MPF00102(void _far *pd_in) {     // State 2
  .....
  .....
  .....
  .....
}

int _far MPF00103(void _far *pd_in) {     // State 3
  .....
  .....
  .....
  .....
}

This page's parent within this Web Site. About this Web Site. Its home page. Email its Author.