2004-04-08 04:41:54 +00:00
|
|
|
package net.i2p.data.i2np;
|
|
|
|
/*
|
|
|
|
* free (adj.): unencumbered; not under the control of others
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
* Written by jrandom in 2003 and released into the public domain
|
|
|
|
* with no warranty of any kind, either expressed or implied.
|
|
|
|
* It probably won't make your computer catch on fire, or eat
|
2004-04-08 04:41:54 +00:00
|
|
|
* your children, but it might. Use at your own risk.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
2004-04-10 11:39:00 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2004-04-08 04:41:54 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
|
2004-05-17 03:38:53 +00:00
|
|
|
import net.i2p.I2PAppContext;
|
2004-04-08 04:41:54 +00:00
|
|
|
import net.i2p.data.DataFormatException;
|
|
|
|
import net.i2p.data.DataHelper;
|
|
|
|
import net.i2p.data.Hash;
|
|
|
|
import net.i2p.data.RouterInfo;
|
|
|
|
import net.i2p.util.Log;
|
|
|
|
|
|
|
|
/**
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
* Defines the message a router sends to another router in response to a
|
|
|
|
* search (DatabaseFindNearest or DatabaseLookup) when it doesn't have the value,
|
2004-04-08 04:41:54 +00:00
|
|
|
* specifying what routers it would search.
|
|
|
|
*
|
|
|
|
* @author jrandom
|
|
|
|
*/
|
|
|
|
public class DatabaseSearchReplyMessage extends I2NPMessageImpl {
|
|
|
|
private final static Log _log = new Log(DatabaseSearchReplyMessage.class);
|
|
|
|
public final static int MESSAGE_TYPE = 3;
|
|
|
|
private Hash _key;
|
|
|
|
private List _routerInfoStructures;
|
|
|
|
private Hash _from;
|
|
|
|
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
public DatabaseSearchReplyMessage(I2PAppContext context) {
|
|
|
|
super(context);
|
|
|
|
setSearchKey(null);
|
|
|
|
_routerInfoStructures = new ArrayList();
|
|
|
|
setFromHash(null);
|
2004-04-08 04:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the key being searched for
|
|
|
|
*/
|
|
|
|
public Hash getSearchKey() { return _key; }
|
|
|
|
public void setSearchKey(Hash key) { _key = key; }
|
|
|
|
|
|
|
|
public int getNumReplies() { return _routerInfoStructures.size(); }
|
|
|
|
public RouterInfo getReply(int index) { return (RouterInfo)_routerInfoStructures.get(index); }
|
|
|
|
public void addReply(RouterInfo info) { _routerInfoStructures.add(info); }
|
|
|
|
public void addReplies(Collection replies) { _routerInfoStructures.addAll(replies); }
|
|
|
|
|
|
|
|
public Hash getFromHash() { return _from; }
|
|
|
|
public void setFromHash(Hash from) { _from = from; }
|
|
|
|
|
|
|
|
public void readMessage(InputStream in, int type) throws I2NPMessageException, IOException {
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
if (type != MESSAGE_TYPE) throw new I2NPMessageException("Message type is incorrect for this message");
|
2004-04-08 04:41:54 +00:00
|
|
|
try {
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
_key = new Hash();
|
|
|
|
_key.readBytes(in);
|
|
|
|
|
|
|
|
int compressedLength = (int)DataHelper.readLong(in, 2);
|
|
|
|
byte compressedData[] = new byte[compressedLength];
|
|
|
|
int read = DataHelper.read(in, compressedData);
|
|
|
|
if (read != compressedLength)
|
|
|
|
throw new IOException("Not enough data to decompress");
|
|
|
|
byte decompressedData[] = DataHelper.decompress(compressedData);
|
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(decompressedData);
|
|
|
|
int num = (int)DataHelper.readLong(bais, 1);
|
|
|
|
_routerInfoStructures.clear();
|
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
RouterInfo info = new RouterInfo();
|
|
|
|
info.readBytes(bais);
|
|
|
|
addReply(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
_from = new Hash();
|
|
|
|
_from.readBytes(in);
|
2004-04-08 04:41:54 +00:00
|
|
|
} catch (DataFormatException dfe) {
|
|
|
|
throw new I2NPMessageException("Unable to load the message data", dfe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected byte[] writeMessage() throws I2NPMessageException, IOException {
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
if (_key == null)
|
|
|
|
throw new I2NPMessageException("Key in reply to not specified");
|
|
|
|
if (_routerInfoStructures == null)
|
|
|
|
throw new I2NPMessageException("RouterInfo replies are null");
|
|
|
|
if (_routerInfoStructures.size() <= 0)
|
|
|
|
throw new I2NPMessageException("No replies specified in SearchReply! Always include oneself!");
|
|
|
|
if (_from == null)
|
|
|
|
throw new I2NPMessageException("No 'from' address specified!");
|
|
|
|
|
2004-04-08 04:41:54 +00:00
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream(32);
|
|
|
|
try {
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
_key.writeBytes(os);
|
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
|
|
|
|
DataHelper.writeLong(baos, 1, _routerInfoStructures.size());
|
|
|
|
for (int i = 0; i < getNumReplies(); i++) {
|
|
|
|
RouterInfo info = getReply(i);
|
|
|
|
info.writeBytes(baos);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte compressed[] = DataHelper.compress(baos.toByteArray());
|
|
|
|
DataHelper.writeLong(os, 2, compressed.length);
|
|
|
|
os.write(compressed);
|
|
|
|
_from.writeBytes(os);
|
2004-04-08 04:41:54 +00:00
|
|
|
} catch (DataFormatException dfe) {
|
|
|
|
throw new I2NPMessageException("Error writing out the message data", dfe);
|
|
|
|
}
|
|
|
|
return os.toByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getType() { return MESSAGE_TYPE; }
|
|
|
|
|
|
|
|
public boolean equals(Object object) {
|
|
|
|
if ( (object != null) && (object instanceof DatabaseSearchReplyMessage) ) {
|
|
|
|
DatabaseSearchReplyMessage msg = (DatabaseSearchReplyMessage)object;
|
|
|
|
return DataHelper.eq(getSearchKey(),msg.getSearchKey()) &&
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
DataHelper.eq(getFromHash(),msg.getFromHash()) &&
|
|
|
|
DataHelper.eq(_routerInfoStructures,msg._routerInfoStructures);
|
2004-04-08 04:41:54 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
return DataHelper.hashCode(getSearchKey()) +
|
|
|
|
DataHelper.hashCode(getFromHash()) +
|
|
|
|
DataHelper.hashCode(_routerInfoStructures);
|
2004-04-08 04:41:54 +00:00
|
|
|
}
|
|
|
|
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
public String toString() {
|
2004-04-08 04:41:54 +00:00
|
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
buf.append("[DatabaseSearchReplyMessage: ");
|
|
|
|
buf.append("\n\tSearch Key: ").append(getSearchKey());
|
|
|
|
buf.append("\n\tReplies: # = ").append(getNumReplies());
|
big ol' update to strip out the singletons, replacing them with
a rooted app context. The core itself has its own I2PAppContext
(see its javadoc for, uh, docs), and the router extends that to
expose the router's singletons. The main point of this is to
make it so that we can run multiple routers in the same JVM, even
to allow different apps in the same JVM to switch singleton
implementations (e.g. run some routers with one set of profile
calculators, and other routers with a different one).
There is still some work to be done regarding the actual boot up
of multiple routers in a JVM, as well as their configuration,
though the plan is to have the RouterContext override the
I2PAppContext's getProperty/getPropertyNames methods to read from
a config file (seperate ones per context) instead of using the
System.getProperty that the base I2PAppContext uses.
Once the multi-router is working, i'll shim in a VMCommSystem
that doesn't depend upon sockets or threads to read/write (and
that uses configurable message send delays / disconnects / etc,
perhaps using data from the routerContext.getProperty to drive it).
I could hold off until the sim is all working, but there's a
truckload of changes in here and I hate dealing with conflicts ;)
Everything works - I've been running 'er for a while and kicked
the tires a bit, but if you see something amiss, please let me
know.
2004-04-24 11:54:35 +00:00
|
|
|
for (int i = 0; i < getNumReplies(); i++) {
|
|
|
|
buf.append("\n\t\tReply [").append(i).append("]: ").append(getReply(i));
|
|
|
|
}
|
2004-04-08 04:41:54 +00:00
|
|
|
buf.append("\n\tFrom: ").append(getFromHash());
|
|
|
|
buf.append("]");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|