1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020] [HCL Technologies Ltd.] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /* This file contains TcpServer class that listens for Netconf Alarm messages
20 on a TCP socket from ODU. It calls the AlarmManager functions for raising
21 or clearing the alarms based on the actions received
24 #include "TcpServer.hpp"
26 #include "AlarmManager.hpp"
27 #include "GlobalDefs.hpp"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
37 #include <arpa/inet.h>
43 TcpServer::~TcpServer()
49 /**********************************************************************
50 Description : Read the data from the connected client application
51 Params[In] : fd - File descriptor
52 Return : int - No of bytes read
53 **********************************************************************/
54 int TcpServer::readMessage(int fd)
57 bzero(&alrmRec,sizeof(alrmRec));
58 int nbytes = read (fd, &alrmRec, sizeof(alrmRec));
63 O1_LOG("\nO1 TcpServer :\nAction %d\nalarm ID %s\n%d\n%s\n%d\n%s\n%s\nbytes %d",
64 alrmRec.msgHeader.action,
66 alrmRec.perceivedSeverity,
67 alrmRec.additionalText,
69 alrmRec.specificProblem,
70 alrmRec.additionalInfo,
74 /*Fill the alarm structure */
75 sscanf(alrmRec.alarmId,"%hu",&alrmId);
76 alrm.setAlarmId(alrmId);
77 alrm.setPerceivedSeverity(alrmRec.perceivedSeverity);
78 alrm.setAdditionalText(alrmRec.additionalText);
79 alrm.setEventType(alrmRec.eventType);
80 alrm.setSpecificProblem(alrmRec.specificProblem);
81 alrm.setAdditionalInfo(alrmRec.additionalInfo);
83 switch(alrmRec.msgHeader.action)
86 if(AlarmManager::instance().raiseAlarm(alrm))
88 O1_LOG("\nO1 TcpServer : Alarm raised for alarm Id %s", alrmRec.alarmId);
92 O1_LOG("\nO1 TcpServer : Error in raising alarm for alrm Id %s", alrmRec.alarmId);
96 if(AlarmManager::instance().clearAlarm(alrm))
98 O1_LOG("\nO1 TcpServer : Alarm cleared for alarm Id %s", alrmRec.alarmId);
102 O1_LOG("\nO1 TcpServer : Error in clearing alarm for alarm Id %s", alrmRec.alarmId);
106 O1_LOG("\nO1 TcpServer : No action performed");
115 /**********************************************************************
116 Description : Open a TCP socket and bind on the port
118 Return : O1::SUCCESS - socket open and bind successful
119 O1::FAILURE - socket open and bind failed
120 **********************************************************************/
121 int TcpServer::makeSocket()
123 struct sockaddr_in name;
124 /* Create the socket. */
125 mSock = socket (PF_INET, SOCK_STREAM, 0);
128 O1_LOG("\nO1 TcpServer : Socket error");
131 /* Give the socket a name. */
132 bzero(&name, sizeof(name));
133 name.sin_family = AF_INET;
134 name.sin_port = htons (mPort);
135 name.sin_addr.s_addr = htonl (INADDR_ANY);
136 if (bind (mSock, (struct sockaddr *) &name, sizeof (name)) < 0)
139 O1_LOG("\nO1 TcpServer : Bind error");
146 /**********************************************************************
147 Description : Start TCP server in thread
149 Return : true - task launched in pthread successfully
150 false - task failed to launch
151 **********************************************************************/
152 bool TcpServer::start()
154 return (pthread_create(&mThreadId, NULL, task, this) == 0);
157 /**********************************************************************
158 Description : A TCP server to handle multiple connection using
161 Return : true - task launched in pthread successfully
162 false - task failed to launch
163 **********************************************************************/
164 bool TcpServer::run()
167 fd_set active_fd_set, read_fd_set;
169 struct sockaddr_in clientName;
173 /* Create the socket and set it up to accept connections. */
174 if( makeSocket() == O1::SUCCESS )
176 if (listen (mSock, 1) < 0)
178 O1_LOG("\nO1 TcpServer : Listen error");
184 /* Initialize the set of active sockets. */
185 FD_ZERO (&active_fd_set);
186 FD_SET (mSock, &active_fd_set);
190 /* Block until input arrives on one or more active sockets. */
191 read_fd_set = active_fd_set;
192 if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
194 O1_LOG("\nO1 TcpServer : Select error");
200 /* Service all the sockets with input pending. */
201 for (i = 0; i < FD_SETSIZE; ++i)
203 if (FD_ISSET (i, &read_fd_set))
207 /* Connection request on original socket. */
209 bzero(&clientName, sizeof(clientName));
210 size = sizeof (clientName);
211 newFd = accept(mSock,(struct sockaddr *) &clientName,&size);
214 O1_LOG("\nO1 TcpServer : Accept error");
219 O1_LOG("\nO1 TcpServer : Connected from host %s, port %hd.\n",
220 inet_ntoa (clientName.sin_addr),
221 ntohs (clientName.sin_port));
222 FD_SET (newFd, &active_fd_set);
226 /* Data arriving on an already-connected socket. */
227 if (readMessage(i) < 0)
230 FD_CLR (i, &active_fd_set);
235 } /* while(1) ends */
237 } /* outer if ends */
246 /**********************************************************************
247 Description : Static function for launching a TCP server instance
249 Params[In] : TcpServer instance
251 **********************************************************************/
252 void* TcpServer::task(void *args)
254 TcpServer *tcpServer = (TcpServer*)args;
260 /**********************************************************************
261 Description : Wait for the thread to complete in the parent process
263 Return : true - pthread join success
264 false - pthread join failed
265 **********************************************************************/
266 bool TcpServer::wait()
268 return (pthread_join(mThreadId,NULL) == 0);
273 /**********************************************************************
275 **********************************************************************/