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.*; |
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); |
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){ |
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 |
} |
} |