Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gazer2kanlin committed Dec 20, 2018
1 parent f80a3f9 commit 2802099
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion uia.comm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.uia.solution</groupId>
<artifactId>uia-comm</artifactId>
<version>0.3.1</version>
<version>0.3.2</version>
<packaging>jar</packaging>
<name>uia-comm</name>
<url>https://github.com/uia4j/uia-comm</url>
Expand Down
22 changes: 11 additions & 11 deletions uia.comm/src/main/java/uia/comm/SocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public SocketClient(final Protocol<SocketDataController> protocol, final Message
* @param clientPort Client port.
*/
public SocketClient(final Protocol<SocketDataController> protocol, final MessageManager manager, String aliasName, int clientPort) {
this.clientPort = clientPort;
this.aliasName = aliasName;
this.protocol = protocol;
this.protocol.addMessageHandler(this);
this.manager = manager;
this.callIns = new HashMap<String, MessageCallIn<SocketDataController>>();
this.callOuts = new ConcurrentHashMap<String, MessageCallOut>();
this.started = false;
this.aliasName = aliasName;
this.maxCache = 20 * 1024; // 20K
this.clientPort = clientPort;
}

public int getMaxCache() {
Expand All @@ -117,11 +117,6 @@ public void setMaxCache(int maxCache) {
this.maxCache = Math.max(16, maxCache);
}

@Override
public String getName() {
return this.aliasName;
}

/**
* Get address.
* @return Address.
Expand Down Expand Up @@ -266,6 +261,11 @@ public synchronized boolean tryConnect() {
}
}

@Override
public String getName() {
return this.aliasName;
}

@Override
public Protocol<SocketDataController> getProtocol() {
return this.protocol;
Expand Down Expand Up @@ -318,10 +318,10 @@ public boolean send(final byte[] data, int times) throws SocketException {
return this.controller.send(data, times);
}
catch (Exception ex) {
logger.error(String.format("%s> send %s failed. ex:%s",
logger.error(String.format("%s> send %s failed",
this.aliasName,
ByteUtils.toHexString(data, 100),
ex.getMessage()));
ByteUtils.toHexString(data, 100)),
ex);
return false;
}
}
Expand Down Expand Up @@ -369,7 +369,7 @@ public boolean send(final byte[] data, final MessageCallOut callOut, long timeou
}

@Override
public boolean send(final byte[] data, MessageCallOut callOut, long timeout, int retry) throws SocketException {
public boolean send(final byte[] data, final MessageCallOut callOut, long timeout, int retry) throws SocketException {
if (!this.started) {
throw new SocketException(this.aliasName + "> is not started.");
}
Expand Down
2 changes: 2 additions & 0 deletions uia.comm/src/main/java/uia/comm/SocketDataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;

import org.apache.log4j.Logger;
Expand Down Expand Up @@ -149,6 +150,7 @@ public void run() {
}

void lastUpdate() {
logger.info(this.name + "> lastUpdate: " + new Date());
this.lastUpdate = System.currentTimeMillis();
}

Expand Down
20 changes: 15 additions & 5 deletions uia.comm/src/main/java/uia/comm/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public SocketServer(Protocol<SocketDataController> protocol, int port, MessageMa
this.controllers = new ConcurrentHashMap<String, SocketDataController>();
this.listeners = new ArrayList<SocketServerListener>();

this.idleTime = 60000;
this.idleTime = 300000;
this.port = port;
this.maxCache = 20 * 1024; // 20K
}
Expand Down Expand Up @@ -316,7 +316,10 @@ public void run() {
MessageCallOut out = callOutsRef.remove(tx);
if (out != null) {
try {
logger.info(String.format("%s> tx:%s callOut timeout", clientName, out.getTxId()));
logger.info(String.format("%s> %s> tx:%s callOut timeout",
SocketServer.this.aliasName,
clientName,
out.getTxId()));
out.timeout();
}
catch(Exception ex) {
Expand Down Expand Up @@ -406,7 +409,7 @@ public void disconnect(String clientName) {
this.clientCallouts.remove(clientName);

if (controller != null) {
logger.info(String.format("%s> %s disconnected, count:%s", this.aliasName, clientName, this.controllers.size()));
logger.info(String.format("%s> %s> disconnected, count:%s", this.aliasName, clientName, this.controllers.size()));
SelectionKey key = controller.getChannel().keyFor(this.serverSelector);
if (key != null) {
key.cancel();
Expand Down Expand Up @@ -583,6 +586,7 @@ public void messageError(ProtocolMonitor<SocketDataController> monitor, Protocol

private void polling() {
if (this.started) {
logger.info(this.aliasName + "> polling");
ArrayList<String> keys = new ArrayList<String>();
Collection<SocketDataController> controllers = this.controllers.values();
synchronized (this.controllers) {
Expand All @@ -594,6 +598,9 @@ private void polling() {
}

for (String key : keys) {
logger.info(String.format("%s> %s> try to disconnect(polling)",
this.aliasName,
key));
disconnect(key);
}
}
Expand All @@ -608,7 +615,7 @@ private void running() {
this.serverSelector.select(); // wait NIO event.
}
catch (Exception ex) {
logger.error("comm> ", ex);
logger.error(this.aliasName + "> NIO failed", ex);
continue;
}

Expand All @@ -631,6 +638,9 @@ private void running() {
else if (key.isReadable()) {
SocketDataController controller = (SocketDataController) key.attachment();
if (!controller.receive()) {
logger.info(String.format("%s> %s> try to disconnect(running)",
this.aliasName,
key));
disconnect(controller.getName());
}
}
Expand Down Expand Up @@ -678,7 +688,7 @@ private void clientConnected(SocketChannel client) {
// use server selector
client.register(this.serverSelector, SelectionKey.OP_READ, controller);

logger.info(String.format("%s> %s connected, count:%s", this.aliasName, clientId, this.controllers.size()));
logger.info(String.format("%s> %s> connected, count:%s", this.aliasName, clientId, this.controllers.size()));

new Thread(new Runnable() {

Expand Down

0 comments on commit 2802099

Please sign in to comment.