changeset 346:edf0e0a5ffc8

*** empty log message ***
author kono
date Tue, 14 Oct 2008 09:33:31 +0900
parents a092a8fe9d97
children 86935b872385
files rep/SessionManager.java test/XMLTest.java
diffstat 2 files changed, 35 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/rep/SessionManager.java	Tue Oct 14 09:00:04 2008 +0900
+++ b/rep/SessionManager.java	Tue Oct 14 09:33:31 2008 +0900
@@ -11,6 +11,8 @@
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
+import org.xml.sax.SAXException;
+
 
 import rep.channel.REPLogger;
 import rep.channel.REPServerSocketChannel;
@@ -61,6 +63,7 @@
 	private int parent_port;
 	static final int DEFAULT_PORT = 8766;
 	private static final int packetLimit = 200;
+	private SessionXMLDecoder decoder = new SessionXMLDecoder();
 
 	public static void main(String[] args) throws InterruptedException, IOException {
 		
@@ -157,7 +160,7 @@
 	 * @return true if there is a processed waiting command
 	 * @throws IOException
 	 */
-	private void checkWaitingCommandInMerge() throws IOException {
+	private void checkWaitingCommandInMerge() {
 		List<PacketSet> w = waitingCommandInMerge;
 		waitingCommandInMerge = new LinkedList<PacketSet>();
 		for(PacketSet p: w) {
@@ -165,11 +168,26 @@
 			if(e!=null &&e.isMerging()) { // still merging do nothing
 				waitingCommandInMerge.add(p);
 			} else {
-				manage(p.channel, p.command);		
+				try {
+					manage(p.channel, p.command);
+				} catch (IOException e1) {
+					close(p.channel);
+				} catch (SAXException e1) {
+					close(p.channel);
+				}		
 			}
 		}
 	}
 	
+	private void close(REPSocketChannel<REPCommand> channel) {
+		REPSelectionKey<REPCommand>key = channel.keyFor1(selector);
+		REPHandler handler = (REPHandler)key.attachment();
+		key.cancel();
+		handler.cancel(channel);
+		// we have to remove session/enditor
+	}
+
+
 	public boolean hasWaitingCommand(REPSocketChannel<REPCommand>c) {
 		for(PacketSet p:waitingCommandInMerge) {
 			if (p.channel==c) {
@@ -193,12 +211,9 @@
 				REPHandler handler = (REPHandler)(key.attachment());
 				try {
 					handler.handle(key);
-				} catch (ClosedChannelException x) {
+				} catch (Exception x) {
 					key.cancel();
 					handler.cancel(key.channel1());
-				} catch (IOException x) {
-					key.cancel();
-					handler.cancel( key.channel1());
 				}
 			}
 		}
@@ -213,7 +228,7 @@
 		channel.register(selector, ops, handler);
 	}
 
-	public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) throws IOException {
+	public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) throws IOException, SAXException {
 		if (sessionManagerCommand(channel, receivedCommand)) return;
 		Session s = getSession(receivedCommand.sid);
 		Editor e = s.getEditor(channel);
@@ -223,7 +238,7 @@
 
 	private boolean sessionManagerCommand(REPSocketChannel<REPCommand> channel,
 			REPCommand receivedCommand) throws ClosedChannelException,
-			IOException {
+			IOException, SAXException {
 		switch(receivedCommand.cmd){
 
 		// Session Manager Command
@@ -341,7 +356,6 @@
 			smList.add(channel);
 
 			//XMLからSessionListオブジェクトを生成する。
-			SessionXMLDecoder decoder = new SessionXMLDecoder();
 			SessionList receivedSessionList = decoder.decode(receivedCommand.string);
 
 			//myHost を設定。
@@ -376,8 +390,7 @@
 		case SMCMD_SM_JOIN_ACK:
 
 			//XMLからSessionListオブジェクトを生成。
-			SessionXMLDecoder decoder2 = new SessionXMLDecoder();
-			SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
+			SessionList receivedSessionList2 = decoder.decode(receivedCommand.string);
 
 			//maxHostを決定。
 			if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
@@ -391,8 +404,7 @@
 
 		case SMCMD_UPDATE:
 		{
-			SessionXMLDecoder decoder3 = new SessionXMLDecoder();
-			SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
+			SessionList receivedSessionList3 = decoder.decode(receivedCommand.string);
 
 			//UPDATEコマンドにより送られてきたSessionの情報を追加する
 			LinkedList<Session> list = receivedSessionList3.getList();
--- a/test/XMLTest.java	Tue Oct 14 09:00:04 2008 +0900
+++ b/test/XMLTest.java	Tue Oct 14 09:33:31 2008 +0900
@@ -1,5 +1,9 @@
 package test;
 
+import java.io.IOException;
+
+import org.xml.sax.SAXException;
+
 import rep.Editor;
 import rep.Session;
 import rep.SessionList;
@@ -38,7 +42,12 @@
 		SessionXMLDecoder decoder = new SessionXMLDecoder();
 		String t1 = encoder.sessionListToXML();
 		System.out.println(t1);
-		SessionList sessionlist2 = decoder.decode(t1);
+		SessionList sessionlist2;
+		try {
+			sessionlist2 = decoder.decode(t1);
+		} catch (Exception e) {
+			return;
+		}
 		
 		/*** SessionList から XML へ ***/
 		//SessionXMLEncoder encoder2 = new SessionXMLEncoder(sessionlist2.getList());