fixed bug relate to weblog and buildscript
authorJung Seungho <shonest.jung@samsung.com>
Tue, 31 Jan 2017 02:10:45 +0000 (11:10 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Thu, 2 Feb 2017 04:58:23 +0000 (04:58 +0000)
- Added WebsocketLog.java related to Weblog delivery to the Websocket based Server
- modified SConscript in cloudSample

Change-Id: I64ccdeae9514e424ff19dbcc38e3fc63529f96f7
Signed-off-by: Jung Seungho <shonest.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16907
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
cloud/samples/client/SConscript
cloud/stack/src/main/java/org/iotivity/cloud/util/WebsocketLog.java [new file with mode: 0644]

index 32e8a1c..555cbfa 100644 (file)
@@ -55,7 +55,7 @@ cc_sample_app_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-Wextra', '-st
 cc_sample_app_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
 cc_sample_app_env.AppendUnique(RPATH = [env.get('BUILD_DIR')])
 
-cc_sample_app_env.PrependUnique(LIBS = ['oc', 'octbstack','connectivity_abstraction','pthread', 'resource_directory', 'ocpmapi'])
+cc_sample_app_env.PrependUnique(LIBS = ['oc', 'octbstack','connectivity_abstraction','pthread', 'resource_directory'])
 
 cc_sample_app_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD', 'RD_CLIENT'])
 
@@ -68,6 +68,7 @@ cc_cloud_src_dir = src_dir + '/cloud/samples/client/'
 tlsOption = env.get('SECURED')
 if tlsOption is '1':
        cc_sample_app_env.Install('.', cc_cloud_src_dir + 'rootca.crt')
+       cc_sample_app_env.PrependUnique(LIBS = ['ocpmapi'])
 
 ######################################################################
 # Sample for the thin cloud device
diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/util/WebsocketLog.java b/cloud/stack/src/main/java/org/iotivity/cloud/util/WebsocketLog.java
new file mode 100644 (file)
index 0000000..1e59023
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+package org.iotivity.cloud.util;
+
+import java.io.IOException;
+import java.net.URI;
+
+import javax.websocket.ClientEndpoint;
+import javax.websocket.CloseReason;
+import javax.websocket.ContainerProvider;
+import javax.websocket.DeploymentException;
+import javax.websocket.OnClose;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import javax.websocket.WebSocketContainer;
+
+@ClientEndpoint
+public class WebsocketLog extends Thread {
+    private Session            userSession = null;
+    private MessageHandler     messageHandler;
+    private WebSocketContainer container;
+    private URI                endpointURI;
+
+    public WebsocketLog(URI endpointURI)
+            throws DeploymentException, IOException {
+        container = ContainerProvider.getWebSocketContainer();
+        this.endpointURI = endpointURI;
+    }
+
+    @Override
+    public void run() {
+        startConnect();
+    }
+
+    @OnOpen
+    public void onOpen(Session userSession) {
+        this.userSession = userSession;
+    }
+
+    @OnClose
+    public void onClose(Session userSession, CloseReason reason)
+            throws InterruptedException {
+        this.userSession = null;
+        Log.d("Disconnected to websocket for Web Log");
+        startConnect();
+    }
+
+    @OnMessage
+    public void onMessage(String message) {
+        if (this.messageHandler != null)
+            this.messageHandler.handleMessage(message);
+    }
+
+    public void addMessageHandler(MessageHandler msgHandler) {
+        this.messageHandler = msgHandler;
+    }
+
+    public void sendMessage(String message) {
+        this.userSession.getAsyncRemote().sendText(message);
+    }
+
+    public static interface MessageHandler {
+        public void handleMessage(String message);
+    }
+
+    public Session getUserSession() {
+        return userSession;
+    }
+
+    public void startConnect() {
+        int retryCount = 0;
+        do {
+            if (retryCount != 0) {
+                Log.d("retry to Connecting... " + retryCount);
+            }
+
+            try {
+                container.connectToServer(this, endpointURI);
+                Log.d("Success to websocket for Web Log");
+            } catch (DeploymentException | IOException e) {
+                Log.d("Skip Connect to websocket for Web Log");
+            }
+            retryCount++;
+        } while (userSession == null && retryCount < 4);
+    }
+}