view rep/SessionManager.java @ 0:e41994ce73c7

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

package rep;

// +-------+--------+--------+-------+--------+---------+------+
// | cmd   | session| editor | seqid | lineno | textsiz | text |
// |       | id     | id     |       |        |         |      |
// +-------+--------+--------+-------+--------+---------+------+
//  o-------header section (network order)-------------o
/*int cmd;	// command
int sid;	// session ID
int eid;	// editor ID
int seqno;	// Sequence number
int lineno;	// line number
int textsize;   // textsize
byte[] text;*/

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

public class SessionManager {
	
	
	private SessionList sessionlist;

	public SessionManager(int port) {
			//manager(port);
	}
	
	public void sessionManagerNet(int port) throws InterruptedException, IOException {
		/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
		Selector selector = Selector.open();
		ServerSocketChannel ssc = ServerSocketChannel.open();
		ssc.configureBlocking(false);
		ssc.socket().bind(new InetSocketAddress(port));
		ssc.register(selector, SelectionKey.OP_ACCEPT);
		sessionlist = new SessionList();
		
		while(true){
			selector.select();
			for(SelectionKey key : selector.selectedKeys()){
				if(key.isAcceptable()){
					SocketChannel channel = ssc.accept();
					if(channel == null) continue;
					channel.configureBlocking(false);
					channel.register(selector, SelectionKey.OP_READ);
					//sessionlist.add(channel);
					channel = null;
				}
				else if(key.isReadable()){
					SocketChannel channel = (SocketChannel)key.channel();
					REPPacketReceive repRec = new REPPacketReceive(channel);
					REPCommand repCom = repRec.unpack();
					manager(channel, repCom);
					//Charset charset = Charset.forName("US-ASCII");
					//ByteBuffer buffer = ByteBuffer.allocate(8192);
					//switch(channel.read(buffer)) {
					//case -1:
					//	channel.close();
					//	break;
					//case 0:
					//	continue;
					//	default:
					//		buffer.flip();
					//	System.out.println(charset.decode(buffer));
					//	channel.write(charset.encode("test"));
					//	break;
					//}
				}
			}
		}
	}
	private void manager(SocketChannel channel, REPCommand repCmd) {
		if(repCmd == null) return;
		switch(repCmd.cmd){
		case REP.SMCMD_JOIN:
			
			int eid = sessionlist.getNumberOfEditor();
			repCmd.setEID(eid);
			repCmd.setCMD(repCmd.cmd + 1);
			REPPacketSend repSend = new REPPacketSend(channel);
			repSend.send(repCmd);
			break;
		case REP.SMCMD_PUT:
			int sessionID = sessionlist.addSession(channel, repCmd.string);
			repCmd.setSID(sessionID);
			repCmd.setCMD(repCmd.cmd + 1);
			//repCmd.setSID(sessionlist.getSessionID(channel));
			REPPacketSend repSend2 = new REPPacketSend(channel);
			repSend2.send(repCmd);
			break;
		case REP.SMCMD_SELECT:
			sessionlist.addEditor(channel, repCmd.sid);
			repCmd.setCMD(repCmd.cmd + 1);
			REPPacketSend repSend3 = new REPPacketSend(channel);
			repSend3.send(repCmd);
		//case REP.REPCMD_INSERT:
		//	break;
		default:
			sessionlist.sendCmd(channel, repCmd);
			break;
			
		}
	}

	public static void main(String[] args) throws InterruptedException, IOException {
		int port;
		if(args.length == 1){
			port = Integer.parseInt(args[1]);
		}else{
		port = 8765;
		}
		SessionManager sm = new SessionManager(port);
		sm.sessionManagerNet(port);
	}

}