faudes::nDevice Class Reference
[I/O Device PlugIn]

An nDevice implements networked IO via a simple TCP/IP protocol. More...

#include <iop_simplenet.h>

List of all members.

Classes

struct  ClientState
 Background: state of a connection to a client (shared). More...
struct  ServerState
 Background: state of a connection to an event server (shared). More...

Public Member Functions

 nDevice (void)
 Default constructor.
virtual ~nDevice (void)
 Explicit destructor.
virtual void Clear (void)
 Clear all configuration.
void ServerAddress (const std::string &rAddr)
 Set server address of this node.
void BroadcastAddress (const std::string &rAddr)
 Set broadcast address for address resolution Note: you can only set the broadcast address while the device is down.
void NetworkName (const std::string &rNetwork)
 Set network name to participate.
void InsNode (const std::string &rNodeName)
 Add a node to the network configuration.
void InsNodeAddress (const std::string &rNode, const std::string &rAddress)
 Add entry to node name resolution.
void ClearNodes (void)
 Add a node to the network configuration.
void InsInputEvent (const std::string &event)
 Insert event as input event.
void InsOutputEvent (const std::string &event)
 Insert event as output event.
virtual void Compile (void)
 Set up internal data structures.
virtual void Start (void)
 Activate the device.
virtual void Stop (void)
 Deactivate the device.
virtual void Reset (void)
 Reset device.
virtual void WriteOutput (Idx output)
 Run output command.

Protected Member Functions

