view src/fdl/ComDebug.java @ 4:2023d9b31af9

fix parameter
author fuchita
date Tue, 12 Feb 2008 09:15:25 +0900
parents ae7e0e92c651
children 4391c9fac885
line wrap: on
line source

package fdl;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;


public class ComDebug implements PSXQueueInterface{
	static final boolean debug = true;
	//public static int seq = 0;
	public static Hashtable<String, Integer> Com_Hashtable  = new Hashtable<String, Integer>();  
	public static LinkedList<SocketChannel> Report_Channellist = new LinkedList<SocketChannel>();
	
	ComDebug(){
		
	}

	public static void Report(LinkedList<SocketChannel> reportCh_list, ByteBuffer command, String report_txt) throws IOException {
		//レポートするチャンネルが0ならreturn
		if(reportCh_list.isEmpty()) {
			return;
		}
		//dataをセット
		ByteBuffer data = ByteBuffer.allocateDirect(24+(report_txt).length()*2);
		data.clear();  // position = 0 
		for(int i=0;i<report_txt.length();i++) {
			data.putChar(report_txt.charAt(i));
		}
    	data.flip();    // limit = current position, position = 0
    	
		//commandをセット
    	command.order(ByteOrder.BIG_ENDIAN);
    	command.putInt(LINDA_PACKET_LENGTH_OFFSET,(report_txt).length()*2+LINDA_HEADER_SIZE-INT_SIZE);
    	command.put(LINDA_MODE_OFFSET,(byte)'D');
    	command.putShort(LINDA_ID_OFFSET,(short) 0);
    	command.putInt(LINDA_SEQ_OFFSET,0);
    	command.putInt(LINDA_DATA_LENGTH_OFFSET,(report_txt).length()*2);
    	command.rewind();

		//送信
    	IOParam io = new IOParam();
		Iterator <SocketChannel> it = reportCh_list.iterator();
		while(it.hasNext()) {
		    io.send(it.next(), command, data);
		}
	}
	private static String getRemoteHostAndPort(SocketChannel channel) {
		String socketString = channel.socket().getRemoteSocketAddress().toString();
		String[] split = socketString.split("/");
		int length = split.length;
		String hostAndPort = split[length-1];
		split = hostAndPort.split(":");
		String host = split[0];
		String port = split[1];
		int portnum = Integer.parseInt(port);
		try {
			InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), portnum);
			host = address.getHostName().toString();
            return (host +":"+port);
        }
        catch( UnknownHostException e ){
        	return hostAndPort;
        }		
	}
	
	private static String getLocalHostAndPort(SocketChannel channel) {
		String socketString = channel.socket().getLocalSocketAddress().toString();
		String[] split = socketString.split("/");
		int length = split.length;
		String hostAndPort = split[length-1];
		split = hostAndPort.split(":");
		String host = split[0];
		String port = split[1];
		try {
            InetAddress localhost = InetAddress.getLocalHost();
            host = localhost.getHostName();            
            return (host +":"+port);
        }
        catch( UnknownHostException e ){
        	return (host +":"+port);
        }
	}
	
	public static String Com_inc(SelectionKey key, Hashtable<String, Integer> comlist, int mode) {
		//通信ログ Hostname:port 'mode' =number  形式でインクリメント
		int cnt = 0;
		SocketChannel ch = (SocketChannel) key.channel();
		
		String remoteString = getRemoteHostAndPort(ch);
		String localString =  getLocalHostAndPort(ch);

		String ComKey = localString + "--" + remoteString + " " + (char)mode;
		if(comlist.containsKey(ComKey)){
			cnt = comlist.get(ComKey);
		}
		cnt++;
		comlist.put(ComKey, cnt);
		long seq = System.currentTimeMillis();
		return (seq+" "+ComKey+"="+cnt);
	}
	
	public static void addChannel(SelectionKey key, LinkedList<SocketChannel> reportCh_list) {
		SocketChannel repch = (SocketChannel) key.channel();
		reportCh_list.add(repch);
	}
	
	public static void delChannel(SelectionKey key, LinkedList<SocketChannel> reportCh_list) {
		SocketChannel repch = (SocketChannel) key.channel();
		reportCh_list.remove(repch);
	}

	
	public void reportCh_remove(SelectionKey key, LinkedList<SocketChannel> reportCh_list) throws IOException {
		//レポートチャンネルが0ならreturn
		if(reportCh_list.isEmpty()) {
			return;
		}else {
			System.out.println("ComDebug Report Channel remove :"+key.channel());
			delChannel(key,reportCh_list);
		}
	}
	
}