154
|
1 package test.channeltest;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.util.LinkedList;
|
|
5
|
|
6 import pathfinder.mergetest.channels.*;
|
|
7
|
|
8
|
|
9 public class testSeMa extends Thread{
|
|
10
|
|
11 int IP;
|
|
12 boolean running=true;
|
|
13 NetworkSimulator<String> ns;
|
|
14 LinkedList<ChannelSimulator<String>> channels;
|
|
15
|
|
16 public testSeMa(NetworkSimulator<String> _ns, String name, int ip){
|
|
17 super(name);
|
|
18 IP = ip;
|
|
19 ns = _ns;
|
|
20 channels = new LinkedList<ChannelSimulator<String>>();
|
|
21 }
|
|
22 public void init(){
|
|
23
|
|
24 }
|
|
25
|
|
26 public void run() {
|
|
27 SelectorSimulator<String> selector = new SelectorSimulator<String>();
|
|
28 ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector);
|
|
29 scs.bind(IP);
|
|
30 selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
|
|
31 ns.writeLog("SessionManager starts mainroutin.", 1);
|
|
32
|
|
33 /* Main Loop */
|
|
34 while(running){
|
|
35
|
|
36 try { selector.select(); }
|
|
37 catch (IOException e) { e.printStackTrace();}
|
|
38
|
|
39 for(SelectionKeySimulator<String> key : selector.selectedKeys()){
|
|
40
|
|
41 if(key.isAcceptable()){
|
|
42 ns.writeLog(this, "gets acceptable channel", 1);
|
|
43 ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel();
|
|
44 ChannelSimulator<String> channel = sc.accept();
|
|
45 selector.register(channel, SelectionKeySimulator.OP_READ);
|
|
46 ns.writeLog(this, "accepts a client.", 1);
|
|
47
|
|
48 }else if(key.isReadable()){
|
|
49 ns.writeLog(this, "gets readable channel", 1);
|
|
50 //SelectableChannelSimulator<String> channel = key.channel();
|
|
51 ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel();
|
|
52 String packet = channel.read();
|
|
53 ns.writeLog(this, "receives String==> `"+packet+"\'", 1);
|
|
54 channel.write("from SeMa"+this.getName()+": world");
|
|
55 }
|
|
56 }
|
|
57 }
|
|
58
|
|
59 }
|
|
60 }
|