4 import dolda.dolcon.protocol.*;
6 class HubManager implements NotifyListener {
7 private Set<HubListener> hubls = new HashSet<HubListener>();
8 private Set<HubListener> delayed = new HashSet<HubListener>();
9 private Map<Integer, Hub> hubs = new TreeMap<Integer, Hub>();
10 private String state = "none";
13 HubManager(Session sess) {
17 private int atoi(String a) {
18 return(Integer.parseInt(a));
21 private void addall(final HubListener ls) {
22 for(final Hub hub : hubs.values()) {
23 sess.dispatch(new Runnable() {
31 private void fetchhubs() {
37 Command cmd = new Command("lsnodes");
39 public void done(Response r) {
42 for(List<String> line : r.lines) {
43 Hub h = new Hub(atoi(line.get(0)), line.get(1));
45 h.numpeers = atoi(line.get(3));
46 h.state = new String[] {"syn", "hs", "est", "dead"}[atoi(line.get(4))];
50 synchronized(HubManager.this) {
52 HubManager.this.notifyAll();
53 for(HubListener ls : delayed) {
59 public void error(Exception e) {
60 synchronized(HubManager.this) {
65 sess.conn.qcmd(new Command("notify", "fn:act", "on"), cmd);
66 sess.conn.addNotifyListener(this);
69 public Collection<Hub> gethubs() throws InterruptedException {
72 while((state != "") && (state != "closed"))
75 Collection<Hub> ret = new LinkedList<Hub>();
77 for(Hub h : hubs.values())
83 public void addls(HubListener hl, boolean addexisting) {
98 public void rmls(HubListener hl) {
100 synchronized(hubls) {
102 if(hubls.isEmpty()) {
107 sess.conn.removeNotifyListener(this);
114 public void notified(Response resp) {
119 if(resp.code == 604) {
120 final Hub h = new Hub(atoi(resp.token(0, 0)), resp.token(0, 1));
124 sess.dispatch(new Runnable() {
126 synchronized(hubls) {
127 for(HubListener ls : hubls)
132 } else if(resp.code == 603) {
135 h = hubs.remove(atoi(resp.token(0, 0)));
137 sess.dispatch(new Runnable() {
139 synchronized(hubls) {
140 for(HubListener ls : hubls)
145 } else if(resp.code == 601) {
147 final String state = new String[] {"syn", "hs", "est", "dead"}[atoi(resp.token(0, 1))];
149 h = hubs.get(atoi(resp.token(0, 0)));
152 sess.dispatch(new Runnable() {
155 for(Hub.Listener ls : h.ls) {
161 } else if(resp.code == 602) {
163 final String name = resp.token(0, 1);
165 h = hubs.get(atoi(resp.token(0, 0)));
168 sess.dispatch(new Runnable() {
171 for(Hub.Listener ls : h.ls) {
177 } else if(resp.code == 605) {
179 final int np = atoi(resp.token(0, 1));
181 h = hubs.get(atoi(resp.token(0, 0)));
184 sess.dispatch(new Runnable() {
187 for(Hub.Listener ls : h.ls) {