virtual void DoReadPreface (TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
 Actual method to read device configuration from tokenreader.
virtual void DoWritePreface (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 Actual method to write the device configuration to a TokenWriter.

Protected Attributes

TaNameSet
< AttributeSimplenetEvent > * 
pConfiguration
 Overall configuration (with actual type).
std::string mNetwork
 Simplenet: network id.
SimplenetAddress mListenAddress
 Simplenet: address of my server incl port (localhost:40000).
SimplenetAddress mBroadcastAddress
 Simplenet: address for udp broadcast (255.255.255.255:40000.
SimplenetAddress mEffectiveListenAddress
 Simplenet: effective address of my server port.
std::map< std::string,
std::string > 
mNetworkNodes
 Simplenet: list of nodes in this network incl default addresses.
std::map< std::string, EventSetmInputSubscriptions
 Compiled data: map subscriptions.
faudes_mutex_t mMutex
 Background: mutex for below shared variables.
faudes_thread_t mThreadListen
 Background: thread handle (global).
bool mStopListen
 Background: request to join via flag (mutexed).
int mListenSocket
 Background: server socket to listen (background only).
int mBroadcastSocket
 Background: udp broadcast socket (background only).
std::map< int, ClientStatemOutputClientStates
 Background: map sockets to connection states (shared).
std::map< std::string,
ServerState
mInputServerStates
 Background: connection states to event servers (by node name).

Friends

void * NDeviceListen (void *)
void * NDeviceServer (void *)
void * NDeviceReply (void *)

Detailed Description

An nDevice implements networked IO via a simple TCP/IP protocol.

Networking

The purpose of this implementation of networked discrete events is to provide a basis for case studies. However, simple networked events via nDevice must not be confused with ongoing research that addresses synchronisation and real time behaviour. We take a really pragmatic approach here.

Our network is identified by its network name. It consists a number of nodes, each identified by its node name. In the current implementation, each node knows about the entire network ie knows about all other nodes. Each node plays two roles:

  • the server role to send event notifications, and
  • the client role to receive event notifications.

In their server role, each node is configured to listen on its server port for incomming TCP connections. When a client (i.e. some other node in client role) connects to the TCP port, the server replies to a simple command set in order to allow the client to subscribe to the nodes output events. When the application executes an output event on the node, this is notified to all connected clients and the clients will interpret the event as an input reading. Vice versa, the each node itself has the role of a client and subscribes to relevant output events served by other nodes in their server role.

Protocol Details

The protocol for commands and notification is libFAUDES-token based and can be inspected eg via nc or telnet; see the shell script in the tutorial. Event notifications are sent spontaneous to all connected clients in the format <Notify> Ev </Notify>. Commands are accepted in the format <Cmd> Command </Cmd> and will be answered accordingly.

Command via TCP Reply
<Cmd> Info </Cmd> configuration of node as token string (<SimplenetDevice name="SimpleMachine"> ... </SimplenetDevice> )
<Cmd> Status </Cmd> acknowledgement with status string (<Ack> Up </Ack>, <Ack> StartUp </Ack> or <Ack> ShutDown </Ack>, respectively; will not connect/reply while Down)
<Cmd> ResetRequest </Cmd> no acknowledgement
<Subscribe> Ev_1 ... Ev_N </Subscribe> subset of available events (e.g. <Subscribed> Ev_2 Ev_7 Ev_9 </Subscribes>)

A minimal alternative implementation for a node consists of (1) a TCP server that ignores all incomming messages and issues event notifications to any relevant events; and, (2) a TCP client that subscribes to all events and then listens to event notifications. All other commands are optional and may change in future revisions of this protocol.

Name Resolution

On the technical side, each node needs to figure the IP addresses incl TCP ports on which the other nodes provide their service. To ease configuration, this information is distributed by UDP datagrams. In order to locate the other nodes within the network, a node may broadcasts a network request datagramm. Each node that receives such a request, replies with an advert datagramm to provide its address. Thus, the simple net nodes rely on some underlying name resolution by DNS services, but node configuration itself refers to simple-net node names only. Since each node knows about the names of all participating nodes, each node will know when all connections are up.

By default, UDP broadcasts are addressed to 255.255.255.255:40000. Since routers are not meant to pass-on broadcasts, nDevice name resolution is restricted to one subnet. If the local host is connected to multiple subnets, you need to specify the relevant subset explicitly by setting the appropriate broadcast address, e.g. 192.168.2.255:40000. To restrict networks to the local machine, set the broadcast address to the loopback device 127.0.0.1:40000. To span a network accross multiple subnets, server addresses can be explicitly specified as an attribute in the node configuration; e.g. <Node> name="SimpleSupervisor" address="192.168.2.1:40000"</Node>. This is also the preferred fallback when address resolution fails for other reasons.

Broadcast via UDP Reply
<Request> network reqnode </Request> advertisement of networkname, nodename and ip-address:port, e.g., <Advert> SimpleLoop SimpleSupervisor 192.168.2.1:40000 </Advert>

File IO

For token IO, the nDevice reads and writes a section with label "SimplenetDevice". There are no relevant attributes yet. Simple machine example:

 <SimplenetDevice name="SimpleMachine"> 

 <!-- Time scale in ms/ftiu -->
 <TimeScale value="1000"/> 

 <!-- IP address of this node, incl. server TCP port -->
 <ServerAddress value="localhost:40000"/> 

 <!-- Broadcaset address for node resolution (optional)
 <BroadcastAddress value="255.255.255.255:40000"/> 

 <!-- Network topology -->
 <Network name="SimpleLoop"> 
 <Node name="SimpleMachine"/> 
 <Node name="SimpleSupervisor"/> 
 </Network> 

 <!-- Event configuration -->
 <EventConfiguration> 
 <Event name="alpha" iotype="input"/> 
 <Event name="beta" iotype="output"/> 
 <Event name="mue" iotype="output"/> 
 <Event name="lambda" iotype="input"/> 
 </EventConfiguration> 

 </SimplenetDevice> 

Implementation Notes

The current status of the code is premature; network io assumes reasonably large buffers; thread/select mechanism is inefficient; exception handling wont work; etc etc

Definition at line 435 of file iop_simplenet.h.


Constructor & Destructor Documentation

faudes::nDevice::nDevice ( void   ) 

Default constructor.

Definition at line 265 of file iop_simplenet.cpp.

faudes::nDevice::~nDevice ( void   )  [virtual]

Explicit destructor.

Definition at line 283 of file iop_simplenet.cpp.


Member Function Documentation

void faudes::nDevice::BroadcastAddress ( const std::string &  rAddr  ) 

Set broadcast address for address resolution Note: you can only set the broadcast address while the device is down.

Parameters:
rAddr Address for UDP broadcasts e.g. "255.255.255.255:40000"
Exceptions:
Exception 
  • No valid address (id 551) (NOT IMPLEMENTED)

Definition at line 308 of file iop_simplenet.cpp.

void faudes::nDevice::Clear ( void   )  [virtual]

Clear all configuration.

This implies Stop().

Reimplemented from faudes::vDevice.

Definition at line 292 of file iop_simplenet.cpp.

void faudes::nDevice::ClearNodes ( void   ) 

Add a node to the network configuration.

Note: you can only configure the network while the device is down.

Definition at line 333 of file iop_simplenet.cpp.

void faudes::nDevice::Compile ( void   )  [virtual]

Set up internal data structures.

Reimplemented from faudes::vDevice.

Definition at line 358 of file iop_simplenet.cpp.

void faudes::nDevice::DoReadPreface ( TokenReader rTr,
const std::string &  rLabel = "",
const Type pContext = 0 
) [protected, virtual]

Actual method to read device configuration from tokenreader.

This method calls the base class to read the device name and the the timescale. It then reads address, networkid and nodes. Overall device configuration is consists of DoReadPreface, DoReadConfiguration and Compile. It isimplemented in vDevice. The label and context parameters are ignored.

Parameters:
rTr TokenReader to read from
rLabel Section to read
pContext Read context to provide contextual information
Exceptions:
Exception 
  • IO error (id 1)

Reimplemented from faudes::vDevice.

Definition at line 399 of file iop_simplenet.cpp.

void faudes::nDevice::DoWritePreface ( TokenWriter rTw,
const std::string &  rLabel = "",
const Type pContext = 0 
) const [protected, virtual]

Actual method to write the device configuration to a TokenWriter.

This method calls the base class vDevice to write the device name and the time scale. It then writes network related data. The label and context parameters are ignored.

Parameters:
rTw Reference to TokenWriter
rLabel Label of section to write
pContext Read context to provide contextual information
Exceptions:
Exception 
  • IO errors (id 2)

Reimplemented from faudes::vDevice.

Definition at line 367 of file iop_simplenet.cpp.

void faudes::nDevice::InsInputEvent ( const std::string &  event  ) 

Insert event as input event.

Note: you can only configure events while the device is down.

Parameters:
event Event by name.

Definition at line 339 of file iop_simplenet.cpp.

void faudes::nDevice::InsNode ( const std::string &  rNodeName  ) 

Add a node to the network configuration.

Note: you can only configure the network while the device is down.

Parameters:
rNodeName Node to participate in wthe network.

Definition at line 321 of file iop_simplenet.cpp.

void faudes::nDevice::InsNodeAddress ( const std::string &  rNode,
const std::string &  rAddress 
)

Add entry to node name resolution.

Note: you can only configure the network while the device is down.

Parameters:
rNode Name of node to register
rAddress Address incl port

Definition at line 327 of file iop_simplenet.cpp.

void faudes::nDevice::InsOutputEvent ( const std::string &  event  ) 

Insert event as output event.

Note: you can only configure event while the device is down.

Parameters:
event Event by name.

Definition at line 348 of file iop_simplenet.cpp.

void faudes::nDevice::NetworkName ( const std::string &  rNetwork  ) 

Set network name to participate.

Note: you can only set the network name while the device is down.

Parameters:
rNetwork Name of network, e.g. "ElevatorNetwork"

Definition at line 315 of file iop_simplenet.cpp.

void faudes::nDevice::Reset ( void   )  [virtual]

Reset device.

Resets buffered input events and current time.

Reimplemented from faudes::vDevice.

Definition at line 1273 of file iop_simplenet.cpp.

void faudes::nDevice::ServerAddress ( const std::string &  rAddr  ) 

Set server address of this node.

Note: you can only set th server address while the device is down.

Parameters:
rAddr Address on which to run this server, e.g. "localhost:40000"
Exceptions:
Exception 
  • No valid address (id 551) (NOT IMPLEMENTED)

Definition at line 302 of file iop_simplenet.cpp.

void faudes::nDevice::Start ( void   )  [virtual]

Activate the device.

This function enables output execution and input reading. It starts the background thread for incomming connections and tries to connect to relevant servers.

Exceptions:
Exception 
  • Not yet configured (id 551)
  • Fatal network error (id 553)
  • Fatal thread error (id 554)

Reimplemented from faudes::vDevice.

Definition at line 536 of file iop_simplenet.cpp.

void faudes::nDevice::Stop ( void   )  [virtual]

Deactivate the device.

This function disables output execution and input reading. It stops the backhround thread to provide connections and disconnects from any servers.

Reimplemented from faudes::vDevice.

Definition at line 634 of file iop_simplenet.cpp.

void faudes::nDevice::WriteOutput ( Idx  output  )  [virtual]

Run output command.

Exceptions:
Exception 
  • unknown output event (id 65)

Implements faudes::vDevice.

Definition at line 485 of file iop_simplenet.cpp.


Friends And Related Function Documentation

void* NDeviceListen ( void *   )  [friend]
void* NDeviceReply ( void *   )  [friend]
void* NDeviceServer ( void *   )  [friend]

Member Data Documentation

Simplenet: address for udp broadcast (255.255.255.255:40000.

Definition at line 640 of file iop_simplenet.h.

Background: udp broadcast socket (background only).

Definition at line 664 of file iop_simplenet.h.

Simplenet: effective address of my server port.

Definition at line 643 of file iop_simplenet.h.

std::map<std::string,ServerState> faudes::nDevice::mInputServerStates [protected]

Background: connection states to event servers (by node name).

Definition at line 687 of file iop_simplenet.h.

std::map<std::string,EventSet> faudes::nDevice::mInputSubscriptions [protected]

Compiled data: map subscriptions.

Definition at line 649 of file iop_simplenet.h.

Simplenet: address of my server incl port (localhost:40000).

Definition at line 637 of file iop_simplenet.h.

Background: server socket to listen (background only).

Definition at line 661 of file iop_simplenet.h.

faudes_mutex_t faudes::nDevice::mMutex [protected]

Background: mutex for below shared variables.

Definition at line 652 of file iop_simplenet.h.

std::string faudes::nDevice::mNetwork [protected]

Simplenet: network id.

Definition at line 634 of file iop_simplenet.h.

std::map<std::string,std::string> faudes::nDevice::mNetworkNodes [protected]

Simplenet: list of nodes in this network incl default addresses.

Definition at line 646 of file iop_simplenet.h.

Background: map sockets to connection states (shared).

Definition at line 675 of file iop_simplenet.h.

bool faudes::nDevice::mStopListen [protected]

Background: request to join via flag (mutexed).

Definition at line 658 of file iop_simplenet.h.

faudes_thread_t faudes::nDevice::mThreadListen [protected]

Background: thread handle (global).

Definition at line 655 of file iop_simplenet.h.

Overall configuration (with actual type).

Definition at line 631 of file iop_simplenet.h.


The documentation for this class was generated from the following files:

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen