1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [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 UnixSocketServer class that listens for Netconf Alarm
20 messages on a Unix socket from ODU. It calls the AlarmManager functions
21 for raising or clearing the alarms based on the actions received */
23 #include "UnixSocketServer.hpp"
25 #include "CmInterface.h"
26 #include "GlobalDefs.hpp"
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include "InitConfig.hpp"
44 /*******************************************************************
50 * Function : UnixSocketServer
53 * - Constructor intialization
55 * @params[in] socket path
57 ******************************************************************/
58 UnixSocketServer::UnixSocketServer(const string& sockPath)
59 : mSockPath(sockPath),
65 /*******************************************************************
71 * Function : ~UnixSocketServer
78 ******************************************************************/
79 UnixSocketServer::~UnixSocketServer()
85 /*******************************************************************
87 * @brief Read the data from the connected client application
91 * Function : readMessage
94 * - Reads the data from the connected client application
96 * @params[in] File descriptor
97 * @return No. of bytes read
99 ******************************************************************/
100 int UnixSocketServer::readMessage(int fd)
102 char recvBuf[BUFLEN];
103 bzero(&recvBuf,sizeof(recvBuf));
105 int nbytes = read (fd, &recvBuf, sizeof(recvBuf));
109 /* sending message to all Unix Socket Server subscribers */
110 createMessage(recvBuf);
118 /*******************************************************************
120 * @brief Open a Unix socket and bind on the port
124 * Function : makeSocket
127 * - Opens a Unix socket and bind on the port
130 * @return O1:SUCCESS - success
131 * O1:FAILURE - failure
132 ******************************************************************/
134 int UnixSocketServer::makeSocket()
136 struct sockaddr_un name;
137 /* Create the socket. */
138 mSock = socket (AF_UNIX, SOCK_STREAM, 0);
141 O1_LOG("\nO1 UnixSocketServer : Socket error");
144 /* Give the socket a name. */
145 bzero(&name, sizeof(name));
146 name.sun_family = AF_UNIX;
148 /* Remove the socket file if it already exists */
149 if ( unlink(mSockPath.c_str()) == 0)
151 O1_LOG("\nO1 UnixSocketServer : "
152 "Removing the existing socket path %s",
155 strcpy(name.sun_path, mSockPath.c_str());
156 if (bind (mSock, (struct sockaddr *) &name, sizeof (name)) < 0)
159 O1_LOG("\nO1 UnixSocketServer : Bind error");
166 /*******************************************************************
168 * @brief A Unix server to handle multiple connection
175 * - A Unix server to handle multiple connection
176 * Uses select multiplexing
179 * @return true - success
181 ******************************************************************/
182 bool UnixSocketServer::run()
185 fd_set active_fd_set, read_fd_set;
187 struct sockaddr_un clientName;
191 /* Create the socket and set it up to accept connections. */
192 if( makeSocket() == O1::SUCCESS )
194 if (listen (mSock, 1) < 0)
196 O1_LOG("\nO1 UnixSocketServer : Listen error");
202 /* Initialize the set of active sockets. */
203 FD_ZERO (&active_fd_set);
204 FD_SET (mSock, &active_fd_set);
208 /* Block until input arrives on one or more active sockets. */
209 read_fd_set = active_fd_set;
210 if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
212 O1_LOG("\nO1 UnixSocketServer : Select error");
218 /* Service all the sockets with input pending. */
219 for (i = 0; i < FD_SETSIZE; ++i)
221 if (FD_ISSET (i, &read_fd_set))
225 /* Connection request on original socket. */
227 bzero(&clientName, sizeof(clientName));
228 size = sizeof (clientName);
229 newFd = accept(mSock,(struct sockaddr *) &clientName,&size);
232 O1_LOG("\nO1 UnixSocketServer : Accept error");
237 O1_LOG("\nO1 UnixSocketServer : Connected from client\n");
238 FD_SET (newFd, &active_fd_set);
242 /* Data arriving on an already-connected socket. */
243 if (readMessage(i) < 0)
246 FD_CLR (i, &active_fd_set);
251 } /* while(1) ends */
253 } /* outer if ends */
262 /*******************************************************************
264 * @brief Clean up open socket
271 * - Performs any clean ups before stopping the thread
275 ******************************************************************/
276 void UnixSocketServer::cleanUp(void)
279 O1_LOG("\nO1 UnixSocketServer : Cleaning up Closing socket \n");
282 /*******************************************************************
284 * @brief Check if the server is running
288 * Function : isRunning
291 * - Returns the running status of the server
294 * @return true : running
296 ******************************************************************/
297 bool UnixSocketServer::isRunning() const
302 /**********************************************************************
304 **********************************************************************/