When i call:
OdetteFTPEntity oftp = session.acceptC onnection(trans port);
I recive a java.nio.Buffer OverflowExcepti on
OdetteFTPEntity oftp = session.acceptC onnection(trans port);
I recive a java.nio.Buffer OverflowExcepti on
package examples.odettej;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;
import org.fossilec.odettej.OdetteFTPEntity;
import org.fossilec.odettej.OdetteFTPException;
import org.fossilec.odettej.PeerAuthenticator;
import org.fossilec.odettej.Session;
import org.fossilec.odettej.service.TransferMode;
import org.fossilec.odettej.transport.Transport;
import org.fossilec.odettej.transport.tcp.TCPTransport;
public class OdetteServer implements Runnable {
private static Logger logger = Logger.getLogger(OdetteServer.class.getName());
private ServerSocketChannel channel = null;
private String baseDir;
private int listenPort;
private boolean shouldStop;
private Thread runner;
public OdetteServer(String baseDir, int listenPort) {
super();
this.baseDir = baseDir;
this.listenPort = listenPort;
}
/**
* @param args
*/
public static void main(String[] args) {
}
public void run() {
serverBind();
try {
handleConnections();
} catch (IOException e) {
logger.throwing("OdetteServer", "run", e);
}
}
private void serverBind() {
// create and bind (listen) to the specified port
try {
channel = ServerSocketChannel.open();
channel.socket().bind(new InetSocketAddress(listenPort));
logger.info("Odette FTP Server listening on port: " + listenPort);
} catch (IOException e) {
logger.throwing("OdetteServer", "bind", e);
}
}
public void handleConnections() throws IOException {
// turn off flag on start until method stop() is called
shouldStop = false;
// create selector
Selector selector = Selector.open();
try {
// set channel as non-blocking mode and register the selector
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_ACCEPT);
while (!shouldStop) {
// wait for any operation avaiable
selector.select();
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
// get avaiable operation
SelectionKey key = (SelectionKey) iterator.next();
// found an accept operation on the server socket
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key
.channel();
// create a client socket, also in non-blocking mode
SocketChannel client = server.accept();
logger.info("Receiving connection from: " + client.socket().getRemoteSocketAddress());
// if (client != null) {
// client.configureBlocking(false);
// // register client socket on the selector
// client.register(selector, SelectionKey.OP_READ);
// }
startSession(client);
}
}
}
} catch (Exception e) {
logger.throwing("OdetteServer", "handleConnections", e);
System.out.println(e.toString());
} finally {
// always close the selector
selector.close();
}
}
private void startSession(SocketChannel client) throws OdetteFTPException {
try {
client.socket().getChannel().configureBlocking(false);
}catch(Exception e) {
System.out.println(e.toString());
}
final Transport transport = new TCPTransport(client);
final Session session = Session.getInstance(new Properties(), TransferMode.BOTH);
final StringBuffer userBoxId = new StringBuffer();
session.setReceivingSupport(new ListeningSupport("C:/oftp/"));
session.acceptConnection(transport);
session.setAuthenticator(new PeerAuthenticator() {
public void init(Session context) { }
public void authenticate(String remoteUser, String remotePassword, String address) throws OdetteFTPException {
userBoxId.append(remoteUser);
String userInboxDir = baseDir + File.separator + remoteUser + File.separator + "incoming";
session.setReceivingSupport(new ListeningSupport(userInboxDir));
// no exception thrown means authentication succeed
}
});
// gives an independent lifecycle to this OFTP session (see the receiving support is already set)
Thread t = new Thread(new Runnable() {
public void run() {
try {
[B]OdetteFTPEntity oftp = session.acceptConnection(transport);[/B]
// create the OdetteApp instance here???
} catch (OdetteFTPException e) {
e.printStackTrace();
logger.throwing("OdetteServer", "startSession$Runnable...", e);
}
}
});
t.start();
}
public void stop() {
shouldStop = true;
}
}
public OdetteFTPEntity acceptConnection(Transport transport)
throws OdetteFTPException {
/* incoming network connection */
if ((state == null) || (state instanceof IdleState)) {
boolean stb = PreferencesUtil.getBoolean(properties, "odette.stb");
int sdeb = PreferencesUtil.getInt(properties, "odette.buffer-size");
service = Service.getInstance(transport, sdeb, stb);
setProperty("odette.called-address", transport.getLocalAddress());
setProperty("odette.calling-address", transport.getRemoteAddress());
/* start session phase */
changeState(ListenerState.class);
[B]state.startSession();[/B]
} else {
NotInIdleStateError();
}
return state;
}
-------------------------------------------------
package org.fossilec.odettej;
import org.fossilec.odettej.service.CommandExchangeBuffer;
import org.fossilec.odettej.service.CommandIdentifier;
import org.fossilec.odettej.service.EndSessionReason;
import org.fossilec.odettej.service.OdetteExchangeBuffer;
import org.fossilec.odettej.service.Service;
import org.fossilec.odettej.service.TransferMode;
import org.fossilec.odettej.transport.Transport;
abstract class AbstractState implements OdetteFTPEntity {
private Session context;
private PreferencesUtil prefs;
private boolean useStrictFormat;
protected AbstractState(Session context) {
super();
this.context = context;
prefs = new PreferencesUtil(context.getProperties());
useStrictFormat = prefs.isUsingStrictFormat();
}
protected abstract void listen() throws OdetteFTPException;
public Session getContext() {
return context;
}
public PreferencesUtil getPreferences() {
return prefs;
}
public Service getService() throws OdetteFTPException {
return context.getService();
}
public void resetCredits() {
String window = context.getProperty("odette.window-size");
setCredits(Integer.parseInt(window));
}
public void consumeCredit() {
int credits = getCredits();
credits--;
setCredits(credits);
}
protected abstract void setCredits(int windowSize);
public abstract int getCredits();
public boolean hasCredits() {
return (getCredits() > 0);
}
protected OdetteExchangeBuffer receive() throws OdetteFTPException {
OdetteExchangeBuffer exchangeBuffer = getService().receive(
useStrictFormat);
if (exchangeBuffer.getIdentifier() == CommandIdentifier.ESID) {
CommandExchangeBuffer esid = (CommandExchangeBuffer) exchangeBuffer;
String code = esid.getParameter("ESIDREAS");
EndSessionReason reason = EndSessionReason.parse(code);
if (reason != EndSessionReason.NORMAL_TERMINATION) {
abnormalRelease(reason);
}
}
return exchangeBuffer;
}
protected void send(OdetteExchangeBuffer oeb) throws OdetteFTPException {
Service service = getService();
service.send(oeb);
}
protected void disconnect() throws OdetteFTPException {
context.disconnect();
}
public boolean isConnected() throws OdetteFTPException {
Service service = getService();
Transport transport = service.getTransport();
return transport.isConnected();
}
public void release() throws OdetteFTPException {
protocolRelease(EndSessionReason.NORMAL_TERMINATION);
}
protected void abnormalRelease(EndSessionReason error)
throws OdetteFTPException {
protocolRelease(error);
endSessionError(error, "abnormal release");
}
protected void abnormalRelease(EndSessionReason error, String msg)
throws OdetteFTPException {
protocolRelease(error);
endSessionError(error, msg);
}
public void abort(EndSessionReason error) throws OdetteFTPException {
protocolRelease(error);
}
private void protocolRelease(EndSessionReason reason)
throws OdetteFTPException {
Service service = getService();
CommandExchangeBuffer esid;
if (reason == null) {
throw new IllegalArgumentException("Reason must not be null");
}
/*
* Sending End Session command.
*/
esid = CommandExchangeBuffer.endSession(reason);
send(esid);
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
service.disconnect();
context.changeState(IdleState.class);
}
private static void endSessionError(EndSessionReason error, String msg)
throws OdetteFTPException {
throw new EndSessionException(error, msg);
}
protected void requestAuthentication(String ssidcode, String ssidpswd,
String remoteAddress) throws OdetteFTPException {
Session context = getContext();
PeerAuthenticator auth = context.getAuthenticator();
if (auth != null) {
auth.authenticate(ssidcode, ssidpswd, remoteAddress);
}
}
protected static boolean windowSizeViolation(int size, Session session) {
return ((size <= 0) || (size > session.getMaxWindow()));
}
protected static boolean bufferSizeViolation(int size, Session session) {
return ((size < OdetteExchangeBuffer.MIN_OEB_LENGTH) || (size > session
.getMaxBuffer()));
}
protected static boolean valueOfYesNo(String parameter) {
return (parameter.equals("Y") ? true : false);
}
protected static void checkStartSession(Session context, int sdeb,
int window, boolean compression, TransferMode mode, boolean restart)
throws OdetteFTPException {
/* check exchange buffer size */
if (bufferSizeViolation(sdeb, context)) {
endSessionError(EndSessionReason.INVALID_COMMAND_DATA,
"Invalid exchange buffer size is being used: " + sdeb);
}
/* check window size */
if (windowSizeViolation(window, context)) {
endSessionError(EndSessionReason.INVALID_COMMAND_DATA,
"Invalid window size is being used: " + window);
}
/* check compression capability */
if ((!context.isCompressionCapable()) && (compression)) {
endSessionError(EndSessionReason.INCOMPATIBLE_MODE,
"Implementation doesn't support compression");
}
/* check transfer mode capability */
TransferMode capmode = context.getCapableTransferMode();
if ((capmode != TransferMode.BOTH) && (mode != capmode)) {
endSessionError(EndSessionReason.INCOMPATIBLE_MODE,
"Implementation doesn't support this mode: " + mode);
}
/* check restart capability */
if ((!context.isRestartCapable()) && (restart)) {
endSessionError(EndSessionReason.INCOMPATIBLE_MODE,
"Implementation doesn't support restart");
}
}
}
_______________________________________________
public interface OdetteFTPEntity {
public Session getContext();
[B]public void startSession() throws OdetteFTPException;[/B]
public void startFile(VirtualFile file, String destination,
String originator, String userData, String reserved)
throws OdetteFTPException;
public void dataRegime() throws OdetteFTPException;
public void closeFile(int recordCount, long unitCount)
throws OdetteFTPException;
public void changeDirection() throws OdetteFTPException;
public void endToEndResponse(String datasetName, Date fileDateTime,
String destination, String originator, String userData,
String reserved) throws OdetteFTPException;
public void release() throws OdetteFTPException;
public void abort(EndSessionReason error) throws OdetteFTPException;
public boolean isConnected() throws OdetteFTPException;
}
package examples.odettej;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;
import org.fossilec.odettej.OdetteFTPEntity;
import org.fossilec.odettej.OdetteFTPException;
import org.fossilec.odettej.PeerAuthenticator;
import org.fossilec.odettej.Session;
import org.fossilec.odettej.service.TransferMode;
import org.fossilec.odettej.transport.Transport;
import org.fossilec.odettej.transport.tcp.TCPTransport;
public class OdetteServer implements Runnable {
private static Logger logger = Logger.getLogger(OdetteServer.class.getName());
private ServerSocketChannel channel = null;
private String baseDir;
private int listenPort;
private boolean shouldStop;
private Thread runner;
public OdetteServer(String baseDir, int listenPort) {
super();
this.baseDir = baseDir;
this.listenPort = listenPort;
}
/**
* @param args
*/
public static void main(String[] args) {
}
public void run() {
serverBind();
try {
handleConnections();
} catch (IOException e) {
logger.throwing("OdetteServer", "run", e);
}
}
private void serverBind() {
// create and bind (listen) to the specified port
try {
channel = ServerSocketChannel.open();
channel.socket().bind(new InetSocketAddress(listenPort));
logger.info("Odette FTP Server listening on port: " + listenPort);
} catch (IOException e) {
logger.throwing("OdetteServer", "bind", e);
}
}
public void handleConnections() throws IOException {
// turn off flag on start until method stop() is called
shouldStop = false;
// create selector
Selector selector = Selector.open();
try {
// set channel as non-blocking mode and register the selector
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_ACCEPT);
while (!shouldStop) {
// wait for any operation avaiable
selector.select();
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
// get avaiable operation
SelectionKey key = (SelectionKey) iterator.next();
// found an accept operation on the server socket
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key
.channel();
// create a client socket, also in non-blocking mode
SocketChannel client = server.accept();
logger.info("Receiving connection from: " + client.socket().getRemoteSocketAddress());
// if (client != null) {
// client.configureBlocking(false);
// // register client socket on the selector
// client.register(selector, SelectionKey.OP_READ);
// }
startSession(client);
}
}
}
} catch (Exception e) {
logger.throwing("OdetteServer", "handleConnections", e);
System.out.println(e.toString());
} finally {
// always close the selector
selector.close();
}
}
private void startSession(SocketChannel client) throws OdetteFTPException {
try {
client.socket().getChannel().configureBlocking(false);
}catch(Exception e) {
System.out.println(e.toString());
}
final Transport transport = new TCPTransport(client);
final Session session = Session.getInstance(new Properties(), TransferMode.BOTH);
final StringBuffer userBoxId = new StringBuffer();
session.setReceivingSupport(new ListeningSupport("C:/oftp/"));
session.acceptConnection(transport);
session.setAuthenticator(new PeerAuthenticator() {
public void init(Session context) { }
public void authenticate(String remoteUser, String remotePassword, String address) throws OdetteFTPException {
userBoxId.append(remoteUser);
String userInboxDir = baseDir + File.separator + remoteUser + File.separator + "incoming";
session.setReceivingSupport(new ListeningSupport(userInboxDir));
// no exception thrown means authentication succeed
}
});
// gives an independent lifecycle to this OFTP session (see the receiving support is already set)
Thread t = new Thread(new Runnable() {
public void run() {
try {
[B]OdetteFTPEntity oftp = session.acceptConnection(transport);[/B]
// create the OdetteApp instance here???
} catch (OdetteFTPException e) {
e.printStackTrace();
logger.throwing("OdetteServer", "startSession$Runnable...", e);
}
}
});
t.start();
}
public void stop() {
shouldStop = true;
}
}
public OdetteFTPEntity acceptConnection(Transport transport)
throws OdetteFTPException {
/* incoming network connection */
if ((state == null) || (state instanceof IdleState)) {
boolean stb = PreferencesUtil.getBoolean(properties, "odette.stb");
int sdeb = PreferencesUtil.getInt(properties, "odette.buffer-size");
service = Service.getInstance(transport, sdeb, stb);
setProperty("odette.called-address", transport.getLocalAddress());
setProperty("odette.calling-address", transport.getRemoteAddress());
/* start session phase */
changeState(ListenerState.class);
[B]state.startSession();[/B]
} else {
NotInIdleStateError();
}
return state;
}
-------------------------------------------------
package org.fossilec.odettej;
import org.fossilec.odettej.service.CommandExchangeBuffer;
import org.fossilec.odettej.service.CommandIdentifier;
import org.fossilec.odettej.service.EndSessionReason;
import org.fossilec.odettej.service.OdetteExchangeBuffer;
import org.fossilec.odettej.service.Service;
import org.fossilec.odettej.service.TransferMode;
import org.fossilec.odettej.transport.Transport;
abstract class AbstractState implements OdetteFTPEntity {
private Session context;
private PreferencesUtil prefs;
private boolean useStrictFormat;
protected AbstractState(Session context) {
super();
this.context = context;
prefs = new PreferencesUtil(context.getProperties());
useStrictFormat = prefs.isUsingStrictFormat();
}
protected abstract void listen() throws OdetteFTPException;
public Session getContext() {
return context;
}
public PreferencesUtil getPreferences() {
return prefs;
}
public Service getService() throws OdetteFTPException {
return context.getService();
}
public void resetCredits() {
String window = context.getProperty("odette.window-size");
setCredits(Integer.parseInt(window));
}
public void consumeCredit() {
int credits = getCredits();
credits--;
setCredits(credits);
}
protected abstract void setCredits(int windowSize);
public abstract int getCredits();
public boolean hasCredits() {
return (getCredits() > 0);
}
protected OdetteExchangeBuffer receive() throws OdetteFTPException {
OdetteExchangeBuffer exchangeBuffer = getService().receive(
useStrictFormat);
if (exchangeBuffer.getIdentifier() == CommandIdentifier.ESID) {
CommandExchangeBuffer esid = (CommandExchangeBuffer) exchangeBuffer;
String code = esid.getParameter("ESIDREAS");
EndSessionReason reason = EndSessionReason.parse(code);
if (reason != EndSessionReason.NORMAL_TERMINATION) {
abnormalRelease(reason);
}
}
return exchangeBuffer;
}
protected void send(OdetteExchangeBuffer oeb) throws OdetteFTPException {
Service service = getService();
service.send(oeb);
}
protected void disconnect() throws OdetteFTPException {
context.disconnect();
}
public boolean isConnected() throws OdetteFTPException {
Service service = getService();
Transport transport = service.getTransport();
return transport.isConnected();
}
public void release() throws OdetteFTPException {
protocolRelease(EndSessionReason.NORMAL_TERMINATION);
}
protected void abnormalRelease(EndSessionReason error)
throws OdetteFTPException {
protocolRelease(error);
endSessionError(error, "abnormal release");
}
protected void abnormalRelease(EndSessionReason error, String msg)
throws OdetteFTPException {
protocolRelease(error);
endSessionError(error, msg);
}
public void abort(EndSessionReason error) throws OdetteFTPException {
protocolRelease(error);
}
private void protocolRelease(EndSessionReason reason)
throws OdetteFTPException {
Service service = getService();
CommandExchangeBuffer esid;
if (reason == null) {
throw new IllegalArgumentException("Reason must not be null");
}
/*
* Sending End Session command.
*/
esid = CommandExchangeBuffer.endSession(reason);
send(esid);
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
service.disconnect();
context.changeState(IdleState.class);
}
private static void endSessionError(EndSessionReason error, String msg)
throws OdetteFTPException {
throw new EndSessionException(error, msg);
}
protected void requestAuthentication(String ssidcode, String ssidpswd,
String remoteAddress) throws OdetteFTPException {
Session context = getContext();
PeerAuthenticator auth = context.getAuthenticator();
if (auth != null) {
auth.authenticate(ssidcode, ssidpswd, remoteAddress);
}
}
protected static boolean windowSizeViolation(int size, Session session) {
return ((size <= 0) || (size > session.getMaxWindow()));
}
protected static boolean bufferSizeViolation(int size, Session session) {
return ((size < OdetteExchangeBuffer.MIN_OEB_LENGTH) || (size > session
.getMaxBuffer()));
}
protected static boolean valueOfYesNo(String parameter) {
return (parameter.equals("Y") ? true : false);
}
protected static void checkStartSession(Session context, int sdeb,
int window, boolean compression, TransferMode mode, boolean restart)
throws OdetteFTPException {
/* check exchange buffer size */
if (bufferSizeViolation(sdeb, context)) {
endSessionError(EndSessionReason.INVALID_COMMAND_DATA,
"Invalid exchange buffer size is being used: " + sdeb);
}
/* check window size */
if (windowSizeViolation(window, context)) {
endSessionError(EndSessionReason.INVALID_COMMAND_DATA,
"Invalid window size is being used: " + window);
}
/* check compression capability */
if ((!context.isCompressionCapable()) && (compression)) {
endSessionError(EndSessionReason.INCOMPATIBLE_MODE,
"Implementation doesn't support compression");
}
/* check transfer mode capability */
TransferMode capmode = context.getCapableTransferMode();
if ((capmode != TransferMode.BOTH) && (mode != capmode)) {
endSessionError(EndSessionReason.INCOMPATIBLE_MODE,
"Implementation doesn't support this mode: " + mode);
}
/* check restart capability */
if ((!context.isRestartCapable()) && (restart)) {
endSessionError(EndSessionReason.INCOMPATIBLE_MODE,
"Implementation doesn't support restart");
}
}
}
_______________________________________________
public interface OdetteFTPEntity {
public Session getContext();
[B]public void startSession() throws OdetteFTPException;[/B]
public void startFile(VirtualFile file, String destination,
String originator, String userData, String reserved)
throws OdetteFTPException;
public void dataRegime() throws OdetteFTPException;
public void closeFile(int recordCount, long unitCount)
throws OdetteFTPException;
public void changeDirection() throws OdetteFTPException;
public void endToEndResponse(String datasetName, Date fileDateTime,
String destination, String originator, String userData,
String reserved) throws OdetteFTPException;
public void release() throws OdetteFTPException;
public void abort(EndSessionReason error) throws OdetteFTPException;
public boolean isConnected() throws OdetteFTPException;
}
final Transport transport = new TCPTransport(client);
final Session session = Session.getInstance(new Properties(), TransferMode.BOTH);
final StringBuffer userBoxId = new StringBuffer();
session.setReceivingSupport(new ListeningSupport("C:/oftp/"));
session.acceptConnection(transport);
/*Error happens here at next line*/
OdetteFTPEntity oftp = session.acceptConnection(transport);
public OdetteFTPEntity acceptConnection(Transport transport)
throws OdetteFTPException {
/* incoming network connection */
if ((state == null) || (state instanceof IdleState)) {
boolean stb = PreferencesUtil.getBoolean(properties, "odette.stb");
int sdeb = PreferencesUtil.getInt(properties, "odette.buffer-size");
service = Service.getInstance(transport, sdeb, stb);
setProperty("odette.called-address", transport.getLocalAddress());
setProperty("odette.calling-address",transport.getRemoteAddress());
/* start session phase */
changeState(ListenerState.class);
/*Error happens here at next line*/
state.startSession();
} else {
NotInIdleStateError();
}
return state;
}
public interface OdetteFTPEntity {
public Session getContext();
/*Error happens here in startSession Function*/
public void startSession() throws OdetteFTPException;
public void startFile(VirtualFile file, String destination,
String originator, String userData, String reserved)
throws OdetteFTPException;
public void dataRegime() throws OdetteFTPException;
public void closeFile(int recordCount, long unitCount)
throws OdetteFTPException;
public void changeDirection() throws OdetteFTPException;
public void endToEndResponse(String datasetName, Date fileDateTime,
String destination, String originator, String userData,
String reserved) throws OdetteFTPException;
public void release() throws OdetteFTPException;
public void abort(EndSessionReason error) throws OdetteFTPException;
public boolean isConnected() throws OdetteFTPException;
}
java.nio.BufferOverflowException at java.nio.DirectByteBuffer.put(DirectByteBuffer.jav a:279) at org.fossilec.odettej.service.StreamTransmissionSer vice.receiveStreamTransmissionBuffer(StreamTransmi ssionService.java:87) at org.fossilec.odettej.service.StreamTransmissionSer vice.receive(StreamTransmissionService.java:64) at org.fossilec.odettej.AbstractState.receive(Abstrac tState.java:85) at org.fossilec.odettej.ListenerState.negotiateStartS ession(ListenerState.java:548) at org.fossilec.odettej.ListenerState.startSession(Li stenerState.java:114) at org.fossilec.odettej.Session.acceptConnection(Sess ion.java:266) at examples.odettej.OdetteServer.startSession(OdetteS erver.java:140) at examples.odettej.OdetteServer.handleConnections(Od etteServer.java:114) at examples.odettej.OdetteServer.run(OdetteServer.jav a:54) at javaapplication1.Main.<init>(Main.java:27) at javaapplication1.Main.main(Main.java:37) java.lang.IllegalStateException: Not in Idle state at org.fossilec.odettej.Session.NotInIdleStateError(S ession.java:310) at org.fossilec.odettej.Session.acceptConnection(Sess ion.java:272) at examples.odettej.OdetteServer$2.run(OdetteServer.j ava:164) at java.lang.Thread.run(Thread.java:595)
Comment