![]() |
Hubiquitus Android
0.3
Android client for hubiquitus protocol
|
00001 /* 00002 * Copyright (c) Novedia Group 2012. 00003 * 00004 * This file is part of Hubiquitus. 00005 * 00006 * Hubiquitus is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * Hubiquitus is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with Hubiquitus. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 package org.hubiquitus.hapi.transport.socketio; 00021 00022 import java.util.Timer; 00023 import java.util.TimerTask; 00024 00025 import io.socket.IOAcknowledge; 00026 import io.socket.IOCallback; 00027 import io.socket.SocketIO; 00028 import io.socket.SocketIOException; 00029 00030 import org.hubiquitus.hapi.hStructures.ConnectionError; 00031 import org.hubiquitus.hapi.hStructures.ConnectionStatus; 00032 import org.hubiquitus.hapi.hStructures.HStatus; 00033 import org.hubiquitus.hapi.transport.HTransport; 00034 import org.hubiquitus.hapi.transport.HTransportDelegate; 00035 import org.hubiquitus.hapi.transport.HTransportOptions; 00036 import org.json.JSONObject; 00037 00044 public class HTransportSocketio implements HTransport, IOCallback { 00045 00046 private HTransportDelegate callback = null; 00047 private HTransportOptions options = null; 00048 private SocketIO socketio = null; 00049 private ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED; 00050 private Timer timeoutTimer = null; 00051 00052 public HTransportSocketio() { 00053 }; 00054 00060 public void connect(HTransportDelegate callback, HTransportOptions options){ 00061 this.connectionStatus = ConnectionStatus.CONNECTING; 00062 00063 this.callback = callback; 00064 this.options = options; 00065 00066 String endpointHost = options.getEndpointHost(); 00067 int endpointPort = options.getEndpointPort(); 00068 String endpointPath = options.getEndpointPath(); 00069 00070 String endpointAdress = toEndpointAdress(endpointHost, endpointPort, endpointPath); 00071 //add a timer to make sure it doesn't go over timeout 00072 timeoutTimer = new Timer(); 00073 //set timer task to add a connection timeout 00074 timeoutTimer.schedule(new TimerTask() { 00075 00076 @Override 00077 public void run() { 00078 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.CONN_TIMEOUT, null); 00079 if(socketio.isConnected()) { 00080 socketio.disconnect(); 00081 } 00082 00083 socketio = null; 00084 } 00085 }, 10000); 00086 00087 //init socketio component 00088 try { 00089 socketio = new SocketIO(); 00090 } catch (Exception e) { 00091 if (timeoutTimer != null) { 00092 timeoutTimer.cancel(); 00093 timeoutTimer = null; 00094 } 00095 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.TECH_ERROR, e.getMessage()); 00096 socketio = null; 00097 } 00098 00099 //connect 00100 try { 00101 socketio.connect(endpointAdress, this); 00102 } catch (Exception e) { 00103 if (timeoutTimer != null) { 00104 timeoutTimer.cancel(); 00105 timeoutTimer = null; 00106 } 00107 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.TECH_ERROR, e.getMessage()); 00108 } 00109 } 00110 00117 public void updateStatus(ConnectionStatus status, ConnectionError error, String errorMsg) { 00118 this.connectionStatus = status; 00119 if (callback != null) { 00120 callback.onStatus(status, error, errorMsg); 00121 } else { 00122 throw new NullPointerException("Error : " + this.getClass().getName() + " requires a callback"); 00123 } 00124 } 00125 00131 public void disconnect() { 00132 this.connectionStatus = ConnectionStatus.DISCONNECTING; 00133 try { 00134 socketio.disconnect(); 00135 } catch (Exception e) { 00136 } 00137 00138 } 00139 00140 /* helper functions */ 00141 00145 public String toEndpointAdress(String endpointHost, int endpointPort, String endpointPath) { 00146 String endpointAdress = ""; 00147 endpointAdress += "http://"; 00148 if (endpointHost != null) { 00149 endpointAdress += endpointHost; 00150 } 00151 00152 if (endpointPort != 0) { 00153 endpointAdress += ":" + endpointPort; 00154 } 00155 00156 //endpointAdress += "/"; 00157 00158 if(endpointPath != null) { 00159 endpointAdress += endpointPath; 00160 } 00161 00162 return endpointAdress; 00163 } 00164 00165 @Override 00166 public void sendObject(JSONObject object) { 00167 if( connectionStatus == ConnectionStatus.CONNECTED) { 00168 socketio.emit("hCommand",object); 00169 } else { 00170 System.out.println("Not connected"); 00171 } 00172 } 00173 00174 /* Socket io delegate callback*/ 00175 public void on(String type, IOAcknowledge arg1, Object... arg2) { 00176 //switch for type 00177 if (type.equals("hStatus") && arg2 != null && arg2[0].getClass() == JSONObject.class) { 00178 JSONObject data = (JSONObject)arg2[0]; 00179 try { 00180 HStatus status = new HStatus(data); 00181 if (timeoutTimer != null) { 00182 timeoutTimer.cancel(); 00183 timeoutTimer = null; 00184 } 00185 updateStatus(status.getStatus(), status.getErrorCode(), status.getErrorMsg()); 00186 } catch (Exception e) { 00187 e.printStackTrace(); 00188 00189 if (timeoutTimer != null) { 00190 timeoutTimer.cancel(); 00191 timeoutTimer = null; 00192 } 00193 socketio.disconnect(); 00194 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.TECH_ERROR, e.getMessage()); 00195 } 00196 } else { 00197 JSONObject data = (JSONObject)arg2[0]; 00198 try { 00199 if (timeoutTimer != null) { 00200 timeoutTimer.cancel(); 00201 timeoutTimer = null; 00202 } 00203 callback.onData(type, data); 00204 } catch (Exception e) { 00205 if (timeoutTimer != null) { 00206 timeoutTimer.cancel(); 00207 timeoutTimer = null; 00208 } 00209 } 00210 } 00211 } 00212 00213 @Override 00214 public void onConnect() { 00215 //try to log in once connected 00216 String publisher = options.getJid().getFullJID(); 00217 String password = options.getPassword(); 00218 String serverHost = options.getServerHost(); 00219 int serverPort = options.getServerPort(); 00220 00221 //prepare data to be sent 00222 JSONObject data = new JSONObject(); 00223 try { 00224 data.put("publisher", publisher); 00225 data.put("password", password); 00226 data.put("serverHost", serverHost); 00227 data.put("serverPort", serverPort); 00228 00229 //send the event 00230 socketio.emit("hConnect", data); 00231 } catch (Exception e) { 00232 if(socketio != null) { 00233 socketio.disconnect(); 00234 } 00235 if (timeoutTimer != null) { 00236 timeoutTimer.cancel(); 00237 timeoutTimer = null; 00238 } 00239 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.TECH_ERROR, e.getMessage()); 00240 } 00241 } 00242 00243 @Override 00244 public void onDisconnect() { 00245 if (timeoutTimer != null) { 00246 timeoutTimer.cancel(); 00247 timeoutTimer = null; 00248 } 00249 00250 if (this.connectionStatus != ConnectionStatus.DISCONNECTED) { 00251 // while(socketio.isConnected()) { 00252 // socketio.disconnect(); 00253 // } 00254 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.NO_ERROR, null); 00255 } 00256 } 00257 00258 @Override 00259 public void onError(SocketIOException arg0) { 00260 if (socketio != null && socketio.isConnected()) { 00261 socketio.disconnect(); 00262 } 00263 socketio = null; 00264 String errorMsg = null; 00265 if (arg0 != null) { 00266 errorMsg = arg0.getMessage(); 00267 } 00268 if (timeoutTimer != null) { 00269 timeoutTimer.cancel(); 00270 timeoutTimer = null; 00271 } 00272 updateStatus(ConnectionStatus.DISCONNECTED, ConnectionError.TECH_ERROR, errorMsg); 00273 } 00274 00275 @Override 00276 public void onMessage(String arg0, IOAcknowledge arg1) { 00277 System.out.println("socketio" + arg0); 00278 } 00279 00280 @Override 00281 public void onMessage(JSONObject arg0, IOAcknowledge arg1) { 00282 System.out.println("socketio" + arg0.toString()); 00283 } 00284 00285 @Override 00286 public String toString() { 00287 return "HTransportSocketio [callback=" + callback + ", options=" 00288 + options + ", socketio=" + socketio + ", connectionStatus=" 00289 + connectionStatus + ", timeoutTimer=" + timeoutTimer + "]"; 00290 } 00291 } 00292