forked from I2P_Developers/i2p.i2p
propagate from branch 'i2p.i2p.zzz.ipv6' (head 5c147c6e394fae03752dcf497923a90e3f2db529)
to branch 'i2p.i2p' (head 7af6987d5546664f76589afe0cbeeb780f4b5d58)
This commit is contained in:
@ -0,0 +1,67 @@
|
||||
package net.i2p.router.client;
|
||||
|
||||
import java.net.Socket;
|
||||
|
||||
import net.i2p.data.Destination;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.data.Lease;
|
||||
import net.i2p.data.LeaseSet;
|
||||
import net.i2p.data.i2cp.I2CPMessageException;
|
||||
import net.i2p.data.i2cp.I2CPMessageReader;
|
||||
import net.i2p.data.i2cp.RequestVariableLeaseSetMessage;
|
||||
import net.i2p.router.Job;
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
/**
|
||||
* For testing
|
||||
*
|
||||
* @since 0.9.8
|
||||
*/
|
||||
class LocalClientConnectionRunner extends ClientConnectionRunner {
|
||||
|
||||
/**
|
||||
* Create a new runner with the given queues
|
||||
*
|
||||
*/
|
||||
public LocalClientConnectionRunner(RouterContext context, ClientManager manager, Socket socket) {
|
||||
super(context, manager, socket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom listener
|
||||
*/
|
||||
@Override
|
||||
protected I2CPMessageReader.I2CPMessageEventListener createListener() {
|
||||
return new LocalClientMessageEventListener(_context, this, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just send the message directly,
|
||||
* don't instantiate a RequestLeaseSetJob
|
||||
*/
|
||||
@Override
|
||||
void requestLeaseSet(LeaseSet set, long expirationTime, Job onCreateJob, Job onFailedJob) {
|
||||
RequestVariableLeaseSetMessage msg = new RequestVariableLeaseSetMessage();
|
||||
msg.setSessionId(getSessionId());
|
||||
for (int i = 0; i < set.getLeaseCount(); i++) {
|
||||
Lease lease = set.getLease(i);
|
||||
msg.addEndpoint(lease);
|
||||
}
|
||||
try {
|
||||
doSend(msg);
|
||||
} catch (I2CPMessageException ime) {
|
||||
ime.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* So LocalClientMessageEventListener can lookup other local dests
|
||||
*/
|
||||
public Destination localLookup(Hash h) {
|
||||
for (Destination d : _manager.getRunnerDestinations()) {
|
||||
if (d.calculateHash().equals(h))
|
||||
return d;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package net.i2p.router.client;
|
||||
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
/**
|
||||
* For testing
|
||||
*
|
||||
* @since 0.9.8
|
||||
*/
|
||||
class LocalClientListenerRunner extends ClientListenerRunner {
|
||||
|
||||
public LocalClientListenerRunner(RouterContext context, ClientManager manager, int port) {
|
||||
super(context, manager, port);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runConnection(Socket socket) {
|
||||
ClientConnectionRunner runner = new LocalClientConnectionRunner(_context, _manager, socket);
|
||||
_manager.registerConnection(runner);
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package net.i2p.router.client;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* 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
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import net.i2p.data.Destination;
|
||||
import net.i2p.data.Payload;
|
||||
import net.i2p.data.i2cp.MessageId;
|
||||
import net.i2p.data.i2cp.MessageStatusMessage;
|
||||
import net.i2p.router.RouterContext;
|
||||
import net.i2p.util.I2PThread;
|
||||
|
||||
/**
|
||||
* For testing clients without a full router.
|
||||
* A complete router-side I2CP implementation, without requiring a router
|
||||
* or any RouterContext subsystems or threads.
|
||||
* Clients may connect only to other local clients.
|
||||
* Lookups and bw limit messages also supported.
|
||||
*
|
||||
* @since 0.9.8
|
||||
*/
|
||||
class LocalClientManager extends ClientManager {
|
||||
|
||||
/**
|
||||
* @param context stub, may be constructed with new RouterContext(null),
|
||||
* no initAll() necessary
|
||||
*/
|
||||
public LocalClientManager(RouterContext context, int port) {
|
||||
super(context, port);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startListeners() {
|
||||
_listener = new LocalClientListenerRunner(_ctx, this, _port);
|
||||
Thread t = new I2PThread(_listener, "ClientListener:" + _port, true);
|
||||
t.start();
|
||||
_isStarted = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local only
|
||||
* TODO: add simulated delay and random drops to test streaming.
|
||||
*
|
||||
* @param flags ignored for local
|
||||
*/
|
||||
@Override
|
||||
void distributeMessage(Destination fromDest, Destination toDest, Payload payload, MessageId msgId, long expiration, int flags) {
|
||||
// check if there is a runner for it
|
||||
ClientConnectionRunner sender = getRunner(fromDest);
|
||||
ClientConnectionRunner runner = getRunner(toDest);
|
||||
if (runner != null) {
|
||||
runner.receiveMessage(toDest, fromDest, payload);
|
||||
if (sender != null)
|
||||
sender.updateMessageDeliveryStatus(msgId, MessageStatusMessage.STATUS_SEND_SUCCESS_LOCAL);
|
||||
} else {
|
||||
// remote. ignore.
|
||||
System.out.println("Message " + msgId + " is targeting a REMOTE destination - DROPPED");
|
||||
if (sender != null)
|
||||
sender.updateMessageDeliveryStatus(msgId, MessageStatusMessage.STATUS_SEND_GUARANTEED_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
RouterContext ctx = new RouterContext(null);
|
||||
int port = ClientManagerFacadeImpl.DEFAULT_PORT;
|
||||
ClientManager mgr = new LocalClientManager(ctx, port);
|
||||
mgr.start();
|
||||
System.out.println("Listening on port " + port);
|
||||
try { Thread.sleep(5*60*1000); } catch (InterruptedException ie) {}
|
||||
System.out.println("Done listening on port " + port);
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package net.i2p.router.client;
|
||||
/*
|
||||
* free (adj.): unencumbered; not under the control of others
|
||||
* 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
|
||||
* your children, but it might. Use at your own risk.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import net.i2p.data.Destination;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.data.Lease;
|
||||
import net.i2p.data.LeaseSet;
|
||||
import net.i2p.data.TunnelId;
|
||||
import net.i2p.data.i2cp.BandwidthLimitsMessage;
|
||||
import net.i2p.data.i2cp.CreateLeaseSetMessage;
|
||||
import net.i2p.data.i2cp.DestLookupMessage;
|
||||
import net.i2p.data.i2cp.DestReplyMessage;
|
||||
import net.i2p.data.i2cp.GetBandwidthLimitsMessage;
|
||||
import net.i2p.data.i2cp.I2CPMessageException;
|
||||
import net.i2p.router.RouterContext;
|
||||
|
||||
/**
|
||||
* For testing
|
||||
*
|
||||
* @since 0.9.8
|
||||
*/
|
||||
class LocalClientMessageEventListener extends ClientMessageEventListener {
|
||||
|
||||
public LocalClientMessageEventListener(RouterContext context, ClientConnectionRunner runner, boolean enforceAuth) {
|
||||
super(context, runner, enforceAuth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Immediately send a fake leaseset
|
||||
*/
|
||||
@Override
|
||||
protected void startCreateSessionJob() {
|
||||
long exp = _context.clock().now() + 10*60*1000;
|
||||
LeaseSet ls = new LeaseSet();
|
||||
Lease lease = new Lease();
|
||||
lease.setGateway(Hash.FAKE_HASH);
|
||||
TunnelId id = new TunnelId(1);
|
||||
lease.setTunnelId(id);
|
||||
Date date = new Date(exp);
|
||||
lease.setEndDate(date);
|
||||
ls.addLease(lease);
|
||||
_runner.requestLeaseSet(ls, exp, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't tell the netdb or key manager
|
||||
*/
|
||||
@Override
|
||||
protected void handleCreateLeaseSet(CreateLeaseSetMessage message) {
|
||||
_runner.leaseSetCreated(message.getLeaseSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Look only in current local dests
|
||||
*/
|
||||
@Override
|
||||
protected void handleDestLookup(DestLookupMessage message) {
|
||||
Hash h = message.getHash();
|
||||
DestReplyMessage msg;
|
||||
Destination d = ((LocalClientConnectionRunner)_runner).localLookup(h);
|
||||
if (d != null)
|
||||
msg = new DestReplyMessage(d);
|
||||
else
|
||||
msg = new DestReplyMessage(h);
|
||||
try {
|
||||
_runner.doSend(msg);
|
||||
} catch (I2CPMessageException ime) {
|
||||
ime.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send dummy limits
|
||||
*/
|
||||
@Override
|
||||
protected void handleGetBWLimits(GetBandwidthLimitsMessage message) {
|
||||
int limit = 1024*1024;
|
||||
BandwidthLimitsMessage msg = new BandwidthLimitsMessage(limit, limit);
|
||||
try {
|
||||
_runner.doSend(msg);
|
||||
} catch (I2CPMessageException ime) {
|
||||
ime.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user