Mercurial > hg > RemoteEditor > REPSessionManager
comparison rep/SessionManager.java @ 222:18d6a7140fa3
*** empty log message ***
author | pin |
---|---|
date | Sun, 31 Aug 2008 12:31:00 +0900 |
parents | 7649238aaf66 |
children | 6b0dd92b8e45 |
comparison
equal
deleted
inserted
replaced
221:6cd4aab9fea3 | 222:18d6a7140fa3 |
---|---|
2 | 2 |
3 import java.io.IOException; | 3 import java.io.IOException; |
4 import java.net.InetSocketAddress; | 4 import java.net.InetSocketAddress; |
5 import java.nio.channels.SelectableChannel; | 5 import java.nio.channels.SelectableChannel; |
6 import java.nio.channels.SelectionKey; | 6 import java.nio.channels.SelectionKey; |
7 import java.nio.channels.Selector; | |
8 | |
9 import java.util.Iterator; | 7 import java.util.Iterator; |
10 import java.util.LinkedList; | 8 import java.util.LinkedList; |
11 import java.util.List; | 9 import java.util.List; |
12 import java.util.concurrent.BlockingQueue; | 10 import java.util.concurrent.BlockingQueue; |
13 import java.util.concurrent.LinkedBlockingQueue; | 11 import java.util.concurrent.LinkedBlockingQueue; |
37 int lineno; // line number | 35 int lineno; // line number |
38 int textsize; // textsize : bytesize | 36 int textsize; // textsize : bytesize |
39 byte[] text; | 37 byte[] text; |
40 */ | 38 */ |
41 | 39 |
42 public class SessionManager implements ConnectionListener, REPActionListener{ | 40 public class SessionManager implements SessionManagerEventListener{ |
43 | 41 |
44 private LinkedList<Session> sessionList; | 42 private LinkedList<Session> sessionList; |
45 private SessionManagerGUI gui; | 43 private SessionManagerGUI gui; |
46 private REPSelector<REPCommand> selector; | 44 private REPSelector<REPCommand> selector; |
47 private SessionManagerList smList; | 45 private SessionManagerList smList; |
48 private String myHost; | 46 private String myHost; |
49 private boolean isMaster = true; | 47 private boolean isMaster = true; |
50 private List<Editor> editorList; | 48 private List<Editor> editorList; |
51 private String maxHost; | 49 private String maxHost; |
52 private boolean isSimulation; | |
53 private List<PacketSet> waitingCommandInMerge; | 50 private List<PacketSet> waitingCommandInMerge; |
54 private BlockingQueue<SessionManagerEvent> waitingQueue; | 51 private BlockingQueue<SessionManagerEvent> waitingQueue; |
55 private static int temp_port; | 52 private static int temp_port; |
56 private static int send_port; | 53 private static int send_port; |
57 static final int DEFAULT_PORT = 8766; | 54 static final int DEFAULT_PORT = 8766; |
87 if(selector.selectNow() > 0){ | 84 if(selector.selectNow() > 0){ |
88 select(); | 85 select(); |
89 } | 86 } |
90 continue; | 87 continue; |
91 } | 88 } |
92 System.out.println(selector.select()); | 89 int i = selector.select(); |
90 System.out.println(i); | |
93 select(); | 91 select(); |
94 } | 92 } |
95 } | 93 } |
96 | 94 |
97 private boolean checkSend() { | 95 private boolean checkSend() { |
468 sm.mainLoop(); | 466 sm.mainLoop(); |
469 | 467 |
470 } | 468 } |
471 | 469 |
472 private void startGUI(SessionManager sm) { | 470 private void startGUI(SessionManager sm) { |
473 gui = new SessionManagerGUI(this); | 471 gui = new SessionManagerGUI(); |
474 Thread th = new Thread( gui ); | 472 Thread th = new Thread( gui ); |
475 th.start(); | 473 th.start(); |
474 gui.addREPActionListener(this); | |
476 } | 475 } |
477 | 476 |
478 public void connectSession(String host) { | 477 public void connectSession(String host) { |
479 int port = DEFAULT_PORT; | 478 int port = DEFAULT_PORT; |
480 port = send_port; | 479 port = send_port; |
520 String host = null; | 519 String host = null; |
521 host = channel.socket().getLocalAddress().getHostName(); | 520 host = channel.socket().getLocalAddress().getHostName(); |
522 return host; | 521 return host; |
523 } | 522 } |
524 | 523 |
525 public void connectionOccured(ConnectionEvent event) { | 524 // public void connectionOccured(SessionManagerEvent event) { |
526 try { | 525 // try { |
527 waitingQueue.put(event); | 526 // waitingQueue.put(event); |
528 } catch (InterruptedException e) { | 527 // } catch (InterruptedException e) { |
529 } | 528 // } |
530 selector.wakeup(); | 529 // selector.wakeup(); |
531 } | 530 // } |
532 | 531 // |
533 public void selectOccured(REPActionEvent event) { | 532 // public void selectOccured(SessionManagerEvent event) { |
534 try { | 533 // try { |
535 waitingQueue.put(event); | 534 // waitingQueue.put(event); |
536 } catch (InterruptedException e) { | 535 // } catch (InterruptedException e) { |
537 e.printStackTrace(); | 536 // e.printStackTrace(); |
538 } | 537 // } |
539 selector.wakeup(); | 538 // selector.wakeup(); |
540 } | 539 // } |
541 | 540 |
542 public void selectSession(REPActionEvent event) { | 541 public void selectSession(SelectButtonEvent event) { |
543 REPSocketChannel<REPCommand> channel = event.getEditorChannel(); | 542 REPSocketChannel<REPCommand> channel = event.getEditorChannel(); |
544 int sid = event.getSID(); | 543 int sid = event.getSID(); |
545 Session session = getSession(sid); | 544 Session session = getSession(sid); |
546 selector.wakeup(); | 545 //selector.wakeup(); |
547 if(session.hasOwner()){ | 546 if(session.hasOwner()){ |
548 Editor editor = new Editor(channel); | 547 Editor editor = new Editor(channel); |
549 session.addEditor(new Editor(channel)); | 548 session.addEditor(new Editor(channel)); |
550 REPCommand sendCommand = new REPCommand(); | 549 REPCommand sendCommand = new REPCommand(); |
551 sendCommand.setCMD(REP.SMCMD_JOIN_ACK); | 550 sendCommand.setCMD(REP.SMCMD_JOIN_ACK); |
573 public void addWaitingCommand(PacketSet set) { | 572 public void addWaitingCommand(PacketSet set) { |
574 waitingCommandInMerge.add(set); | 573 waitingCommandInMerge.add(set); |
575 } | 574 } |
576 | 575 |
577 public List<Session> getSessionList() { | 576 public List<Session> getSessionList() { |
578 // TODO Auto-generated method stub | |
579 return sessionList; | 577 return sessionList; |
580 } | 578 } |
581 | 579 |
582 public List<Editor> getEditorList() { | 580 public List<Editor> getEditorList() { |
583 // TODO Auto-generated method stub | |
584 return editorList; | 581 return editorList; |
585 } | 582 } |
586 | 583 |
584 public void buttonPressed(SessionManagerEvent event) { | |
585 try { | |
586 waitingQueue.put(event); | |
587 } catch (InterruptedException e) {} | |
588 selector.wakeup(); | |
589 | |
590 } | |
591 | |
587 } | 592 } |