Files
i2p.i2p/apps/sam/java/src/net/i2p/sam/SAMv3RawSession.java
mkvore-commit a4d16af95d SAM version 3 :
- Raw and Datagram sessions implemented
      - option "SILENT=true" added to the stream protocol
      - java 6 warnings removed
   ministreaming :
      - java 6 warnings removed
   ministreaming and streaming :
      -  added functions : 
      	I2PServerSocket.waitIncoming(long timeout)
      	I2PServerSocket.accept(boolean block)
2009-04-02 08:22:31 +00:00

89 lines
2.3 KiB
Java

/**
*
*/
package net.i2p.sam;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.Properties;
import net.i2p.client.I2PSessionException;
import net.i2p.data.DataFormatException;
import net.i2p.util.Log;
/**
* @author MKVore
*
*/
public class SAMv3RawSession extends SAMRawSession implements SAMv3Handler.Session, SAMRawReceiver {
String nick = null ;
SAMv3Handler handler = null ;
SAMv3Handler.DatagramServer server ;
private final static Log _log = new Log ( SAMv3DatagramSession.class );
SocketAddress clientAddress = null ;
public String getNick() { return nick; }
/**
* @param nick nickname of the session
* @param server DatagramServer used for communication with the client
* @throws IOException
* @throws DataFormatException
* @throws I2PSessionException
*/
public SAMv3RawSession(String nick)
throws IOException, DataFormatException, I2PSessionException {
super(SAMv3Handler.sSessionsHash.get(nick).getDest(),
SAMv3Handler.sSessionsHash.get(nick).getProps(),
SAMv3Handler.sSessionsHash.get(nick).getHandler()
);
this.nick = nick ;
this.recv = this ;
this.server = SAMv3Handler.DatagramServer.getInstance() ;
SAMv3Handler.SessionRecord rec = SAMv3Handler.sSessionsHash.get(nick);
if ( rec==null ) throw new InterruptedIOException() ;
this.handler = rec.getHandler();
Properties props = rec.getProps();
String portStr = props.getProperty("PORT") ;
if ( portStr==null ) {
_log.debug("receiver port not specified. Current socket will be used.");
}
else {
int port = Integer.parseInt(portStr);
String host = props.getProperty("HOST");
if ( host==null ) {
_log.debug("no host specified. Take from the client socket");
host = rec.getHandler().getClientIP();
}
this.clientAddress = new InetSocketAddress(host,port);
}
}
public void receiveRawBytes(byte[] data) throws IOException {
if (this.clientAddress==null) {
this.handler.receiveRawBytes(data);
} else {
ByteBuffer msgBuf = ByteBuffer.allocate(data.length);
msgBuf.put(data);
msgBuf.flip();
this.server.send(this.clientAddress, msgBuf);
}
}
public void stopRawReceiving() {}
}