26acfe70cb40e8d26d825bb5920e5bcb25c2a974
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.server;
22
23 import java.net.InetSocketAddress;
24
25 import org.java_websocket.WebSocket;
26 import org.java_websocket.handshake.ClientHandshake;
27 import org.java_websocket.server.WebSocketServer;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * This class is the web socket server specific implementation for Apex.
33  *
34  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
35  */
36 abstract class WebSocketServerImpl extends WebSocketServer {
37     // The logger for this class
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageServerImpl.class);
39
40     /**
41      * Constructor of this class.
42      *
43      * @param address host address of the local machine.
44      */
45     protected WebSocketServerImpl(final InetSocketAddress address) {
46         super(address);
47         LOGGER.entry(address.getAddress().getHostAddress() + ":" + address.getPort());
48         LOGGER.exit();
49     }
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.java_websocket.server.WebSocketServer#onOpen(org.java_websocket.WebSocket ,
55      * org.java_websocket.handshake.ClientHandshake)
56      */
57     @Override
58     public void onOpen(final WebSocket conn, final ClientHandshake handshake) {
59         LOGGER.entry(conn, handshake);
60         LOGGER.debug("A client connection opened from machine {}.",
61                 conn.getRemoteSocketAddress().getAddress().getHostAddress());
62         LOGGER.exit();
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.java_websocket.server.WebSocketServer#onClose(org.java_websocket. WebSocket, int, java.lang.String,
69      * boolean)
70      */
71     @Override
72     public void onClose(final WebSocket conn, final int code, final String reason, final boolean remote) {
73         LOGGER.entry(conn, code, remote);
74         LOGGER.debug("A client  connection from machine {} closing with code {}.",
75                 conn.getRemoteSocketAddress().getAddress().getHostAddress(), code);
76         LOGGER.exit();
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.java_websocket.server.WebSocketServer#onError(org.java_websocket.WebSocket, java.lang.Exception)
83      */
84     @Override
85     public void onError(final WebSocket conn, final Exception ex) {
86         // some errors like port binding failed may not be assignable to a specific web socket
87         LOGGER.error("server error occurred", ex);
88     }
89 }