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;
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());
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;
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;
}
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);
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);
deviceServer.addResource(aclInviteHandler);
- deviceServer.addResource(crlHandler);
+ deviceServer.addResource(crlHandler);
KeepAliveResource resKeepAlive = new KeepAliveResource(
new int[] { 1, 2, 4, 8 });
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();
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());
-<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>
*/
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;
@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 {
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);
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) {
}
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();
}
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;
+ }
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
+import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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
};
}
- public static void createfile() throws FileNotFoundException{
+ public static void createfile() throws FileNotFoundException {
File dir = new File("..//errLog//");
if (!dir.isDirectory()) {
dir.mkdirs();
Log.v(log);
}
-
public static void setLogLevel(int level) {
logLevel = level;
}
format += " " + log;
System.out.println(format);
+
+ if (websocketpoint != null && websocketpoint.getUserSession() != null) {
+ websocketpoint.sendMessage("[" + mServerName + "]" + format);
+ }
}
private static String getDetailInfo() {