CORDET Framework - C2 Implementation
|
Interface for the server socket used in the CORDET Demo. More...
Go to the source code of this file.
Functions | |
void | CrDaServerSocketInitAction (FwPrDesc_t prDesc) |
Initialization action for the server socket. More... | |
void | CrDaServerSocketInitCheck (FwPrDesc_t prDesc) |
Initialization check for the server socket. More... | |
void | CrDaServerSocketConfigCheck (FwPrDesc_t prDesc) |
Configuration check for the server socket. More... | |
void | CrDaServerSocketShutdownAction (FwSmDesc_t smDesc) |
Shutdown action for the server socket. More... | |
CrFwBool_t | CrDaServerSocketPcktHandover (CrFwPckt_t pckt) |
Function implementing the hand-over operation for the server socket. More... | |
void | CrDaServerSocketConfigAction (FwPrDesc_t prDesc) |
Configuration action for the server socket. More... | |
void | CrDaServerSocketPoll () |
Poll the server socket to check whether a new packet has arrived from either client. More... | |
CrFwPckt_t | CrDaServerSocketPcktCollect (CrFwDestSrc_t pcktSrc) |
Function implementing the Packet Collect Operation for the server socket. More... | |
CrFwBool_t | CrDaServerSocketIsPcktAvail (CrFwDestSrc_t pcktSrc) |
Function implementing the Packet Available Check Operation for the server socket. More... | |
void | CrDaServerSocketSetPort (int n) |
Set the port number for the socket. More... | |
Interface for the server socket used in the CORDET Demo.
The CORDET Demo consists of three applications which communicate with each other via sockets. The physical connections of the applications are shown in the figure below.
This module defines the functions through which the InStreams and OutStreams of the demo applications control a server socket in order to receive packets (InStream) or to send them (OutStream). These functions are used to customize the InStreams (see CrMaInStreamUserPar.h
for the Master Application and CrSlInStreamUserPar.h
for the Slave Application) and the OutStreams (see CrMaInStreamUserPar.h
for the Master Application and CrSlInStreamUserPar.h
for the Slave Application) which interact with the socket. More precisely:
CrDaServerSocketInitAction
should be used as the initialization action for the InStreams and OutStreams.CrDaServerSocketInitCheck
should be used as the initialization check action for the InStreams and OutStreams.CrDaServerSocketConfigAction
should be used as the configuration action for the InStreams and OutStreams.CrDaServerSocketShutdownAction
should be used as the shutdown action for the InStreams and OutStreams.CrDaServerSocketPcktCollect
should be used as the Packet Collect operation for the InStreams.CrDaServerSocketIsPcktAvail
should be used as the Packet Available Check operation for the InStreams.CrDaServerSocketPcktHandover
should be used as the Packet Hand-Over operation for the OutStreams.The functions in this module should be accessed in mutual exclusion. Compliance with this constraint is not enforced and is therefore under the responsibility of the caller.
The socket controlled by this module is built as a server socket using the Internet domain and the TCP protocol. It is designed to work with the client socket of CrDaClientSocket.h
. The socket expects two client connections.
The socket must be initialized with the port number for its socket (this is defined through functions CrDaClientSocketSetPort
.
In the initialization process of this module, a socket is bound and listening starts on it. A thread is then spawned which waits for incoming connections from its two client sockets. When the incoming connections have been accepted, the socket is ready to complete its configuration.
The socket assumes a polling approach for incoming packets: function CrDaServerSocketPoll
should be called periodically by an external scheduler. This function performs a non-blocking read on the socket to check whether a packet is available at the socket from either of its clients. If a packet is available, the function retrieves its source and forwards it to the associated InStream by calling function CrFwInStreamPcktAvail
on the InStream to signal the arrival of a new packet. This causes all pending packets from that source to be collected by the InStream and stored in its Packet Queue.
The implementation of this module assumes that, at each read operation, an entire packet is received. The situation where the packet is split into fragments during the transmission process is not handled. In practice, this socket is intended for the case where the socket is local to the host platform (i.e. where the host name is "localhost" and both sender and receiver of a packet are located on the same platform).
Packets which are read from the socket are stored in a buffer (the Read Buffer). This is an array of bytes whose size is equal to the maximum size of a middleware packet. The Read Buffer can be either "full" (if its first byte is different from zero) or "empty" (if its first byte has been cleared). Two Read Buffers are instantiated, one for each client.
The packet hand-over operation for OutStreams is implemented in function CrDaServerSocketPcktHandover
which performs a non-blocking write to the socket.
If an error is encountered while performing a system call, this module uses function perror
to print an error message and, if the error was encountered in the initialization or configuration action, it sets the outcome of the action to 0 ("failure") and returns.
This module assumes that the maximum length of a packet is smaller than 256 bytes. Compliance with this constraint is verified in the Initialization Check.
Mode of Use of a Server Socket Module
This interface may be controlled jointly by multiple InStreams and/or OutStreams. Its socket is initialized when the first of the these components is initialized (the initialization of the other InStreams/OutStreams has no effect). The socket is reset whenever one of the InStreams/OutStreams is reset. The socket is shut down whenever one of the InStreams/OutStreams is shut down (the shutdown of the other InStreams/OutStreams has no effect).
After creation, the user must define the port number for the socket. This is done through function CrDaServerSocketSetPort
. After this is done, the socket can be initialized and configured. During the initialization process, a thread is spawned which waits for a client socket to be connected to it. The server socket can only be successfully configured after this thread has accepted the connection from the client socket.
This file is part of the CORDET Framework.
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
For information on alternative licensing, please contact P&P Software GmbH.
Definition in file CrDemoSlave2/CrDaServerSocket.h.
void CrDaServerSocketConfigAction | ( | FwPrDesc_t | prDesc | ) |
Configuration action for the server socket.
This action clears the Read Buffer and executes the Configuration Action of the base InStream/OutStream.
prDesc | the configuration procedure descriptor. |
Definition at line 185 of file CrDemoMaster/CrDaServerSocket.c.
void CrDaServerSocketConfigCheck | ( | FwPrDesc_t | prDesc | ) |
Configuration check for the server socket.
The check is successful if the connection from the matching client socket has been successfully accepted.
prDesc | the initialization procedure descriptor. |
Definition at line 391 of file CrDemoMaster/CrDaServerSocket.c.
void CrDaServerSocketInitAction | ( | FwPrDesc_t | prDesc | ) |
Initialization action for the server socket.
If the server socket has already been initialized, this function calls the Initialization Action of the base InStream/OutStream and then returns. If the server socket has not yet been initialized, this action:
The function sets the outcome to "success" if all these operations are successful.
prDesc | the initialization procedure descriptor. |
Definition at line 106 of file CrDemoMaster/CrDaServerSocket.c.
void CrDaServerSocketInitCheck | ( | FwPrDesc_t | prDesc | ) |
Initialization check for the server socket.
The check is successful if the port number has been set to a value larger than 2000.
prDesc | the initialization procedure descriptor. |
Definition at line 379 of file CrDemoMaster/CrDaServerSocket.c.
CrFwBool_t CrDaServerSocketIsPcktAvail | ( | CrFwDestSrc_t | pcktSrc | ) |
Function implementing the Packet Available Check Operation for the server socket.
This function implements the following logic:
pcktSrc
, the function returns 1.pcktSrc
, the function performs a non-blocking read on the socket.pcktSrc
, the function stores it in the Read Buffer and then returns 1.pcktSrc
, the above logic is applied to the second Read Buffer.pcktSrc | the source associated to the InStream |
Definition at line 270 of file CrDemoMaster/CrDaServerSocket.c.
CrFwPckt_t CrDaServerSocketPcktCollect | ( | CrFwDestSrc_t | pcktSrc | ) |
Function implementing the Packet Collect Operation for the server socket.
If the packet in the first Read Buffer has a source attribute equal to pcktSrc
, this function:
CrFwPcktMake
If the packet in the first Read Buffer has a source attribute different from pcktSrc
, the same logic as above is applied to the second Read Buffer. If neither Read Buffer holds a packet from pcktSrc
, this function returns NULL. Note that the logic of the server socket module guarantees that at least one Read Buffer will always be full when this function is called.
pcktSrc | the source associated to the InStream |
Definition at line 238 of file CrDemoMaster/CrDaServerSocket.c.
CrFwBool_t CrDaServerSocketPcktHandover | ( | CrFwPckt_t | pckt | ) |
Function implementing the hand-over operation for the server socket.
This function performs a non-blocking write on the socket and, if it succeeds, it returns 1; otherwise, it returns 0. the client socket to which the write operation is made depends on the destination of the argument packet.
pckt | the packet to be written to the socket |
Definition at line 315 of file CrDemoMaster/CrDaServerSocket.c.
void CrDaServerSocketPoll | ( | ) |
Poll the server socket to check whether a new packet has arrived from either client.
This function should be called periodically by an external scheduler. If there is a pending packet (i.e. if a Read Buffer is full), its source is determined, and then function CrFwInStreamPcktAvail
is called on the InStream associated to that packet source. If there is no pending packet, a non-blocking read is performed from each client to check whether a packet is available from that client. If a packet is available, it is placed into the Read Buffer, its source is determined, and then function CrFwInStreamPcktAvail
is called on the InStream associated to that packet source.
Definition at line 199 of file CrDemoMaster/CrDaServerSocket.c.
void CrDaServerSocketSetPort | ( | int | n | ) |
Set the port number for the socket.
The port number must be an integer greater than 2000.
n | the port number. |
Definition at line 406 of file CrDemoMaster/CrDaServerSocket.c.
void CrDaServerSocketShutdownAction | ( | FwSmDesc_t | smDesc | ) |
Shutdown action for the server socket.
If the server socket has already been shut down, this function calls the Shutdown Action of the base InStream/OutStream and then returns. If the client socket has not yet been shut down, this action executes the Shutdown Action of the base OutStream/InStream and then closes the socket.
smDesc | the OutStream State Machine descriptor (this parameter is not used). |
Definition at line 166 of file CrDemoMaster/CrDaServerSocket.c.