/[gzz]/gzz/lava/gzz/modules/gispmap/GispP2PMap.java
ViewVC logotype

Diff of /gzz/lava/gzz/modules/gispmap/GispP2PMap.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by tjl, Fri Feb 21 20:02:51 2003 UTC revision 1.6 by benja, Sat Feb 22 10:52:56 2003 UTC
# Line 62  package gzz.modules.gispmap; Line 62  package gzz.modules.gispmap;
62  import gzz.storm.*;  import gzz.storm.*;
63  import gzz.storm.impl.*;  import gzz.storm.impl.*;
64  import gzz.util.HexUtil;  import gzz.util.HexUtil;
65    import gzz.util.ByteArrayKey;
66    
67  import java.io.*;  import java.io.*;
68  import java.util.*;  import java.util.*;
# Line 79  public class GispP2PMap implements P2PPo Line 80  public class GispP2PMap implements P2PPo
80      static public boolean dbg = false;      static public boolean dbg = false;
81      static private void pa(String s) { System.out.println(s); }      static private void pa(String s) { System.out.println(s); }
82    
83      GISPUDP gisp;      protected GISP gisp;
84        protected Map cache = new HashMap();
85    
86      public GispP2PMap() {      public GispP2PMap(String seedAddress) throws Exception {
87          int offs = (int)(2000 * new java.util.Random().nextDouble());          int offs = (int)(2000 * new java.util.Random().nextDouble());
88          gisp = new GISPUDP(12415 + offs);          GISPUDP gisp = new GISPUDP(12415 + offs);
89          gisp.start(new String[0]);          this.gisp = gisp;
90            if(seedAddress != null) {
91                String[] seeds = new String[] { seedAddress };
92                gisp.addSeedAddresses(seeds);
93            }
94            gisp.start(new String[] {"strength_min=1"});
95        }
96    
97        public GispP2PMap() throws Exception {
98            this(null);
99      }      }
100    
101        public GISP getGisp() { return gisp; }
102    
103      public void put(byte[] key, byte[] value) throws IOException {      public void put(byte[] key, byte[] value) throws IOException {
104          // inelegant...          // inelegant...
105          String keyString = HexUtil.byteArrToHex(key);          String keyString = HexUtil.byteArrToHex(key);
# Line 94  public class GispP2PMap implements P2PPo Line 107  public class GispP2PMap implements P2PPo
107                    
108          gisp.insert(keyString, valString);          gisp.insert(keyString, valString);
109    
110            cache.remove(new ByteArrayKey(key));
111    
112          // XXX keep alive after ttl...          // XXX keep alive after ttl...
113      }      }
114    
115      public Collector get(byte[] key) throws IOException {      public Collector get(byte[] key) throws IOException {
116          if(dbg) pa("get collector");          if(dbg) pa("get collector");
117          return new MyCollector(key);          ByteArrayKey bak = new ByteArrayKey(key);
118            Collector c = (Collector)cache.get(bak);
119            if(c == null || c.getAge() > 2 * 60 * 1000) {
120                c = new MyCollector(key);
121                cache.put(bak, c);
122            }
123            return c;
124      }      }
125    
126      protected class MyCollector extends AsyncSetCollector      protected class MyCollector extends AsyncSetCollector implements ResultListener {
         implements ResultListener, Runnable {  
127    
128          protected MyCollector(byte[] key) {          protected MyCollector(byte[] key) {
129              String k = HexUtil.byteArrToHex(key);              String k = HexUtil.byteArrToHex(key);
130              gisp.query(k, this);              gisp.query(k, this, 3000);
             new Thread(this).start();  
131          }          }
132    
133          public void stringResult(String data){          public void stringResult(String data){
# Line 123  public class GispP2PMap implements P2PPo Line 142  public class GispP2PMap implements P2PPo
142          public void xmlResult(byte[] data){          public void xmlResult(byte[] data){
143          }                }      
144    
145          public void run() {          public void queryExpired() {
             if(dbg) pa("start sleeping");  
             try {  
                 Thread.sleep(3000);  
                 if(dbg) pa("normal wakeup");  
             } catch(InterruptedException e) {  
                 if(dbg) pa("interrupted");  
             }  
   
146              finish(true);              finish(true);
             if(dbg) pa("finish");  
147          }          }
148      }      }
149  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26