Cloud log delivery to the Websocket based Server without xml
authorJung Seungho <shonest.jung@samsung.com>
Thu, 26 Jan 2017 04:02:21 +0000 (13:02 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Sun, 29 Jan 2017 06:58:48 +0000 (06:58 +0000)
Change-Id: I0a64c84efea8c15b30c67cc56f4f51a9ae3e5ee2
Signed-off-by: Jung Seungho <shonest.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16789
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
cloud/account/src/main/java/org/iotivity/cloud/accountserver/AccountServer.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java
cloud/messagequeue/src/main/java/org/iotivity/cloud/mqserver/MessageQueueServer.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/ResourceDirectoryServer.java
cloud/stack/pom.xml
cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapLogHandler.java
cloud/stack/src/main/java/org/iotivity/cloud/util/Log.java

index f2830e5..a5d3f1c 100644 (file)
@@ -29,8 +29,8 @@ import org.iotivity.cloud.accountserver.resources.account.session.SessionResourc
 import org.iotivity.cloud.accountserver.resources.account.tokenrefresh.TokenRefreshResource;
 import org.iotivity.cloud.accountserver.resources.acl.group.GroupResource;
 import org.iotivity.cloud.accountserver.resources.acl.id.AclResource;
-import org.iotivity.cloud.accountserver.resources.acl.verify.AclVerifyResource;
 import org.iotivity.cloud.accountserver.resources.acl.invite.InviteResource;
+import org.iotivity.cloud.accountserver.resources.acl.verify.AclVerifyResource;
 import org.iotivity.cloud.accountserver.resources.credprov.cert.CertificateResource;
 import org.iotivity.cloud.accountserver.resources.credprov.crl.CrlResource;
 import org.iotivity.cloud.base.ServerSystem;
@@ -45,15 +45,22 @@ import org.iotivity.cloud.util.Log;
 public class AccountServer {
 
     public static void main(String[] args) throws Exception {
-        Log.Init();
-
         System.out.println("-----Account SERVER-----");
 
-        if (args.length != 2) {
-            Log.e("coap server port and TLS mode required\n" + "ex) 5685 0\n");
+        Log.Init();
+
+        if (!(args.length == 2 || args.length == 4)) {
+            Log.e("coap server <Port> and TLS mode required\n"
+                    + "and WebSocketLog-Server <Address> <Port> (optional)\n"
+                    + "ex) 5685 0 127.0.0.1 8080\n");
             return;
         }
 
+        if (args.length == 4) {
+            Log.InitWebLog(args[2], args[3],
+                    AccountServer.class.getSimpleName().toString());
+        }
+
         ServerSystem serverSystem = new ServerSystem();
 
         serverSystem.addResource(new AccountResource());
index a330020..bbfa7de 100755 (executable)
@@ -28,8 +28,8 @@ import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.server.CoapServer;
 import org.iotivity.cloud.base.server.HttpServer;
 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
-import org.iotivity.cloud.ciserver.resources.RouteResource;
 import org.iotivity.cloud.ciserver.resources.KeepAliveResource;
+import org.iotivity.cloud.ciserver.resources.RouteResource;
 import org.iotivity.cloud.ciserver.resources.proxy.account.Account;
 import org.iotivity.cloud.ciserver.resources.proxy.account.AccountSession;
 import org.iotivity.cloud.ciserver.resources.proxy.account.Acl;
@@ -47,13 +47,16 @@ import org.iotivity.cloud.util.Log;
 public class CloudInterfaceServer {
 
     public static void main(String[] args) throws Exception {
+
         Log.Init();
 
         System.out.println("-----CI SERVER-------");
 
-        if (!(args.length == 8 || args.length == 9)) {
+        if (!(args.length == 8 || args.length == 9 || args.length == 10
+                || args.length == 11)) {
             Log.e("\nCoAP-server <Port> and RD-server <Address> <Port> Account-server <Address> <Port> MQ-broker <Address> <Port> HC-proxy [HTTP-port] and TLS-mode <0|1> are required.\n"
-                    + "ex) 5683 127.0.0.1 5684 127.0.0.1 5685 127.0.0.1 5686 80 0\n");
+                    + "and WebSocketLog-Server <Address> <Port> (optional)\n"
+                    + "ex) 5683 127.0.0.1 5684 127.0.0.1 5685 127.0.0.1 5686 80 0 127.0.0.1 8080\n");
             return;
         }
 
@@ -65,10 +68,21 @@ public class CloudInterfaceServer {
         boolean tlsMode = false;
         if (hcProxyMode) {
             tlsMode = Integer.parseInt(args[8]) == 1;
+
         } else {
             tlsMode = Integer.parseInt(args[7]) == 1;
         }
 
+        if (args.length == 10 || args.length == 11) {
+            if (hcProxyMode) {
+                Log.InitWebLog(args[9], args[10],
+                        CloudInterfaceServer.class.getSimpleName().toString());
+            } else {
+                Log.InitWebLog(args[8], args[9],
+                        CloudInterfaceServer.class.getSimpleName().toString());
+            }
+        }
+
         ConnectorPool.addConnection("rd",
                 new InetSocketAddress(args[1], Integer.parseInt(args[2])),
                 tlsMode);
@@ -92,7 +106,7 @@ public class CloudInterfaceServer {
         AclGroup aclGroupHandler = new AclGroup();
         Certificate certHandler = new Certificate();
         AclInvite aclInviteHandler = new AclInvite();
-       Crl crlHandler = new Crl();
+        Crl crlHandler = new Crl();
         CoapDevicePool devicePool = deviceServer.getDevicePool();
 
         deviceServer.addResource(acHandler);
@@ -117,7 +131,7 @@ public class CloudInterfaceServer {
 
         deviceServer.addResource(aclInviteHandler);
 
-       deviceServer.addResource(crlHandler);
+        deviceServer.addResource(crlHandler);
 
         KeepAliveResource resKeepAlive = new KeepAliveResource(
                 new int[] { 1, 2, 4, 8 });
index 79e32b9..a08cac3 100644 (file)
@@ -36,14 +36,20 @@ public class MessageQueueServer {
 
         System.out.println("-----MQ SERVER-----");
 
-        if (args.length != 6) {
+        if (!(args.length != 6 || args.length == 8)) {
             Log.e("coap server port, Kafka_zookeeper_Address port"
                     + " and Kafka_broker_Address Port and TLS mode required\n"
-                    + "ex) 5686 127.0.0.1 2181 127.0.0.1 9092 0\n");
+                    + " and WebSocketLog-Server <Address> <Port> (optional)\n"
+                    + "ex) 5686 127.0.0.1 2181 127.0.0.1 9092 0 127.0.0.1 8080\n");
 
             return;
         }
 
+        if (args.length == 8) {
+            Log.InitWebLog(args[6], args[7],
+                    MessageQueueServer.class.getSimpleName().toString());
+        }
+
         ServerSystem serverSystem = new ServerSystem();
 
         MQBrokerResource MQBroker = new MQBrokerResource();
index b5f41c2..d22cb38 100644 (file)
@@ -44,11 +44,16 @@ public class ResourceDirectoryServer {
 
         System.out.println("-----RD SERVER-----");
 
-        if (args.length != 2) {
+        if (!(args.length != 2 || args.length != 4)) {
             Log.e("coap server port and TLS mode required\n" + "ex) 5684 0\n");
             return;
         }
 
+        if (args.length == 4) {
+            Log.InitWebLog(args[2], args[3],
+                    ResourceDirectoryServer.class.getSimpleName().toString());
+        }
+
         ServerSystem serverSystem = new ServerSystem();
 
         serverSystem.addResource(new ResourceDirectoryResource());
index 325c608..9cdb5d8 100644 (file)
@@ -1,14 +1,15 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <groupId>org.iotivity.cloud</groupId>
-  <artifactId>CloudStack</artifactId>
-  <version>0.0.1-SNAPSHOT</version>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>org.iotivity.cloud</groupId>
+       <artifactId>CloudStack</artifactId>
+       <version>0.0.1-SNAPSHOT</version>
 
-  <properties>
+       <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.test.skip>true</maven.test.skip>
        </properties>
-  <dependencies>
+       <dependencies>
                <dependency>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>20140107</version>
                </dependency>
                <dependency>
-               <groupId>com.fasterxml.jackson.core</groupId>
-               <artifactId>jackson-databind</artifactId>
-               <version>2.4.0</version>
-               </dependency>
-               <dependency>
-                       <groupId>com.fasterxml.jackson.dataformat</groupId>
-                       <artifactId>jackson-dataformat-cbor</artifactId>
-                       <version>2.4.0</version>
+                       <groupId>com.fasterxml.jackson.core</groupId>
+                       <artifactId>jackson-databind</artifactId>
+                       <version>2.4.0</version>
+               </dependency>
+               <dependency>
+                       <groupId>com.fasterxml.jackson.dataformat</groupId>
+                       <artifactId>jackson-dataformat-cbor</artifactId>
+                       <version>2.4.0</version>
                </dependency>
                <dependency>
                        <groupId>junit</groupId>
                        <version>1.7.0</version>
                </dependency>
                <dependency>
-            <groupId>log4j</groupId>
-            <artifactId>log4j</artifactId>
-            <version>1.2.17</version>
-        </dependency>          
+                       <groupId>log4j</groupId>
+                       <artifactId>log4j</artifactId>
+                       <version>1.2.17</version>
+               </dependency>
+               <dependency>
+                       <groupId>javax.websocket</groupId>
+                       <artifactId>javax.websocket-client-api</artifactId>
+                       <version>1.0</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.glassfish.tyrus</groupId>
+                       <artifactId>tyrus-client</artifactId>
+                       <version>1.1</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.glassfish.tyrus</groupId>
+                       <artifactId>tyrus-container-grizzly</artifactId>
+                       <version>1.1</version>
+               </dependency>
+               <dependency>
+                       <groupId>javax.json</groupId>
+                       <artifactId>javax.json-api</artifactId>
+                       <version>1.0</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.glassfish</groupId>
+                       <artifactId>javax.json</artifactId>
+                       <version>1.0.1</version>
+               </dependency>
+               <dependency>
+                       <groupId>com.google.code.gson</groupId>
+                       <artifactId>gson</artifactId>
+                       <version>2.8.0</version>
+               </dependency>
        </dependencies>
-  <build>
+       <build>
                <plugins>
                        <plugin>
                                <inherited>true</inherited>
index 7eb1e50..37622b9 100644 (file)
  */
 package org.iotivity.cloud.base.protocols.coap;
 
+import java.util.ArrayList;
+
+import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.util.Cbor;
 import org.iotivity.cloud.util.Log;
 
+import com.google.gson.Gson;
+
 import io.netty.channel.ChannelDuplexHandler;
 import io.netty.channel.ChannelHandler.Sharable;
 import io.netty.channel.ChannelHandlerContext;
@@ -37,7 +44,10 @@ import io.netty.channel.ChannelPromise;
 @Sharable
 public class CoapLogHandler extends ChannelDuplexHandler {
 
-    static final int MAX_LOGLEN = 100;
+    private Cbor<Object> mCbor      = new Cbor<>();
+    private Gson         mGson      = new Gson();
+
+    static final int     MAX_LOGLEN = 100;
 
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
@@ -61,7 +71,17 @@ public class CoapLogHandler extends ChannelDuplexHandler {
     public void write(ChannelHandlerContext ctx, Object msg,
             ChannelPromise promise) {
 
-        String log = getCoapLog(ctx, msg);
+        String log = null;
+
+        if (msg instanceof CoapRequest) {
+            log = composeCoapRequest(
+                    ctx.channel().id().asLongText().substring(26),
+                    (CoapRequest) msg);
+        } else {
+            log = composeCoapResponse(
+                    ctx.channel().id().asLongText().substring(26),
+                    (CoapResponse) msg);
+        }
 
         Log.v(log);
 
@@ -72,70 +92,21 @@ public class CoapLogHandler extends ChannelDuplexHandler {
     public void channelRead(ChannelHandlerContext ctx, Object msg)
             throws Exception {
 
-        String log = getCoapLog(ctx, msg);
-
-        Log.v(log);
-
-        ctx.fireChannelRead(msg);
+        String log = null;
 
-    }
-
-    private String getCoapLog(ChannelHandlerContext ctx, Object msg) {
         if (msg instanceof CoapRequest) {
-            return composeCoapRequest(
+            log = composeCoapRequest(
                     ctx.channel().id().asLongText().substring(26),
                     (CoapRequest) msg);
-        } else if (msg instanceof CoapSignaling) {
-            return composeCoapSignaling(
-                    ctx.channel().id().asLongText().substring(26),
-                    (CoapSignaling) msg);
         } else {
-            return composeCoapResponse(
+            log = composeCoapResponse(
                     ctx.channel().id().asLongText().substring(26),
                     (CoapResponse) msg);
         }
-    }
 
-    private String composeCoapSignaling(String channelId,
-            CoapSignaling signaling) {
-        StringBuilder strBuilder = new StringBuilder();
-
-        strBuilder.append(channelId);
-        strBuilder.append(" " + signaling.getTokenString());
-
-        switch (signaling.getSignalingMethod()) {
-            case CSM:
-                strBuilder.append(" 7.01 CSM");
-                strBuilder.append(" SERVER-NAME:");
-                strBuilder.append(signaling.getCsmServerName());
-                strBuilder.append(" MAX-MESSAGE-SIZE:");
-                strBuilder.append(signaling.getCsmMaxMessageSize());
-                strBuilder.append(" BLOCK-WISE-TRANSFER:");
-                strBuilder.append(signaling.getCsmBlockWiseTransfer());
-                break;
-            case PING:
-                strBuilder.append(" 7.02 PING");
-                break;
-            case PONG:
-                strBuilder.append(" 7.03 PONG");
-                break;
-            case RELEASE:
-                strBuilder.append(" 7.04 RELEASE");
-                break;
-            case ABORT:
-                strBuilder.append(" 7.05 ABORT");
-                break;
-            default:
-                break;
-        }
+        Log.v(log);
 
-        if (signaling.getPayloadSize() > 0) {
-            strBuilder.append(" SZ:" + signaling.getPayloadSize() + " P:"
-                    + new String(signaling.getPayload(), 0,
-                            signaling.getPayloadSize() > MAX_LOGLEN ? MAX_LOGLEN
-                                    : signaling.getPayloadSize()));
-        }
-        return strBuilder.toString();
+        ctx.fireChannelRead(msg);
     }
 
     private String composeCoapRequest(String channelId, CoapRequest request) {
@@ -176,12 +147,9 @@ public class CoapLogHandler extends ChannelDuplexHandler {
         }
 
         if (request.getPayloadSize() > 0) {
-            strBuilder
-                    .append(" SZ:" + request.getPayloadSize() + " P:"
-                            + new String(request.getPayload(), 0,
-                                    request.getPayloadSize() > MAX_LOGLEN
-                                            ? MAX_LOGLEN
-                                            : request.getPayloadSize()));
+            strBuilder.append(" CT:" + request.getContentFormat());
+            strBuilder.append(" SZ:" + request.getPayloadSize() + " P:"
+                    + getJsonPayloadString(request));
         }
 
         return strBuilder.toString();
@@ -278,14 +246,30 @@ public class CoapLogHandler extends ChannelDuplexHandler {
         }
 
         if (response.getPayloadSize() > 0) {
-            strBuilder
-                    .append(" SZ:" + response.getPayloadSize() + " P:"
-                            + new String(response.getPayload(), 0,
-                                    response.getPayloadSize() > MAX_LOGLEN
-                                            ? MAX_LOGLEN
-                                            : response.getPayloadSize()));
+            strBuilder.append(" CT:" + response.getContentFormat());
+            strBuilder.append(" SZ:" + response.getPayloadSize() + " P:"
+                    + getJsonPayloadString(response));
         }
 
         return strBuilder.toString();
     }
+
+    private String getJsonPayloadString(CoapMessage coapMessage) {
+        String jsonPayload = null;
+        if (coapMessage.getContentFormat() == ContentFormat.APPLICATION_CBOR) {
+
+            Object mapPayload = mCbor.parsePayloadFromCbor(
+                    coapMessage.getPayload(), Object.class);
+            if (coapMessage.getUriPath()
+                    .contains(OICConstants.WELL_KNOWN_FULL_URI)) {
+                jsonPayload = mGson.toJson((ArrayList<Object>) mapPayload);
+                return jsonPayload.length() <= MAX_LOGLEN ? jsonPayload
+                        : jsonPayload.substring(0, MAX_LOGLEN);
+            }
+            jsonPayload = mGson.toJson(mapPayload);
+            return jsonPayload.length() <= MAX_LOGLEN ? jsonPayload
+                    : jsonPayload.substring(0, MAX_LOGLEN);
+        }
+        return jsonPayload;
+    }
 }
index 3dc258b..737d4cd 100644 (file)
@@ -25,6 +25,7 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.PrintStream;
+import java.net.URI;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 
@@ -34,24 +35,43 @@ import io.netty.channel.Channel;
 
 public class Log {
 
-    public static final int VERBOSE  = 0;
-    public static final int DEBUG    = 1;
-    public static final int INFO     = 2;
-    public static final int WARNING  = 3;
-    public static final int ERROR    = 4;
+    public static final int         VERBOSE        = 0;
+    public static final int         DEBUG          = 1;
+    public static final int         INFO           = 2;
+    public static final int         WARNING        = 3;
+    public static final int         ERROR          = 4;
 
-    private static int      logLevel = VERBOSE;
+    private static int              logLevel       = VERBOSE;
 
-    private static FileOutputStream fos = null;
-    private static PrintStream      ps  = null;
+    private static FileOutputStream fos            = null;
+    private static PrintStream      ps             = null;
 
-    private final static Logger logger = Logger.getLogger(Log.class);
+    private final static Logger     logger         = Logger
+            .getLogger(Log.class);
+    private static WebsocketLog     websocketpoint = null;
+    private static String           mServerName    = null;
 
     public static void Init() throws FileNotFoundException {
         System.setOut(Log.createLoggingProxy(System.out));
         createfile();
     }
 
+    public static void InitWebLog(String weblogHostname, String port,
+            String serverName) {
+        mServerName = serverName;
+
+        if (weblogHostname != null) {
+            try {
+                String LogServerAddr = "ws://" + weblogHostname + ":" + port;
+                websocketpoint = new WebsocketLog(new URI(LogServerAddr));
+                websocketpoint.start();
+            } catch (Exception e) {
+                System.out.println(e.getMessage());
+                websocketpoint = null;
+            }
+        }
+    }
+
     public static PrintStream createLoggingProxy(
             final PrintStream realPrintStream) {
         // TODO Auto-generated method stub
@@ -63,7 +83,7 @@ public class Log {
         };
     }
 
-    public static void createfile() throws FileNotFoundException{
+    public static void createfile() throws FileNotFoundException {
         File dir = new File("..//errLog//");
         if (!dir.isDirectory()) {
             dir.mkdirs();
@@ -81,7 +101,6 @@ public class Log {
         Log.v(log);
     }
 
-
     public static void setLogLevel(int level) {
         logLevel = level;
     }
@@ -142,6 +161,10 @@ public class Log {
         format += " " + log;
 
         System.out.println(format);
+
+        if (websocketpoint != null && websocketpoint.getUserSession() != null) {
+            websocketpoint.sendMessage("[" + mServerName + "]" + format);
+        }
     }
 
     private static String getDetailInfo() {