#include <iop_simplenet.h>

Classes

struct  ClientState
 
struct  ServerState
 

Public Member Functions

 nDevice (void)
 
virtual ~nDevice (void)
 
virtual void Clear (void)
 
void ServerAddress (const std::string &rAddr)
 
void BroadcastAddress (const std::string &rAddr)
 
void NetworkName (const std::string &rNetwork)
 
void InsNode (const std::string &rNodeName)
 
void InsNodeAddress (const std::string &rNode, const std::string &rAddress)
 
void ClearNodes (void)
 
void InsInputEvent (const std::string &event)
 
void InsOutputEvent (const std::string &event)
 
virtual void Compile (void)
 
virtual void Start (void)
 
virtual void Stop (void)
 
virtual void Reset (void)
 
virtual void WriteOutput (Idx output)
 
- Public Member Functions inherited from faudes::vDevice
 vDevice (void)
 
virtual ~vDevice (void)
 
void Name (const std::string &rName)
 
const std::string & Name (void) const
 
void Tolerance (Time::Type maxgap)
 
Time::Type Tolerance (void)
 
virtual void TimeScale (unsigned int scale)
 
virtual int TimeScale (void)
 
virtual void Configure (Idx event, const AttributeDeviceEvent &attr)
 
virtual void Configure (const EventSet &rPhysicalEvents)
 
virtual const EventSetOutputs (void) const
 
virtual const EventSetInputs (void) const
 
virtual bool ResetRequest (void)
 
virtual DeviceState Status (void)
 
virtual std::string StatusString (void)
 
virtual void FlushOutputs (void)
 
virtual Idx ReadInput (void)
 
virtual Idx PeekInput (void)
 
virtual bool InputReady (void)
 
virtual bool WaitInputs (Time::Type duration)
 
virtual bool WaitInputsMs (long int duration)
 
virtual Time::Type CurrentTime (void)
 
virtual long int CurrentTimeMs (void)
 
virtual void CurrentTime (Time::Type now)
 
virtual void CurrentTimeMs (long int nowms)
 
virtual faudes_systime_t FtuToSystemTime (Time::Type duration)
 
void UseCondition (faudes_mutex_t *wmutex, faudes_cond_t *wcond)
 
void UseBuffer (faudes_mutex_t *bmutex, std::deque< Idx > *bbuffer)
 
virtual std::string EStr (Idx ev)
 
SampledDensityFunction Performance (void)
 
void ResetPerformance (void)
 
void WritePerformance (void)
 
- Public Member Functions inherited from faudes::Type
 Type (void)
 
 Type (const Type &rType)
 
virtual ~Type (void)
 
virtual TypeNew (void) const
 
virtual TypeCopy (void) const
 
virtual const TypeCast (const Type *pOther) const
 
virtual TypeAssign (const Type &rSrc)
 
Typeoperator= (const Type &rSrc)
 
virtual bool Equal (const Type &rOther) const
 
bool operator== (const Type &rOther) const
 
bool operator!= (const Type &rOther) const
 
virtual const std::string & TypeName (void) const
 
void Write (const Type *pContext=0) const
 
void Write (const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0, std::ios::openmode openmode=std::ios::out|std::ios::trunc) const
 
void Write (const std::string &pFileName, std::ios::openmode openmode) const
 
