faudes::nDevice Class Reference
|
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, EventSet > | mInputSubscriptions |
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, ClientState > | mOutputClientStates |
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 *) |
An nDevice implements networked IO via a simple TCP/IP protocol.
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:
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.
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.
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> |
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>
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.
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.
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.
rAddr | Address for UDP broadcasts e.g. "255.255.255.255:40000" |
Exception |
|
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.
rTr | TokenReader to read from | |
rLabel | Section to read | |
pContext | Read context to provide contextual information |
Exception |
|
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.
rTw | Reference to TokenWriter | |
rLabel | Label of section to write | |
pContext | Read context to provide contextual information |
Exception |
|
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.
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.
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.
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.
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.
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.
rAddr | Address on which to run this server, e.g. "localhost:40000" |
Exception |
|
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.
Exception |
|
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.
Exception |
|
Implements faudes::vDevice.
Definition at line 485 of file iop_simplenet.cpp.
void* NDeviceListen | ( | void * | ) | [friend] |
void* NDeviceReply | ( | void * | ) | [friend] |
void* NDeviceServer | ( | void * | ) | [friend] |
SimplenetAddress faudes::nDevice::mBroadcastAddress [protected] |
Simplenet: address for udp broadcast (255.255.255.255:40000.
Definition at line 640 of file iop_simplenet.h.
int faudes::nDevice::mBroadcastSocket [protected] |
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.
SimplenetAddress faudes::nDevice::mListenAddress [protected] |
Simplenet: address of my server incl port (localhost:40000).
Definition at line 637 of file iop_simplenet.h.
int faudes::nDevice::mListenSocket [protected] |
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.
std::map<int, ClientState> faudes::nDevice::mOutputClientStates [protected] |
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.
TaNameSet<AttributeSimplenetEvent>* faudes::nDevice::pConfiguration [protected] |
Overall configuration (with actual type).
Definition at line 631 of file iop_simplenet.h.
libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen