view src/remoteeditor/network/REPPacketSend.java @ 98:dd811063bbc5

*** empty log message ***
author pin
date Sat, 22 Dec 2007 15:07:03 +0900
parents b1710c49c883
children cf4df4f73605
line wrap: on
line source

package remoteeditor.network;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import remoteeditor.command.REPCommand;

public class REPPacketSend {
	SocketChannel socketchannel;
	
	public REPPacketSend(SocketChannel sc){
		socketchannel = sc;
	}
	
	public ByteBuffer pack(REPCommand command){
		//command.setString(command.string + ":temp:123456");	
    	System.out.println("send command: " + command.toString());
    	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
    	buffer.clear();  // position = 0 
    	buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
    	buffer.putInt(command.seq); buffer.putInt(command.lineno); 
    	buffer.putInt(command.string.length()*2); 
    	for(int i=0;i<command.string.length();i++) {
			buffer.putChar(command.string.charAt(i));
		}	
    	buffer.flip();    // limit = current position, position = 0
		return buffer;
	}
	
	public void send(REPCommand command){
		try {
			socketchannel.write(pack(command));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public SocketChannel getChannel() {
		return socketchannel;		
	}
}