void Write (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
virtual void XWrite (const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0) const
 
void XWrite (const Type *pContext=0) const
 
void XWrite (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
std::string ToString (const std::string &rLabel="", const Type *pContext=0) const
 
std::string ToText (const std::string &rLabel="", const Type *pContext=0) const
 
void DWrite (const Type *pContext=0) const
 
void DWrite (const std::string &pFileName, const std::string &rLabel="", const Type *pContext=0, std::ios::openmode openmode=std::ios::out|std::ios::trunc) const
 
void DWrite (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
void SWrite (TokenWriter &rTw) const
 
void SWrite (void) const
 
std::string ToSText (void) const
 
void Read (const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
 
void FromString (const std::string &rString, const std::string &rLabel="", const Type *pContext=0)
 
void Read (TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
 

Protected Member Functions

virtual void DoReadPreface (TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
 
virtual void DoWritePreface (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
- Protected Member Functions inherited from faudes::vDevice
virtual void DoWrite (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
virtual void DoWriteConfiguration (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
virtual void DoRead (TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
 
virtual void DoReadConfiguration (TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
 
virtual long int FtuToMs (Time::Type faudes_time)
 
virtual Time::Type MsToFtu (long int real_time)
 
- Protected Member Functions inherited from faudes::Type
void DoAssign (const Type &rSrc)
 
bool DoEqual (const Type &rOther) const
 
virtual void DoXWrite (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
virtual void DoDWrite (TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
 
virtual void DoSWrite (TokenWriter &rTw) const
 
virtual const TypeDefinitionTypeDefinitionp (void) const
 
virtual Token XBeginTag (const std::string &rLabel="", const std::string &rFallbackLabel="") const
 

Protected Attributes

TaNameSet< AttributeSimplenetEvent > * pConfiguration
 
std::string mNetwork
 
SimplenetAddress mListenAddress
 
SimplenetAddress mBroadcastAddress
 
SimplenetAddress mEffectiveListenAddress
 
std::map< std::string, std::string > mNetworkNodes
 
std::map< std::string, EventSetmInputSubscriptions
 
faudes_mutex_t mMutex
 
faudes_thread_t mThreadListen
 
bool mStopListen
 
int mListenSocket
 
int mBroadcastSocket
 
std::map< int, ClientStatemOutputClientStates
 
std::map< std::string, ServerStatemInputServerStates
 
- Protected Attributes inherited from faudes::vDevice
std::string mDefaultLabel
 
std::string mName
 
EventSetmpConfiguration
 
EventSet mInputs
 
EventSet mOutputs
 
DeviceState mState
 
faudes_mutex_t mWaitMutex
 
faudes_cond_t mWaitCondition
 
faudes_mutex_t * pWaitMutex
 
faudes_cond_t * pWaitCondition
 
faudes_systime_t mTimeZero
 
int mTimeScale
 
Time::Type mMaxSyncGap
 
std::deque< IdxmInputBuffer
 
std::deque< Idx > * pInputBuffer
 
faudes_mutex_t mBufferMutex
 
faudes_mutex_t * pBufferMutex
 
bool mResetRequest
 
faudes_systime_t * mpPerformanceWaitEnter
 
faudes_systime_t * mpPerformanceWaitExit
 
int mPerformanceEndIterator
 
int mPerformanceBeginIterator
 

Friends

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

Additional Inherited Members

- Public Types inherited from faudes::vDevice
enum  DeviceState { Down , StartUp , Up , ShutDown }
 
- Static Public Member Functions inherited from faudes::vDevice
static vDeviceFromTokenReader (TokenReader &rTr)
 
static vDeviceFromFile (const std::string &rFileName)
 
static void StopAll (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 </Subscribed>)

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>
void BroadcastAddress(const std::string &rAddr)
void ServerAddress(const std::string &rAddr)
virtual int TimeScale(void)
Definition: iop_vdevice.h:374
TNode< Idx, Idx > Node

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 431 of file iop_simplenet.h.

Constructor & Destructor Documentation

◆ nDevice()

faudes::nDevice::nDevice ( void  )

Default constructor

Definition at line 265 of file iop_simplenet.cpp.

◆ ~nDevice()

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

Explicit destructor.

Definition at line 283 of file iop_simplenet.cpp.

Member Function Documentation

◆ BroadcastAddress()

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
rAddrAddress 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.

◆ Clear()

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.

◆ ClearNodes()

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.

◆ Compile()

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

Set up internal data structures

Reimplemented from faudes::vDevice.

Definition at line 358 of file iop_simplenet.cpp.

◆ DoReadPreface()

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

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
rTrTokenReader to read from
rLabelSection to read
pContextRead context to provide contextual information
Exceptions
Exception
  • IO error (id 1)

Reimplemented from faudes::vDevice.

Definition at line 399 of file iop_simplenet.cpp.

◆ DoWritePreface()

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

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
rTwReference to TokenWriter
rLabelLabel of section to write
pContextRead context to provide contextual information
Exceptions
Exception
  • IO errors (id 2)

Reimplemented from faudes::vDevice.

Definition at line 367 of file iop_simplenet.cpp.

◆ InsInputEvent()

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
eventEvent by name.

Definition at line 339 of file iop_simplenet.cpp.

◆ InsNode()

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
rNodeNameNode to participate in wthe network.

Definition at line 321 of file iop_simplenet.cpp.

◆ InsNodeAddress()

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
rNodeName of node to register
rAddressAddress incl port

Definition at line 327 of file iop_simplenet.cpp.

◆ InsOutputEvent()

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
eventEvent by name.

Definition at line 348 of file iop_simplenet.cpp.

◆ NetworkName()

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
rNetworkName of network, e.g. "ElevatorNetwork"

Definition at line 315 of file iop_simplenet.cpp.

◆ Reset()

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

Reset device. Resets buffered input events and current time.

Reimplemented from faudes::vDevice.

Definition at line 1280 of file iop_simplenet.cpp.

◆ ServerAddress()

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
rAddrAddress 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.

◆ Start()

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 541 of file iop_simplenet.cpp.

◆ Stop()

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 639 of file iop_simplenet.cpp.

◆ WriteOutput()

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

Run output command.

Exceptions
Exception
  • unknown output event (id 65)

Implements faudes::vDevice.

Definition at line 490 of file iop_simplenet.cpp.

Friends And Related Function Documentation

◆ NDeviceListen

void* NDeviceListen ( void *  )
friend

Definition at line 682 of file iop_simplenet.cpp.

◆ NDeviceReply

void* NDeviceReply ( void *  )
friend

◆ NDeviceServer

void* NDeviceServer ( void *  )
friend

Member Data Documentation

◆ mBroadcastAddress

SimplenetAddress faudes::nDevice::mBroadcastAddress
protected

Simplenet: address for udp broadcast (255.255.255.255:40000

Definition at line 636 of file iop_simplenet.h.

◆ mBroadcastSocket

int faudes::nDevice::mBroadcastSocket
protected

Background: udp broadcast socket (background only)

Definition at line 660 of file iop_simplenet.h.

◆ mEffectiveListenAddress

SimplenetAddress faudes::nDevice::mEffectiveListenAddress
protected

Simplenet: effective address of my server port

Definition at line 639 of file iop_simplenet.h.

◆ mInputServerStates

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

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

Definition at line 683 of file iop_simplenet.h.

◆ mInputSubscriptions

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

Compiled data: map subscriptions

Definition at line 645 of file iop_simplenet.h.

◆ mListenAddress

SimplenetAddress faudes::nDevice::mListenAddress
protected

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

Definition at line 633 of file iop_simplenet.h.

◆ mListenSocket

int faudes::nDevice::mListenSocket
protected

Background: server socket to listen (background only)

Definition at line 657 of file iop_simplenet.h.

◆ mMutex

faudes_mutex_t faudes::nDevice::mMutex
protected

Background: mutex for below shared variables

Definition at line 648 of file iop_simplenet.h.

◆ mNetwork

std::string faudes::nDevice::mNetwork
protected

Simplenet: network id

Definition at line 630 of file iop_simplenet.h.

◆ mNetworkNodes

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

Simplenet: list of nodes in this network incl default addresses

Definition at line 642 of file iop_simplenet.h.

◆ mOutputClientStates

std::map<int, ClientState> faudes::nDevice::mOutputClientStates
protected

Background: map sockets to connection states (shared)

Definition at line 671 of file iop_simplenet.h.

◆ mStopListen

bool faudes::nDevice::mStopListen
protected

Background: request to join via flag (mutexed)

Definition at line 654 of file iop_simplenet.h.

◆ mThreadListen

faudes_thread_t faudes::nDevice::mThreadListen
protected

Background: thread handle (global)

Definition at line 651 of file iop_simplenet.h.

◆ pConfiguration

TaNameSet<AttributeSimplenetEvent>* faudes::nDevice::pConfiguration
protected

Overall configuration (with actual type)

Definition at line 627 of file iop_simplenet.h.


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

libFAUDES 2.33b --- 2025.05.07 --- c++ api documentaion by doxygen