view src/fdl/CommDebugHook.java @ 71:0352536c33fa

(example: writer) get linda server addr from commandline arg.
author kazz@e065701.local
date Fri, 23 Oct 2009 14:11:07 +0900
parents 2a366abc3f1f
children
line wrap: on
line source

package fdl;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.util.LinkedList;

public class CommDebugHook implements IOHandlerHook {

	public LinkedList<String> logs = new LinkedList<String>();
	// String conversion is too expensive, should use ByteBuffer directory or
	// create log object for the purpose.

	public void closeHook(SelectionKey key) {
		if (key!=null) logs.add(closeLog(key));
	}


	public void checkHook(SelectionKey key, int id, int seq, char mode) {
		if (isPrivilege(key, id)) return;
		logs.add(log(key, id, seq, mode, null));
	}


	public void inHook(SelectionKey key, int id, int seq, char mode) {
		if (isPrivilege(key, id)) return;
		logs.add(log(key, id, seq, mode, null));
	}


	private boolean isPrivilege(SelectionKey key, int id) {
		return key==null || id>=PSX.PRIVILEGED_ID_START;
	}


	public void outHook(SelectionKey key, int id, int seq, char mode, ByteBuffer data) {
		if (isPrivilege(key, id)) return;
		String sendtext = PSX.getdataString(data);
		logs.add(log(key, id, seq, mode, sendtext));
	}


	public void waitReadHook(SelectionKey key, int id, int seq, char mode) {
		if (isPrivilege(key, id)) return;
		logs.add(log(key, id, seq, mode, null));
	}

	public String log(SelectionKey key,int id, int seq,char mode, String sendtext) {
		//通信ログ Hostname:port 'mode' =number  形式でインクリメント
		int cnt = 0;
		if (sendtext==null) sendtext="none";

		IOHandler io = (IOHandler) key.attachment();
		String ComKey = io.localString + "--" + io.remoteString + " " + mode;
		
		io.cnt++;
		long time = System.currentTimeMillis();
		return (time+" "+ComKey+"="+cnt+" id="+id+" seq="+seq+" data="+sendtext);
	}

	private String closeLog(SelectionKey key) {
		String close_Channel = key.channel().toString();
		String[] split = close_Channel.split("/");
		String local[] = split[1].split(" ");
		String remote[] = split[2].split("]");

		String localAddress = local[0];
		String remoteAddress = remote[0];

		return  "CloseInfo >"+localAddress+"--"+remoteAddress;
	}
	
	public ByteBuffer getLog() {
		String log = logs.poll();
		if (log==null) return null;
		return PSX.string2ByteBuffer(log);
	}

}