]> git.basschouten.com Git - openhab-addons.git/blob
c2d3ab4b06e42d2dc924cb997c3433f0935a0e6e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.samsungtv.internal.protocol;
14
15 import java.io.IOException;
16 import java.net.URI;
17 import java.util.concurrent.Future;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.websocket.api.Session;
22 import org.eclipse.jetty.websocket.api.WebSocketAdapter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Websocket base class
28  *
29  * @author Arjan Mels - Initial contribution
30  */
31 @NonNullByDefault
32 class WebSocketBase extends WebSocketAdapter {
33     private final Logger logger = LoggerFactory.getLogger(WebSocketBase.class);
34     /**
35      *
36      */
37     final RemoteControllerWebSocket remoteControllerWebSocket;
38
39     private @Nullable Future<?> sessionFuture;
40
41     /**
42      * @param remoteControllerWebSocket
43      */
44     WebSocketBase(RemoteControllerWebSocket remoteControllerWebSocket) {
45         this.remoteControllerWebSocket = remoteControllerWebSocket;
46     }
47
48     boolean isConnecting = false;
49
50     @Override
51     public void onWebSocketClose(int statusCode, @Nullable String reason) {
52         logger.debug("{} connection closed: {} - {}", this.getClass().getSimpleName(), statusCode, reason);
53         super.onWebSocketClose(statusCode, reason);
54         isConnecting = false;
55     }
56
57     @Override
58     public void onWebSocketError(@Nullable Throwable error) {
59         if (logger.isTraceEnabled()) {
60             logger.trace("{} connection error", this.getClass().getSimpleName(), error);
61         } else {
62             logger.debug("{} connection error", this.getClass().getSimpleName());
63         }
64         super.onWebSocketError(error);
65         isConnecting = false;
66     }
67
68     void connect(URI uri) throws RemoteControllerException {
69         if (isConnecting || isConnected()) {
70             logger.trace("{} already connecting or connected", this.getClass().getSimpleName());
71             return;
72         }
73
74         logger.debug("{} connecting to: {}", this.getClass().getSimpleName(), uri);
75         isConnecting = true;
76
77         try {
78             sessionFuture = remoteControllerWebSocket.client.connect(this, uri);
79             logger.trace("Connecting session Future: {}", sessionFuture);
80         } catch (IOException | IllegalStateException e) {
81             throw new RemoteControllerException(e);
82         }
83     }
84
85     @Override
86     public void onWebSocketConnect(@Nullable Session session) {
87         logger.debug("{} connection established: {}", this.getClass().getSimpleName(),
88                 session != null ? session.getRemoteAddress().getHostString() : "");
89         super.onWebSocketConnect(session);
90
91         isConnecting = false;
92     }
93
94     void close() {
95         logger.debug("{} connection close requested", this.getClass().getSimpleName());
96
97         Session session = getSession();
98         if (session != null) {
99             session.close();
100         }
101
102         final Future<?> sessionFuture = this.sessionFuture;
103         logger.trace("Closing session Future: {}", sessionFuture);
104         if (sessionFuture != null && !sessionFuture.isDone()) {
105             sessionFuture.cancel(true);
106         }
107     }
108
109     void sendCommand(String cmd) {
110         try {
111             if (isConnected()) {
112                 getRemote().sendString(cmd);
113                 logger.trace("{}: sendCommand: {}", this.getClass().getSimpleName(), cmd);
114             } else {
115                 logger.warn("{} sending command while socket not connected: {}", this.getClass().getSimpleName(), cmd);
116                 // retry opening connection just in case
117                 remoteControllerWebSocket.openConnection();
118
119                 getRemote().sendString(cmd);
120                 logger.trace("{}: sendCommand: {}", this.getClass().getSimpleName(), cmd);
121             }
122         } catch (IOException | RemoteControllerException e) {
123             logger.warn("{}: cannot send command", this.getClass().getSimpleName(), e);
124         }
125     }
126
127     @Override
128     public void onWebSocketText(@Nullable String str) {
129         logger.trace("{}: onWebSocketText: {}", this.getClass().getSimpleName(), str);
130     }
131 }