]> git.basschouten.com Git - openhab-addons.git/blob
ff33d23a9617dd76ed36b8d0d1aaedf0b9c41ab6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.minecraft.internal.server;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17
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;
22
23 import com.firebase.tubesock.WebSocket;
24 import com.firebase.tubesock.WebSocketException;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27
28 import rx.Observable;
29 import rx.Observable.OnSubscribe;
30 import rx.Subscriber;
31 import rx.functions.Action0;
32 import rx.subscriptions.Subscriptions;
33
34 /**
35  * Holds information about connection to Minecraft server.
36  *
37  * @author Mattias Markehed - Initial contribution
38  */
39 public class ServerConnection {
40
41     private String host;
42     private int port;
43     private ThingUID thingUID;
44     private Gson gson = new GsonBuilder().create();
45
46     private MinecraftSocketHandler socketHandler;
47
48     private WebSocket webSocket;
49
50     private ServerConnection(ThingUID thingUID, String host, int port) {
51         this.host = host;
52         this.port = port;
53         this.thingUID = thingUID;
54     }
55
56     public class ServerData {
57         public String host;
58         public int port;
59         public ServerConnection connection;
60
61         public ServerData(String host, int port, ServerConnection connection) {
62             this.host = host;
63             this.port = port;
64             this.connection = connection;
65         }
66     }
67
68     /**
69      * Get host address of server.
70      *
71      * @return server host address.
72      */
73     public String getHost() {
74         return host;
75     }
76
77     /**
78      * Get port used to communicate to Minecraft server.
79      *
80      * @return
81      */
82     public int getPort() {
83         return port;
84     }
85
86     /**
87      * Get UID of server
88      *
89      * @return server uid.
90      */
91     public ThingUID getThingUID() {
92         return thingUID;
93     }
94
95     public void sendMessage(OHMessage message) {
96         String serializedMessage = gson.toJson(message);
97         webSocket.send(serializedMessage);
98     }
99
100     /**
101      * Add the handler used to handle messages state updates web socket.
102      *
103      * @param handler the websocket handler to use for new connections.
104      */
105     private void setSocketHandler(MinecraftSocketHandler handler) {
106         socketHandler = handler;
107     }
108
109     private void setWebSocket(WebSocket webSocket) {
110         this.webSocket = webSocket;
111     }
112
113     /**
114      * Get the object used to handle state changes and messages from web socket.
115      *
116      * @return handler for web socket.
117      */
118     public MinecraftSocketHandler getSocketHandler() {
119         return socketHandler;
120     }
121
122     /**
123      * Directly connect to server.
124      * Reconnects when connection is lost
125      */
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);
128
129         return Observable.<ServerConnection> create(new OnSubscribe<ServerConnection>() {
130
131             private final Logger logger = LoggerFactory.getLogger(ServerConnection.class);
132
133             @Override
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() {
139                         @Override
140                         public void onError(WebSocketException e) {
141                             subscriber.onError(e);
142                         }
143
144                         @Override
145                         public void onClose() {
146                             logger.info("Connection to Minecraft server stopped");
147                             subscriber.onCompleted();
148                         }
149                     };
150
151                     URI destUri = null;
152                     try {
153                         destUri = new URI(serverUrl);
154                     } catch (URISyntaxException e) {
155                         subscriber.onError(e);
156                     }
157                     final WebSocket websocket = new WebSocket(destUri);
158                     websocket.setEventHandler(socketHandler);
159                     websocket.connect();
160
161                     serverConnection.setSocketHandler(socketHandler);
162                     serverConnection.setWebSocket(websocket);
163                     subscriber.onNext(serverConnection);
164
165                     subscriber.add(Subscriptions.create(new Action0() {
166                         @Override
167                         public void call() {
168                             subscriber.unsubscribe();
169                             websocket.close();
170                         }
171                     }));
172                 }
173             }
174         });
175     }
176
177     @Override
178     public int hashCode() {
179         final int prime = 31;
180         int result = 1;
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());
185         return result;
186     }
187
188     @Override
189     public boolean equals(Object obj) {
190         if (this == obj) {
191             return true;
192         }
193         if (obj == null) {
194             return false;
195         }
196         if (getClass() != obj.getClass()) {
197             return false;
198         }
199         ServerConnection other = (ServerConnection) obj;
200         if (host == null) {
201             if (other.host != null) {
202                 return false;
203             }
204         } else if (!host.equals(other.host)) {
205             return false;
206         }
207         if (port != other.port) {
208             return false;
209         }
210         if (socketHandler == null) {
211             if (other.socketHandler != null) {
212                 return false;
213             }
214         } else if (!socketHandler.equals(other.socketHandler)) {
215             return false;
216         }
217         if (thingUID == null) {
218             if (other.thingUID != null) {
219                 return false;
220             }
221         } else if (!thingUID.equals(other.thingUID)) {
222             return false;
223         }
224         return true;
225     }
226 }