view rep/REPPacketSend.java @ 0:e41994ce73c7

*** empty log message ***
author pin
date Tue, 13 Feb 2007 04:43:30 +0900
parents
children 689622193437
line wrap: on
line source

package rep;

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

public class REPPacketSend {
	SocketChannel socketchannel;
	
	public REPPacketSend(SocketChannel sc){
		socketchannel = sc;
	}
	
	public ByteBuffer pack(REPCommand command){
    	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));
			//System.out.println(command.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}