annotate src/pathfinder/BlockingQ/SeMaSimulator.java @ 84:5ac3df98f780

merger simulator using BlockingQ
author kent
date Mon, 12 Nov 2007 17:57:38 +0900
parents
children 0a483aa8cf71
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
1 package pathfinder.BlockingQ;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
2
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
3 import java.util.ArrayList;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
4 import java.util.List;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
5
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
6 public class SeMaSimulator<P> extends Thread {
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
7 private int MAX_PACKET;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
8 private int MAX_CLIENT;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
9 private boolean running=true;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
10 private NetworkSimulator<P> ns;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
11 private List<ChannelSimulator<P>> csList;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
12
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
13 public SeMaSimulator(NetworkSimulator<P> _ns, int max_client, int max_packet){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
14 ns = _ns;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
15 MAX_CLIENT = max_client;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
16 MAX_PACKET = max_packet;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
17 csList = new ArrayList<ChannelSimulator<P>>();
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
18 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
19 public SeMaSimulator(NetworkSimulator<P> _ns){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
20 this(_ns, 2, 0);
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
21 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
22
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
23 synchronized public void finish(){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
24 synchronized(ns){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
25 running = false;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
26 ns.notify();
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
27 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
28 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
29
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
30 /**
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
31 * Check whether the NetworkSimulator hold waiting connections.
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
32 */
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
33 private void checkAccept(){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
34 ChannelSimulator<P> cs;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
35 while((cs=ns.accept())!=null){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
36 csList.add(cs);
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
37 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
38 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
39
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
40 public void run(){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
41 int i=0;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
42 int count=0;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
43 P packet;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
44
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
45 while(csList.size()<MAX_CLIENT){ checkAccept(); Thread.yield(); }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
46 System.out.println("SessionManager start.");
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
47
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
48 /* Main Loop */
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
49 ChannelSimulator<P> cs = csList.get(i);
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
50 while(running
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
51 && (MAX_PACKET==0 || count<MAX_PACKET)){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
52 synchronized(ns){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
53 int prev_i=i;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
54 while((packet=cs.poll())==null && running){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
55 i = (i+1)%csList.size(); // i++
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
56 cs = csList.get(i); // 次のChennelをゲット
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
57 if(i==prev_i) try { ns.wait(); } catch (InterruptedException e) { e.printStackTrace(); }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
58 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
59 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
60 if(!running) break;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
61
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
62 System.out.println("SeMa pass packet to "+i+":>> "+packet.toString());
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
63 i = (i+1)%csList.size(); // i++
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
64 cs = csList.get(i); // 次のChennelをゲット
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
65
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
66 // readできていたならそれを書き込む
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
67 try {
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
68 cs.write(packet);
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
69 } catch (InterruptedException e) {
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
70 System.out.println("SeMa cannot write!!");
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
71 e.printStackTrace();
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
72 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
73 count++;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
74 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
75 /*
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
76 ChannelSimulator<P> cs = csList.get(i);
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
77 while(running
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
78 && MAX_PACKET==0 || count<MAX_PACKET){
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
79 packet=cs.poll(); // [i]からread
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
80 //if(packet!=null) System.out.println("SeMa catch packet to "+i+":>> "+packet.toString());
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
81 i = (i+1)%csList.size(); // i++
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
82 cs = csList.get(i); // 次のChennelをゲット
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
83 if (packet!=null) {
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
84 System.out.println("SeMa pass packet to "+i+":>> "+packet.toString());
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
85 cs.write(packet); // readできていたならそれを書き込む
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
86 count++;
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
87 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
88 //if (i==0) checkAccept(); //全部回ったらaccept待ちをチェック
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
89 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
90 */
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
91 System.out.println("SessionManager finish.");
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
92 }
5ac3df98f780 merger simulator using BlockingQ
kent
parents:
diff changeset
93 }