2004-08-01 18:34:02 +00:00
|
|
|
package net.i2p.client.streaming;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InterruptedIOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.ConnectException;
|
|
|
|
import java.net.NoRouteToHostException;
|
2004-10-30 23:43:59 +00:00
|
|
|
import java.util.Properties;
|
2004-08-01 18:34:02 +00:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import net.i2p.I2PAppContext;
|
|
|
|
import net.i2p.I2PException;
|
|
|
|
import net.i2p.data.DataFormatException;
|
2008-07-16 13:42:54 +00:00
|
|
|
import net.i2p.data.Destination;
|
2005-07-11 23:06:23 +00:00
|
|
|
import net.i2p.util.I2PThread;
|
2004-08-01 18:34:02 +00:00
|
|
|
import net.i2p.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple streaming lib test app that connects to a given destination and sends
|
2008-10-11 10:28:31 +00:00
|
|
|
* it a particular amount of random data, then disconnects.
|
|
|
|
* @see #main(java.lang.String[])
|
2004-08-01 18:34:02 +00:00
|
|
|
*/
|
|
|
|
public class StreamSinkClient {
|
|
|
|
private Log _log;
|
|
|
|
private int _sendSize;
|
|
|
|
private int _writeDelay;
|
|
|
|
private String _peerDestFile;
|
2004-10-30 23:43:59 +00:00
|
|
|
private String _i2cpHost;
|
|
|
|
private int _i2cpPort;
|
2004-08-01 18:34:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the client but don't fire it up.
|
|
|
|
* @param sendSize how many KB to send
|
|
|
|
* @param writeDelayMs how long to wait between each .write (0 for no delay)
|
|
|
|
* @param serverDestFile file containing the StreamSinkServer's binary Destination
|
|
|
|
*/
|
|
|
|
public StreamSinkClient(int sendSize, int writeDelayMs, String serverDestFile) {
|
2004-10-30 23:43:59 +00:00
|
|
|
this(null, -1, sendSize, writeDelayMs, serverDestFile);
|
|
|
|
}
|
|
|
|
public StreamSinkClient(String i2cpHost, int i2cpPort, int sendSize, int writeDelayMs, String serverDestFile) {
|
|
|
|
_i2cpHost = i2cpHost;
|
|
|
|
_i2cpPort = i2cpPort;
|
2004-08-01 18:34:02 +00:00
|
|
|
_sendSize = sendSize;
|
|
|
|
_writeDelay = writeDelayMs;
|
|
|
|
_peerDestFile = serverDestFile;
|
|
|
|
_log = I2PAppContext.getGlobalContext().logManager().getLog(StreamSinkClient.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually connect and run the client - this call blocks until completion.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void runClient() {
|
2004-10-30 23:43:59 +00:00
|
|
|
I2PSocketManager mgr = null;
|
|
|
|
if (_i2cpHost != null)
|
|
|
|
mgr = I2PSocketManagerFactory.createManager(_i2cpHost, _i2cpPort, new Properties());
|
|
|
|
else
|
|
|
|
mgr = I2PSocketManagerFactory.createManager();
|
2004-08-01 18:34:02 +00:00
|
|
|
Destination peer = null;
|
|
|
|
FileInputStream fis = null;
|
|
|
|
try {
|
|
|
|
fis = new FileInputStream(_peerDestFile);
|
|
|
|
peer = new Destination();
|
|
|
|
peer.readBytes(fis);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.error("Error finding the peer destination to contact in " + _peerDestFile, ioe);
|
|
|
|
return;
|
|
|
|
} catch (DataFormatException dfe) {
|
|
|
|
_log.error("Peer destination is not valid in " + _peerDestFile, dfe);
|
|
|
|
return;
|
|
|
|
} finally {
|
2008-10-19 22:09:14 +00:00
|
|
|
if (fis != null) try { fis.close(); } catch (IOException ioe) {}
|
2004-08-01 18:34:02 +00:00
|
|
|
}
|
2005-07-11 23:06:23 +00:00
|
|
|
|
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug("Send " + _sendSize + "KB to " + peer.calculateHash().toBase64());
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
I2PSocket sock = mgr.connect(peer);
|
|
|
|
byte buf[] = new byte[Math.min(32*1024, _sendSize*1024)];
|
|
|
|
Random rand = new Random();
|
|
|
|
OutputStream out = sock.getOutputStream();
|
|
|
|
long beforeSending = System.currentTimeMillis();
|
|
|
|
for (int i = 0; (_sendSize < 0) || (i < _sendSize); i+= buf.length/1024) {
|
|
|
|
rand.nextBytes(buf);
|
|
|
|
out.write(buf);
|
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug("Wrote " + ((1+i*buf.length)/1024) + "/" + _sendSize + "KB");
|
|
|
|
if (_writeDelay > 0) {
|
|
|
|
try { Thread.sleep(_writeDelay); } catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sock.close();
|
|
|
|
long afterSending = System.currentTimeMillis();
|
2004-08-01 18:34:02 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
2005-07-11 23:06:23 +00:00
|
|
|
_log.debug("Sent " + _sendSize + "KB in " + (afterSending-beforeSending) + "ms");
|
|
|
|
} catch (InterruptedIOException iie) {
|
|
|
|
_log.error("Timeout connecting to the peer", iie);
|
|
|
|
//return;
|
|
|
|
} catch (NoRouteToHostException nrthe) {
|
|
|
|
_log.error("Unable to connect to the peer", nrthe);
|
|
|
|
//return;
|
|
|
|
} catch (ConnectException ce) {
|
|
|
|
_log.error("Connection already dropped", ce);
|
|
|
|
//return;
|
|
|
|
} catch (I2PException ie) {
|
|
|
|
_log.error("Error connecting to the peer", ie);
|
|
|
|
return;
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.error("IO error sending", ioe);
|
|
|
|
return;
|
|
|
|
}
|
2004-08-01 18:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-17 14:35:18 +00:00
|
|
|
* Fire up the client. <code>Usage: StreamSinkClient [i2cpHost i2cpPort] sendSizeKB writeDelayMs serverDestFile [concurrentSends]</code> <br>
|
2004-08-01 18:34:02 +00:00
|
|
|
* <ul>
|
2005-07-04 20:44:17 +00:00
|
|
|
* <li><b>sendSizeKB</b>: how many KB to send, or -1 for unlimited</li>
|
2004-08-01 18:34:02 +00:00
|
|
|
* <li><b>writeDelayMs</b>: how long to wait between each .write (0 for no delay)</li>
|
|
|
|
* <li><b>serverDestFile</b>: file containing the StreamSinkServer's binary Destination</li>
|
2005-07-11 23:06:23 +00:00
|
|
|
* <li><b>concurrentSends</b>: how many concurrent threads should send to the server at once</li>
|
2004-08-01 18:34:02 +00:00
|
|
|
* </ul>
|
2008-10-11 10:28:31 +00:00
|
|
|
* @param args [i2cpHost i2cpPort] sendSizeKB writeDelayMs serverDestFile [concurrentSends]
|
2004-08-01 18:34:02 +00:00
|
|
|
*/
|
|
|
|
public static void main(String args[]) {
|
2004-10-30 23:43:59 +00:00
|
|
|
StreamSinkClient client = null;
|
|
|
|
int sendSizeKB = -1;
|
|
|
|
int writeDelayMs = -1;
|
2005-07-11 23:06:23 +00:00
|
|
|
int concurrent = 1;
|
2004-10-30 23:43:59 +00:00
|
|
|
|
|
|
|
switch (args.length) {
|
2005-07-11 23:06:23 +00:00
|
|
|
case 3: // fall through
|
|
|
|
case 4:
|
2004-10-30 23:43:59 +00:00
|
|
|
try {
|
|
|
|
sendSizeKB = Integer.parseInt(args[0]);
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
System.err.println("Send size invalid [" + args[0] + "]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
writeDelayMs = Integer.parseInt(args[1]);
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
System.err.println("Write delay ms invalid [" + args[1] + "]");
|
|
|
|
return;
|
|
|
|
}
|
2005-07-11 23:06:23 +00:00
|
|
|
if (args.length == 4) {
|
|
|
|
try { concurrent = Integer.parseInt(args[3]); } catch (NumberFormatException nfe) {}
|
|
|
|
}
|
2004-10-30 23:43:59 +00:00
|
|
|
client = new StreamSinkClient(sendSizeKB, writeDelayMs, args[2]);
|
|
|
|
break;
|
2005-07-11 23:06:23 +00:00
|
|
|
case 5: // fall through
|
|
|
|
case 6:
|
2004-10-30 23:43:59 +00:00
|
|
|
try {
|
|
|
|
int port = Integer.parseInt(args[1]);
|
|
|
|
sendSizeKB = Integer.parseInt(args[2]);
|
|
|
|
writeDelayMs = Integer.parseInt(args[3]);
|
|
|
|
client = new StreamSinkClient(args[0], port, sendSizeKB, writeDelayMs, args[4]);
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
System.err.println("arg error");
|
|
|
|
}
|
2005-07-11 23:06:23 +00:00
|
|
|
if (args.length == 6) {
|
|
|
|
try { concurrent = Integer.parseInt(args[5]); } catch (NumberFormatException nfe) {}
|
|
|
|
}
|
2004-10-30 23:43:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-07-11 23:06:23 +00:00
|
|
|
System.out.println("Usage: StreamSinkClient [i2cpHost i2cpPort] sendSizeKB writeDelayMs serverDestFile [concurrentSends]");
|
|
|
|
}
|
|
|
|
if (client != null) {
|
|
|
|
for (int i = 0; i < concurrent; i++)
|
|
|
|
new I2PThread(new Runner(client), "Client " + i).start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class Runner implements Runnable {
|
|
|
|
private StreamSinkClient _client;
|
|
|
|
public Runner(StreamSinkClient client) {
|
|
|
|
_client = client;
|
|
|
|
}
|
|
|
|
public void run() {
|
|
|
|
_client.runClient();
|
2004-08-01 18:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|