CORDET Framework - C2 Implementation
CrDemoSlave1/CrDaServerSocket.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include "CrDaServerSocket.h"
23 #include "CrDaConstants.h"
24 /* Include FW Profile files */
25 #include "FwSmConstants.h"
26 #include "FwSmConfig.h"
27 #include "FwSmCore.h"
28 #include "FwPrConfig.h"
29 #include "FwPrCore.h"
30 #include "FwPrConstants.h"
31 /* Include configuration files */
32 #include "CrFwOutStreamUserPar.h"
33 #include "CrFwCmpData.h"
34 /* Include framework files */
36 #include "BaseCmp/CrFwBaseCmp.h"
37 #include "Pckt/CrFwPckt.h"
38 #include "CrFwTime.h"
39 #include "CrFwRepErr.h"
41 /* Include file for socket implementation */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netdb.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <pthread.h>
53 
55 static int portno = 0;
56 
58 static int sockfd = 0;
59 
61 static int newsockfd[2];
62 
64 static struct sockaddr_in cli_addr;
65 
67 static socklen_t clilen;
68 
70 static int pcktMaxLength;
71 
73 static unsigned char* readBuffer[2];
74 
79 static void* acceptThreadEntry(void* ptr);
80 
86 static void serverSocketPoll(int nsockfd, unsigned char* buffer);
87 
95 static CrFwBool_t serverSocketIsPcktAvail(CrFwDestSrc_t src, int nsockfd, unsigned char* buffer);
96 
103 static CrFwPckt_t serverSocketPcktCollect(CrFwDestSrc_t src, unsigned char* buffer);
104 
105 /* ---------------------------------------------------------------------------------------------*/
106 void CrDaServerSocketInitAction(FwPrDesc_t prDesc) {
107  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
108  struct sockaddr_in serv_addr, cli_addr;
109  pthread_t acceptThread;
110  pthread_attr_t attr;
111 
112  /* Check if server socket has already been initialized */
113  if (sockfd != 0) {
114  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
116  else
118  return;
119  }
120 
121  /* Create the read buffer */
123  readBuffer[0] = malloc(pcktMaxLength*sizeof(char));
124  readBuffer[1] = malloc(pcktMaxLength*sizeof(char));
125 
126  /* Create the socket */
127  sockfd = socket(AF_INET, SOCK_STREAM, 0);
128  if (sockfd < 0) {
129  perror("CrDaServerSocketInitAction, Socket creation");
130  streamData->outcome = 0;
131  return;
132  }
133 
134  bzero((char*) &serv_addr, sizeof(serv_addr));
135  serv_addr.sin_family = AF_INET;
136  serv_addr.sin_addr.s_addr = INADDR_ANY;
137  serv_addr.sin_port = htons(portno);
138  if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
139  perror("CrDaServerSocketInitAction, Bind Socket");
140  streamData->outcome = 0;
141  return;
142  }
143  listen(sockfd,5);
144  clilen = sizeof(cli_addr);
145 
146  /* Create thread which will accept the connection call from the client sockets */
147  pthread_attr_init(&attr);
148  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
149  perror("CrDaServerSocketInitAction, set detached state");
150  streamData->outcome = 0;
151  return;
152  }
153  if (pthread_create(&acceptThread, NULL, acceptThreadEntry, NULL) < 0) {
154  streamData->outcome = 0;
155  return;
156  };
157 
158  /* Execute default initialization action for OutStream/InStream */
159  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
161  else
163 }
164 
165 /* ---------------------------------------------------------------------------------------------*/
166 void CrDaServerSocketShutdownAction(FwSmDesc_t smDesc) {
167  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwSmGetData(smDesc);
168 
169  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
171  else
173 
174  if (sockfd != 0) {
175  free(readBuffer[0]);
176  free(readBuffer[1]);
177  close(newsockfd[0]);
178  close(newsockfd[1]);
179  close(sockfd);
180  sockfd = 0;
181  }
182 }
183 
184 /* ---------------------------------------------------------------------------------------------*/
185 void CrDaServerSocketConfigAction(FwPrDesc_t prDesc) {
186  CrFwCmpData_t* streamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
187 
188  /* Clear Read Buffers */
189  readBuffer[0][0] = 0;
190  readBuffer[1][0] = 0;
191 
192  if (streamData->typeId == CR_FW_INSTREAM_TYPE)
194  else
196 }
197 
198 /* ---------------------------------------------------------------------------------------------*/
202 }
203 
204 /* ---------------------------------------------------------------------------------------------*/
205 static void serverSocketPoll(int nsockfd, unsigned char* buffer) {
206  int n;
207  FwSmDesc_t inStream;
208  CrFwDestSrc_t src;
209 
210  if (buffer[0] != 0) {
211  src = CrFwPcktGetSrc((CrFwPckt_t)buffer);
212  inStream = CrFwInStreamGet(src);
213  CrFwInStreamPcktAvail(inStream);
214  return;
215  }
216 
217  n = read(nsockfd, buffer, pcktMaxLength);
218  if (n == -1) /* no data are available from the socket */
219  return;
220  if (n == 0) {
221  printf("CrDaServerSocketPoll: Error reading from socket\n");
222  return;
223  }
224  if (n == buffer[0]) { /* a valid packet has arrived */
225  src = CrFwPcktGetSrc((CrFwPckt_t)buffer);
226  inStream = CrFwInStreamGet(src);
227  CrFwInStreamPcktAvail(inStream);
228  return;
229  }
230  if (n != buffer[0]) {
231  printf("CrDaServerSocketPoll: invalid packet received from socket\n");
232  buffer[0] = 0;
233  return;
234  }
235 }
236 
237 /* ---------------------------------------------------------------------------------------------*/
239  CrFwPckt_t pckt;
240 
241  pckt = serverSocketPcktCollect(src, readBuffer[0]);
242  if (pckt != NULL)
243  return pckt;
244  pckt = serverSocketPcktCollect(src, readBuffer[1]);
245  if (pckt != NULL)
246  return pckt;
247 
248  return NULL;
249 }
250 
251 /* ---------------------------------------------------------------------------------------------*/
252 static CrFwPckt_t serverSocketPcktCollect(CrFwDestSrc_t src, unsigned char* buffer) {
253  CrFwPckt_t pckt;
254  CrFwDestSrc_t pcktSrc;
255 
256  if (buffer[0] != 0) {
257  pcktSrc = CrFwPcktGetSrc((CrFwPckt_t)buffer);
258  if (src == pcktSrc) {
259  pckt = CrFwPcktMake((CrFwPcktLength_t)buffer[0]);
260  memcpy(pckt, buffer, buffer[0]);
261  buffer[0] = 0;
262  return pckt;
263  } else
264  return NULL;
265  } else
266  return NULL;
267 }
268 
269 /* ---------------------------------------------------------------------------------------------*/
272  return 1;
273 
275  return 1;
276 
277  return 0;
278 }
279 
280 /* ---------------------------------------------------------------------------------------------*/
281 static CrFwBool_t serverSocketIsPcktAvail(CrFwDestSrc_t src, int nsockfd, unsigned char* buffer) {
282  int n;
283  CrFwDestSrc_t pcktSrc;
284 
285  if (buffer[0] != 0) {
286  return 1;
287  }
288 
289  n = read(nsockfd, buffer, pcktMaxLength);
290  if (n == -1) /* no data are available from the socket (EAGAIN) */
291  return 0;
292  if (n == 0) {
293  printf("CrDaServerSocketPoll: Error reading from socket\n");
294  return 0;
295  }
296 
297  if (n == buffer[0]) { /* a valid packet has arrived */
298  pcktSrc = CrFwPcktGetSrc((CrFwPckt_t)buffer);
299  if (src == pcktSrc)
300  return 1;
301  else
302  return 0;
303  }
304 
305  if (n != buffer[0]) {
306  printf("CrDaServerSocketIsPcktAvail: invalid packet received from socket");
307  buffer[0] = 0;
308  return 0;
309  }
310 
311  return 0;
312 }
313 
314 /* ---------------------------------------------------------------------------------------------*/
316  int len = (int)CrFwPcktGetLength(pckt);
317  int n;
319 
320  dest = CrFwPcktGetDest(pckt);
321  if (dest == CR_DA_MASTER)
322  n = write(newsockfd[0], pckt, len);
323  else
324  n = write(newsockfd[1], pckt, len);
325 
326  if (n < 0)
327  return 0;
328 
329  if (n != (int)CrFwPcktGetLength(pckt)) {
330  printf("CrDaServerSocketPcktHandover: error writing to socket\n");
331  return 0;
332  }
333 
334  return 1;
335 }
336 
337 /* ---------------------------------------------------------------------------------------------*/
338 static void* acceptThreadEntry(void* ptr) {
339  int flags;
340 
341  printf("S1: Waiting for client socket in Master Application to connect ...\n");
342  newsockfd[0] = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
343  if (newsockfd < 0) {
344  perror("CrDaServerSocketInitAction, Socket Accept");
345  }
346 
347  /* Set the socket to non-blocking mode */
348  if ((flags = fcntl(newsockfd[0], F_GETFL, 0)) < 0) {
349  perror("CrDaServerSocketInitAction, Set socket attributes");
350  return NULL;
351  }
352  if (fcntl(newsockfd[0], F_SETFL, flags | O_NONBLOCK) < 0) {
353  perror("CrDaServerSocketInitAction, Set socket attributes");
354  return NULL;
355  }
356  printf("S1: Client socket in Master Application successfully connected.\n");
357 
358  printf("S1: Waiting for client socket in Slave 2 Application to connect ...\n");
359  newsockfd[1] = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
360  if (newsockfd < 0) {
361  perror("CrDaServerSocketInitAction, Socket Accept");
362  }
363 
364  /* Set the socket to non-blocking mode */
365  if ((flags = fcntl(newsockfd[1], F_GETFL, 0)) < 0) {
366  perror("CrDaServerSocketInitAction, Set socket attributes");
367  return NULL;
368  }
369  if (fcntl(newsockfd[1], F_SETFL, flags | O_NONBLOCK) < 0) {
370  perror("CrDaServerSocketInitAction, Set socket attributes");
371  return NULL;
372  }
373  printf("S1: Client socket in Slave 2 Application successfully connected.\n");
374 
375  return NULL;
376 }
377 
378 /* ---------------------------------------------------------------------------------------------*/
379 void CrDaServerSocketInitCheck(FwPrDesc_t prDesc) {
380  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
381 
382  if (portno < 2000)
383  outStreamData->outcome = 0;
384  else
385  outStreamData->outcome = 1;
386 
387  return;
388 }
389 
390 /* ---------------------------------------------------------------------------------------------*/
391 void CrDaServerSocketConfigCheck(FwPrDesc_t prDesc) {
392  CrFwCmpData_t* outStreamData = (CrFwCmpData_t*)FwPrGetData(prDesc);
393 
394  if (newsockfd[0] > 0) {
395  if (newsockfd[1] > 0)
396  outStreamData->outcome = 1;
397  else
398  outStreamData->outcome = 0;
399  } else
400  outStreamData->outcome = 0;
401 
402  return;
403 }
404 
405 /* ---------------------------------------------------------------------------------------------*/
407  portno = n;
408 }
void CrFwOutStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an OutStream.
#define CR_DA_MASTER
The identifier of the Master Application of the CORDET Demo.
void CrDaServerSocketInitCheck(FwPrDesc_t prDesc)
Initialization check for the server socket.
Type for the Framework Component Data (FCD).
Interface through which framework components access the current time.
CrFwOutcome_t outcome
The outcome of an action or check executed by a state machine or by one of its procedures.
CrFwDestSrc_t CrFwPcktGetSrc(CrFwPckt_t pckt)
Return the source of the command or report encapsulated in a packet.
static void * acceptThreadEntry(void *ptr)
Entry point for the thread which waits for the incoming connection from the client socket...
unsigned char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:38
static struct sockaddr_in cli_addr
Socket variable.
Definition of the OutStream component.
unsigned char CrFwDestSrc_t
Type used for the command or report destination and source.
static int newsockfd[2]
The file descriptors for the client sockets.
CrFwPckt_t CrFwPcktMake(CrFwPcktLength_t pcktLength)
Make function for command or report packets.
#define CR_FW_INSTREAM_TYPE
Type identifier for the InStream components.
Interface for creating and accessing a report or command packet.
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:32
static CrFwBool_t serverSocketIsPcktAvail(CrFwDestSrc_t src, int nsockfd, unsigned char *buffer)
Check whether a packet from the argument source is available.
CrFwDestSrc_t CrFwPcktGetDest(CrFwPckt_t pckt)
Return the destination of the command or report encapsulated in a packet.
Interface for reporting an error detected by a framework component.
void CrFwInStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an InStream.
Definition: CrFwInStream.c:312
void CrDaServerSocketInitAction(FwPrDesc_t prDesc)
Initialization action for the server socket.
CrFwPcktLength_t CrFwPcktGetLength(CrFwPckt_t pckt)
Return the length (in number of bytes) of a packet.
CrFwPckt_t CrDaServerSocketPcktCollect(CrFwDestSrc_t src)
Function implementing the Packet Collect Operation for the server socket.
CrFwBool_t CrDaServerSocketIsPcktAvail(CrFwDestSrc_t src)
Function implementing the Packet Available Check Operation for the server socket. ...
unsigned short int CrFwPcktLength_t
Type for the packet length.
void CrDaServerSocketPoll()
Poll the server socket to check whether a new packet has arrived from either client.
CrFwPcktLength_t CrFwPcktGetMaxLength()
Return the maximum length of a packet in number of bytes.
void CrDaServerSocketConfigAction(FwPrDesc_t prDesc)
Configuration action for the server socket.
Definition of the utility functions for the CORDET Framework.
void CrFwOutStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an OutStream.
void CrFwOutStreamDefInitAction(FwPrDesc_t prDesc)
Default initialization action for an OutStream.
void CrDaServerSocketSetPort(int n)
Set the port number for the socket.
static int portno
Set the port number (must be same as the port number specified in CrDaServerSocket.c
CrFwBool_t CrDaServerSocketPcktHandover(CrFwPckt_t pckt)
Function implementing the hand-over operation for the server socket.
void CrFwInStreamDefShutdownAction(FwSmDesc_t smDesc)
Default shutdown action for an InStream.
Definition: CrFwInStream.c:301
FwSmDesc_t CrFwInStreamGet(CrFwDestSrc_t src)
Getter function for the InStream corresponding to the argument source.
Definition: CrFwInStream.c:211
Definition of Base Component.
static CrFwDestSrc_t dest
Destination.
static CrFwCmpData_t outStreamData[CR_FW_NOF_OUTSTREAM]
The base data structures for the OutStream State Machines and their Procedures.
Definition: CrFwOutStream.c:80
void CrDaServerSocketConfigCheck(FwPrDesc_t prDesc)
Configuration check for the server socket.
Interface for the server socket used in the CORDET Demo.
Header file to define constants and types for the CORDET Demo.
CrFwTypeId_t typeId
The type identifier of the framework component.
void CrFwInStreamDefConfigAction(FwPrDesc_t prDesc)
Default configuration action for an InStream.
Definition: CrFwInStream.c:288
void CrFwInStreamPcktAvail(FwSmDesc_t smDesc)
Query the middleware for available packets and collect them if they are available.
Definition: CrFwInStream.c:241
void CrDaServerSocketShutdownAction(FwSmDesc_t smDesc)
Shutdown action for the server socket.
static unsigned char * readBuffer[2]
The Read Buffers.
static void serverSocketPoll(int nsockfd, unsigned char *buffer)
Poll the socket for data from one of the two clients.
static int pcktMaxLength
The maximum size of an incoming packet.
static int sockfd
The file descriptors for the socket.
static CrFwPckt_t serverSocketPcktCollect(CrFwDestSrc_t src, unsigned char *buffer)
Collect a packet from the argument source.
static socklen_t clilen
Socket variable.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved