changeset 356:b18c24dcc5d2

Before chaning put/join scheme for ditributed select.
author kono
date Sat, 18 Oct 2008 19:01:40 +0900
parents 98607350f7d1
children 6ae9dcb30a12
files rep/EditorList.java rep/EditorPlus.java rep/Session.java rep/SessionManager.java rep/SessionManagerList.java
diffstat 5 files changed, 42 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/rep/EditorList.java	Fri Oct 17 22:11:34 2008 +0900
+++ b/rep/EditorList.java	Sat Oct 18 19:01:40 2008 +0900
@@ -2,111 +2,43 @@
 
 import java.util.LinkedList;
 
-import rep.channel.REPSocketChannel;
-
-public class EditorList {
-
-	private int numberOfEditor;
-	private LinkedList<Editor> editorList = new LinkedList<Editor>();
+public class EditorList extends LinkedList<Editor> {
 
-	public void sendJoinAck(REPCommand repCmd) {
-		Forwarder editor = null;
-		for(Forwarder editor2 : editorList){
-			error(String.valueOf(editor2.getEID()), String.valueOf(repCmd.eid));
-			if(editor2.getEID() == repCmd.eid){
-				editor = editor2;
-				break;
-			}
-		}
-		error(editor);
-		editor.getChannel().write(repCmd);
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	private int eid_root=0;
+	private LinkedList<Editor> waiting= new LinkedList<Editor>();
+
+
+	public void addWaitingEditor(Editor fw) {
+		waiting.add(fw);
 	}
 
 
-	public void sendJoinAck(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
-		REPCommand command = repCmd;
-		command.setCMD(REP.SMCMD_JOIN_ACK);
-
-		channel.write(command);
-	}
-
-	public int addEditor(SessionManager manager,REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
-		numberOfEditor++;
-		editorList.add(new Editor(manager,numberOfEditor, channel));
-		return numberOfEditor;
-	}
-
-	public void addEditor(SessionManager manager,REPSocketChannel<REPCommand> channel) {
-		editorList.add(new Editor(manager,0, channel));
-	}
-
-	public void setEID(REPCommand repCmd) {
-		for(Forwarder editor : editorList){
-			if(editor.getEID() == 0){
-				editor.setEID(repCmd.eid);
-				break;
-			}
+	public void assignSessionManagerIDtoWaitingSM(int eid) {
+		// 待っていたEditorにEditor IDを登録し,Session Manager List
+		// に登録する。この後、EditorのPUT/JOINに従って、ACKを送り、EID
+		// を確定する。
+		Editor waiter;
+		if ((waiter=waiting.poll())!=null) {
+			waiter.setEID(eid);
+			add(waiter);
+			return;
 		}
-	}
-	
-	private void error(Object obj) {
-		if(obj == null){
-			SessionManager.logger.writeLog("null!");
-		}
-	}
-	private void error(String str1, String str2){
-		if(str1.equals(str2)){
-			return;
-		}else{
-			SessionManager.logger.writeLog("Not equals! str1:str2");
-		}
+		assert false;
 	}
 
 
-	public void sendPutAck(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
-		channel.write(repCmd);
-	}
-	
-	public void send(REPSocketChannel<REPCommand> channel, REPCommand command){
-		channel.write(command);
-	}
-
-
-	public void setHost(String myHost) {
-		for(Editor editor : editorList) {
-			editor.setHost(myHost);
-		}
+	public int newEid() {
+		return ++eid_root;
 	}
 
 
-	public Forwarder getEditor(String hostport) {
-		for(Editor editor : editorList){
-			String[] splited = hostport.split(":");
-			SessionManager.logger.writeLog(
-					splited[0] + "," + editor.getHost());
-			if(splited[0].equals(editor.getHost())){
-				return editor;
-			}
-		}
-		return null;
+	public boolean waiting(Editor editor) {
+		return waiting.contains(editor);
 	}
 
 
-	public int addEditor(Editor editor) {
-		numberOfEditor++;
-		editorList.add(editor);
-		return numberOfEditor;
-	}
-
-
-	public Forwarder getEditor(REPSocketChannel<REPCommand> channel) {
-		Forwarder editor1 = null;
-		for(Forwarder editor: editorList){
-			if(channel == editor.getChannel()){
-				editor1 = editor;
-			}
-		}
-		return editor1;
-	}
-
 }
--- a/rep/EditorPlus.java	Fri Oct 17 22:11:34 2008 +0900
+++ b/rep/EditorPlus.java	Sat Oct 18 19:01:40 2008 +0900
@@ -4,7 +4,7 @@
 
 public class EditorPlus {
 
-	public int eid; // unique in a session, eid==0 master
+	public int eid; // globally unique
 	public int sid=-1; // globally unique
 	public String host;
 	public String file;
--- a/rep/Session.java	Fri Oct 17 22:11:34 2008 +0900
+++ b/rep/Session.java	Sat Oct 18 19:01:40 2008 +0900
@@ -10,7 +10,6 @@
 	private String sessionName;
 	private LinkedList<EditorPlus> editorList = new LinkedList<EditorPlus>();
 	private boolean isOwner = false;
-	private int eidSeed = 0;
 	private Forwarder firstForwarder;
 	
 	public Session(int sid, String name, Forwarder editor) {
@@ -65,17 +64,6 @@
 		return sessionName;
 	}
 	
-	public int newEid() {
-		int eid=0;
-		boolean flag = true;
-		while(flag) {
-			eid=eidSeed++;
-			for(EditorPlus e:editorList) {
-				if((flag = (eid==e.eid))) break;
-			}
-		}
-		return eid;
-	}
 	
 	public boolean deleteEditor(REPSocketChannel<REPCommand> channel) {
 		boolean flag = false;
--- a/rep/SessionManager.java	Fri Oct 17 22:11:34 2008 +0900
+++ b/rep/SessionManager.java	Sat Oct 18 19:01:40 2008 +0900
@@ -50,7 +50,7 @@
 	private SessionManagerGUI gui;
 	private REPSelector<REPCommand> selector;
 	SessionManagerList smList;
-	List<Editor> editorList;
+	EditorList editorList;
 	// editorList は、sessionList に入っているeditorとは別なeditorのlistらしい。
 	// private String maxHost;
 	private List<PacketSet> waitingCommandInMerge;
@@ -111,7 +111,7 @@
 
 		sessionList = new LinkedList<Session>();
 		smList = new SessionManagerList();
-		editorList = new LinkedList<Editor>();
+		editorList = new EditorList();
 		waitingCommandInMerge = new LinkedList<PacketSet>();
 		
 
@@ -361,11 +361,10 @@
 			return;
 		}
 		if (editor.hasSession()) return;
-		//REPSocketChannel<REPCommand> channel = editor.getChannel();
+		if (editorList.waiting(editor)) return;
 
-		// System.out.println("SessionManager.session.hasOnwer="+session.hasOwner());
 		if(session.hasOwner()){
-			editor.setEID(session.newEid());
+			editor.setEID(editorList.newEid());
 			editor.setSID(sid);
 			session.addForwarder(editor);
 			REPCommand sendCommand = new REPCommand();
--- a/rep/SessionManagerList.java	Fri Oct 17 22:11:34 2008 +0900
+++ b/rep/SessionManagerList.java	Sat Oct 18 19:01:40 2008 +0900
@@ -1,18 +1,18 @@
 package rep;
 
 import java.util.LinkedList;
-import rep.channel.REPSocketChannel;
+
+public class SessionManagerList extends LinkedList<Forwarder>{
 
-public class SessionManagerList {
-
-	private LinkedList<Forwarder> list = new LinkedList<Forwarder>();
-	private int mySMID;
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	private int mySMID=0;
+	private int smid_root=0;
 	private Forwarder parent=null;
 	private LinkedList<Forwarder> waiting= new LinkedList<Forwarder>();
 
-	public void add(Forwarder channel) {
-		list.add(channel);
-	}
 
 	public void setMaster(Forwarder f) {
 		this.parent = f;
@@ -23,7 +23,7 @@
 	}
 
 	public void sendToSlaves(REPCommand repCmd) {
-		for(Forwarder channel : list){
+		for(Forwarder channel : this){
 			channel.send(repCmd);
 		}
 	}
@@ -33,19 +33,13 @@
 	}
 
 	public int addNewSessionManager(Forwarder fw,REPCommand receivedCommand) {
-		list.add(fw);
-		int sid = list.size();
+		add(fw);
+		int sid = ++smid_root;
 		fw.setSID(sid);
 		fw.setName(receivedCommand.string);
 		return sid;
 	}
 
-	public boolean isSessionManagerChannel(REPSocketChannel<REPCommand> channel) {
-		for(Forwarder f : list){
-			if (f.channel==channel) return true;
-		}
-		return false;
-	}
 
 	public void setSessionManagerID(int sid) {
 		mySMID = sid;
@@ -67,7 +61,7 @@
 		Forwarder waiter;
 		if ((waiter=waiting.poll())!=null) {
 			waiter.setSID(sid);
-			list.add(waiter);
+			add(waiter);
 			return;
 		}
 		assert false;