2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.minecraft.internal.server;
16 import java.net.URISyntaxException;
18 import org.openhab.binding.minecraft.internal.message.OHMessage;
19 import org.openhab.core.thing.ThingUID;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.firebase.tubesock.WebSocket;
24 import com.firebase.tubesock.WebSocketException;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
29 import rx.Observable.OnSubscribe;
31 import rx.functions.Action0;
32 import rx.subscriptions.Subscriptions;
35 * Holds information about connection to Minecraft server.
37 * @author Mattias Markehed - Initial contribution
39 public class ServerConnection {
43 private ThingUID thingUID;
44 private Gson gson = new GsonBuilder().create();
46 private MinecraftSocketHandler socketHandler;
48 private WebSocket webSocket;
50 private ServerConnection(ThingUID thingUID, String host, int port) {
53 this.thingUID = thingUID;
56 public class ServerData {
59 public ServerConnection connection;
61 public ServerData(String host, int port, ServerConnection connection) {
64 this.connection = connection;
69 * Get host address of server.
71 * @return server host address.
73 public String getHost() {
78 * Get port used to communicate to Minecraft server.
82 public int getPort() {
91 public ThingUID getThingUID() {
95 public void sendMessage(OHMessage message) {
96 String serializedMessage = gson.toJson(message);
97 webSocket.send(serializedMessage);
101 * Add the handler used to handle messages state updates web socket.
103 * @param handler the websocket handler to use for new connections.
105 private void setSocketHandler(MinecraftSocketHandler handler) {
106 socketHandler = handler;
109 private void setWebSocket(WebSocket webSocket) {
110 this.webSocket = webSocket;
114 * Get the object used to handle state changes and messages from web socket.
116 * @return handler for web socket.
118 public MinecraftSocketHandler getSocketHandler() {
119 return socketHandler;
123 * Directly connect to server.
124 * Reconnects when connection is lost
126 public static Observable<ServerConnection> create(final ThingUID thingUID, final String host, final int port) {
127 final String serverUrl = String.format("ws://%s:%d/stream", host, port);
129 return Observable.<ServerConnection> create(new OnSubscribe<ServerConnection>() {
131 private final Logger logger = LoggerFactory.getLogger(ServerConnection.class);
134 public void call(final Subscriber<? super ServerConnection> subscriber) {
135 logger.info("Start connecting to Minecraft server at: {}", serverUrl);
136 if (!subscriber.isUnsubscribed()) {
137 ServerConnection serverConnection = new ServerConnection(thingUID, host, port);
138 MinecraftSocketHandler socketHandler = new MinecraftSocketHandler() {
140 public void onError(WebSocketException e) {
141 subscriber.onError(e);
145 public void onClose() {
146 logger.info("Connection to Minecraft server stopped");
147 subscriber.onCompleted();
153 destUri = new URI(serverUrl);
154 } catch (URISyntaxException e) {
155 subscriber.onError(e);
157 final WebSocket websocket = new WebSocket(destUri);
158 websocket.setEventHandler(socketHandler);
161 serverConnection.setSocketHandler(socketHandler);
162 serverConnection.setWebSocket(websocket);
163 subscriber.onNext(serverConnection);
165 subscriber.add(Subscriptions.create(new Action0() {
168 subscriber.unsubscribe();
178 public int hashCode() {
179 final int prime = 31;
181 result = prime * result + ((host == null) ? 0 : host.hashCode());
182 result = prime * result + port;
183 result = prime * result + ((socketHandler == null) ? 0 : socketHandler.hashCode());
184 result = prime * result + ((thingUID == null) ? 0 : thingUID.hashCode());
189 public boolean equals(Object obj) {
196 if (getClass() != obj.getClass()) {
199 ServerConnection other = (ServerConnection) obj;
201 if (other.host != null) {
204 } else if (!host.equals(other.host)) {
207 if (port != other.port) {
210 if (socketHandler == null) {
211 if (other.socketHandler != null) {
214 } else if (!socketHandler.equals(other.socketHandler)) {
217 if (thingUID == null) {
218 if (other.thingUID != null) {
221 } else if (!thingUID.equals(other.thingUID)) {