CORDET Framework - C2 Implementation
CrDemoSlave2/CrDaServerSocket.c File Reference

Implementation of interface to server socket. More...

Go to the source code of this file.

Functions

static void * acceptThreadEntry (void *ptr)
 Entry point for the thread which waits for the incoming connection from the client socket. More...
 
static void serverSocketPoll (int nsockfd, unsigned char *buffer)
 Poll the socket for data from one of the two clients. More...
 
static CrFwBool_t serverSocketIsPcktAvail (CrFwDestSrc_t src, int nsockfd, unsigned char *buffer)
 Check whether a packet from the argument source is available. More...
 
static CrFwPckt_t serverSocketPcktCollect (CrFwDestSrc_t src, unsigned char *buffer)
 Collect a packet from the argument source. More...
 
void CrDaServerSocketInitAction (FwPrDesc_t prDesc)
 Initialization action for the server socket. More...
 
void CrDaServerSocketShutdownAction (FwSmDesc_t smDesc)
 Shutdown action 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 src)
 Function implementing the Packet Collect Operation for the server socket. More...
 
CrFwBool_t CrDaServerSocketIsPcktAvail (CrFwDestSrc_t src)
 Function implementing the Packet Available Check Operation for the server socket. More...
 
CrFwBool_t CrDaServerSocketPcktHandover (CrFwPckt_t pckt)
 Function implementing the hand-over operation 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 CrDaServerSocketSetPort (int n)
 Set the port number for the socket. More...
 

Variables

static int portno = 0
 Set the port number (must be same as the port number specified in CrDaServerSocket.c
 
static int sockfd = 0
 The file descriptors for the socket.
 
static int newsockfd [2]
 The file descriptors for the client sockets.
 
static struct sockaddr_in cli_addr
 Socket variable.
 
static socklen_t clilen
 Socket variable.
 
static int pcktMaxLength
 The maximum size of an incoming packet.
 
static unsigned char * readBuffer [2]
 The Read Buffers.
 

Detailed Description

Implementation of interface to server socket.

Author
Vaclav Cechticky vacla.nosp@m.v.ce.nosp@m.chtic.nosp@m.ky@p.nosp@m.np-so.nosp@m.ftwa.nosp@m.re.co.nosp@m.m
Alessandro Pasetti paset.nosp@m.ti@p.nosp@m.np-so.nosp@m.ftwa.nosp@m.re.co.nosp@m.m

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

Function Documentation

static void * acceptThreadEntry ( void *  ptr)
static

Entry point for the thread which waits for the incoming connection from the client socket.

Parameters
ptrunused argument (required with compatibility with pthread create function)

Definition at line 338 of file CrDemoSlave2/CrDaServerSocket.c.

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.

Parameters
prDescthe configuration procedure descriptor.

Definition at line 185 of file CrDemoSlave2/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.

Parameters
prDescthe initialization procedure descriptor.

Definition at line 391 of file CrDemoSlave2/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:

  • creates and binds the socket
  • start listening on the socket
  • spawns a thread which waits for an incoming connection from, first, the client socket of the Master Application and, then, the client socket of the Slave 2 Application
  • execute the Initialization Action of the base InStream/OutStream

The function sets the outcome to "success" if all these operations are successful.

Parameters
prDescthe initialization procedure descriptor.

Definition at line 106 of file CrDemoSlave2/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.

Parameters
prDescthe initialization procedure descriptor.

Definition at line 379 of file CrDemoSlave2/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:

  • The function begins by checking the first Read Buffer.
  • If it is full and the source attribute of the packet it contains is equal to pcktSrc, the function returns 1.
  • If the Read Buffer is not full or it is full but the source attribute of the packet it contains is not equal to pcktSrc, the function performs a non-blocking read on the socket.
  • If the read operation returns a packet with a source attribute equal to pcktSrc, the function stores it in the Read Buffer and then returns 1.
  • If the read operation returns nothing or returns a packet with a source attribute other than pcktSrc, the above logic is applied to the second Read Buffer.
  • If no packet for the argument destination is found from either Read Buffer, the function returns 0.
Parameters
pcktSrcthe source associated to the InStream
Returns
the value of a predefined flag

Definition at line 270 of file CrDemoSlave2/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:

  • creates a packet instance through a call to CrFwPcktMake
  • copies the content of the Read Buffer into the newly created packet instance
  • clears the Read Buffer
  • returns the packet instance

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.

Parameters
pcktSrcthe source associated to the InStream
Returns
the packet collected from the argument source

Definition at line 238 of file CrDemoSlave2/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.

Parameters
pcktthe packet to be written to the socket
Returns
1 if the packet was successfully written to the socket; 0 otherwise.

Definition at line 315 of file CrDemoSlave2/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 CrDemoSlave2/CrDaServerSocket.c.

void CrDaServerSocketSetPort ( int  n)

Set the port number for the socket.

The port number must be an integer greater than 2000.

Parameters
nthe port number.

Definition at line 406 of file CrDemoSlave2/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.

Parameters
smDescthe OutStream State Machine descriptor (this parameter is not used).

Definition at line 166 of file CrDemoSlave2/CrDaServerSocket.c.

static CrFwBool_t serverSocketIsPcktAvail ( CrFwDestSrc_t  src,
int  nsockfd,
unsigned char *  buffer 
)
static

Check whether a packet from the argument source is available.

Parameters
srcthe source
nsockfdthe socket which is to be polled
bufferthe Read Buffer associated to the client which is to be polled
Returns
1 if a packet is avaiable; 0 otherwise

Definition at line 281 of file CrDemoSlave2/CrDaServerSocket.c.

static CrFwPckt_t serverSocketPcktCollect ( CrFwDestSrc_t  src,
unsigned char *  buffer 
)
static

Collect a packet from the argument source.

Parameters
srcthe source
bufferthe Read Buffer associated to the client from which the packet is read
Returns
the packet collected from the argument source

Definition at line 252 of file CrDemoSlave2/CrDaServerSocket.c.

static void serverSocketPoll ( int  nsockfd,
unsigned char *  buffer 
)
static

Poll the socket for data from one of the two clients.

Parameters
nsockfdthe socket which is to be polled
bufferthe Read Buffer associated to the client which is to be polled

Definition at line 205 of file CrDemoSlave2/CrDaServerSocket.c.

P&P Software GmbH, Copyright 2012-2013, All Rights Reserved