import be.nikiroo.utils.CryptUtils;
import be.nikiroo.utils.IOUtils;
import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.Version;
import be.nikiroo.utils.serial.Exporter;
import be.nikiroo.utils.serial.Importer;
import be.nikiroo.utils.streams.BufferedOutputStream;
private Socket s;
private boolean server;
+ private Version clientVersion;
+ private Version serverVersion;
+
private CryptUtils crypt;
private Object lock = new Object();
* Method that will be called when an action is performed on either the
* client or server this {@link ConnectAction} represent.
*
+ * @param version
+ * the version on the other side of the communication (client or
+ * server)
+ *
* @throws Exception
* in case of I/O error
*/
- abstract protected void action() throws Exception;
+ abstract protected void action(Version version) throws Exception;
+
+ /**
+ * Method called when we negotiate the version with the client.
+ * <p>
+ * Thus, it is only called on the server.
+ * <p>
+ * Will return the actual server version by default.
+ *
+ * @param clientVersion
+ * the client version
+ *
+ * @return the version to send to the client
+ */
+ abstract protected Version negotiateVersion(Version clientVersion);
/**
* Handler called when an unexpected error occurs in the code.
* @param key
* an optional key to encrypt all the communications (if NULL,
* everything will be sent in clear text)
+ * @param version
+ * the client-or-server version (depending upon the boolean
+ * parameter <tt>server</tt>)
*/
- protected ConnectAction(Socket s, boolean server, String key) {
+ protected ConnectAction(Socket s, boolean server, String key,
+ Version version) {
this.s = s;
this.server = server;
if (key != null) {
crypt = new CryptUtils(key);
}
+
+ if (version == null) {
+ version = new Version();
+ }
+
+ if (server) {
+ serverVersion = version;
+ } else {
+ clientVersion = version;
+ }
+ }
+
+ /**
+ * The version of this client-or-server.
+ *
+ * @return the version
+ */
+ public Version getVersion() {
+ if (server) {
+ return serverVersion;
+ }
+
+ return clientVersion;
}
/**
try {
out = new BufferedOutputStream(s.getOutputStream());
try {
- action();
+ action(server ? serverVersion : clientVersion);
} finally {
out.close();
}
import javax.net.ssl.SSLException;
+import be.nikiroo.utils.Version;
+
/**
* Base class used for the client basic handling.
* <p>
protected ConnectAction action;
/**
- * Create a new {@link ConnectActionClient}.
+ * Create a new {@link ConnectActionClient}, using the current version of
+ * the program.
*
* @param host
* the host to bind to
* an optional key to encrypt all the communications (if NULL,
* everything will be sent in clear text)
*
+ *
* @throws IOException
* in case of I/O error
* @throws UnknownHostException
*/
public ConnectActionClient(String host, int port, String key)
throws IOException {
- this(new Socket(host, port), key);
+ this(host, port, key, Version.getCurrentVersion());
}
/**
* Create a new {@link ConnectActionClient}.
*
+ * @param host
+ * the host to bind to
+ * @param port
+ * the port to bind to
+ * @param key
+ * an optional key to encrypt all the communications (if NULL,
+ * everything will be sent in clear text)
+ * @param clientVersion
+ * the client version
+ *
+ *
+ * @throws IOException
+ * in case of I/O error
+ * @throws UnknownHostException
+ * if the host is not known
+ * @throws IllegalArgumentException
+ * if the port parameter is outside the specified range of valid
+ * port values, which is between 0 and 65535, inclusive
+ */
+ public ConnectActionClient(String host, int port, String key,
+ Version clientVersion) throws IOException {
+ this(new Socket(host, port), key, clientVersion);
+ }
+
+ /**
+ * Create a new {@link ConnectActionClient}, using the current version of
+ * the program.
+ *
* @param s
* the socket to bind to
* @param key
* everything will be sent in clear text)
*/
public ConnectActionClient(Socket s, String key) {
- action = new ConnectAction(s, false, key) {
+ this(s, key, Version.getCurrentVersion());
+ }
+
+ /**
+ * Create a new {@link ConnectActionClient}.
+ *
+ * @param s
+ * the socket to bind to
+ * @param key
+ * an optional key to encrypt all the communications (if NULL,
+ * everything will be sent in clear text)
+ * @param clientVersion
+ * the client version
+ */
+ public ConnectActionClient(Socket s, String key, Version clientVersion) {
+ action = new ConnectAction(s, false, key, clientVersion) {
@Override
- protected void action() throws Exception {
+ protected void action(Version serverVersion) throws Exception {
ConnectActionClient.this.clientHello();
- ConnectActionClient.this.action();
+ ConnectActionClient.this.action(serverVersion);
}
@Override
protected void onError(Exception e) {
ConnectActionClient.this.onError(e);
}
+
+ @Override
+ protected Version negotiateVersion(Version clientVersion) {
+ new Exception("Should never be called on a client")
+ .printStackTrace();
+ return null;
+ }
};
}
/**
* Method that will be called when an action is performed on the client.
*
+ * @param serverVersion
+ * the version of the server connected to this client
+ *
* @throws Exception
* in case of I/O error
*/
@SuppressWarnings("unused")
- public void action() throws Exception {
+ public void action(Version serverVersion) throws Exception {
}
/**
import javax.net.ssl.SSLException;
+import be.nikiroo.utils.Version;
+
/**
* Base class used for the server basic handling.
* <p>
protected ConnectAction action;
/**
- * Create a new {@link ConnectActionServer}.
+ * Create a new {@link ConnectActionServer}, using the current version.
*
* @param s
* the socket to bind to
* everything will be sent in clear text)
*/
public ConnectActionServer(Socket s, String key) {
- action = new ConnectAction(s, true, key) {
+ this(s, key, Version.getCurrentVersion());
+ }
+
+ /**
+ * Create a new {@link ConnectActionServer}.
+ *
+ * @param s
+ * the socket to bind to
+ * @param key
+ * an optional key to encrypt all the communications (if NULL,
+ * everything will be sent in clear text)
+ * @param serverVersion
+ * the version of this server,that will be sent to the client
+ */
+ public ConnectActionServer(Socket s, String key, Version serverVersion) {
+ action = new ConnectAction(s, true, key, serverVersion) {
@Override
- protected void action() throws Exception {
+ protected void action(Version clientVersion) throws Exception {
ConnectActionServer.this.serverHello();
- ConnectActionServer.this.action();
+ ConnectActionServer.this.action(clientVersion);
}
@Override
protected void onError(Exception e) {
ConnectActionServer.this.onError(e);
}
+
+ @Override
+ protected Version negotiateVersion(Version clientVersion) {
+ return ConnectActionServer.this.negotiateVersion(clientVersion);
+ }
};
}
/**
* Method that will be called when an action is performed on the server.
*
+ * @param clientVersion
+ * the version of the client connected to this server
+ *
* @throws Exception
* in case of I/O error
*/
@SuppressWarnings("unused")
- public void action() throws Exception {
+ public void action(Version clientVersion) throws Exception {
}
/**
*/
protected void onError(@SuppressWarnings("unused") Exception e) {
}
+
+ /**
+ * Method called when we negotiate the version with the client.
+ * <p>
+ * Will return the actual server version by default.
+ *
+ * @param clientVersion
+ * the client version
+ *
+ * @return the version to send to the client
+ */
+ protected Version negotiateVersion(
+ @SuppressWarnings("unused") Version clientVersion) {
+ return action.getVersion();
+ }
}
\ No newline at end of file
import be.nikiroo.utils.StringUtils;
import be.nikiroo.utils.TraceHandler;
+import be.nikiroo.utils.Version;
import be.nikiroo.utils.serial.Importer;
/**
// Bad impl, not up to date (should work, but not efficient)
return new ConnectActionServerString(s, key) {
@Override
- public void action() throws Exception {
- onClientContact();
+ public void action(Version clientVersion) throws Exception {
+ onClientContact(clientVersion);
final ConnectActionServerString bridge = this;
try {
new ConnectActionClientString(forwardToHost, forwardToPort,
forwardToKey) {
@Override
- public void action() throws Exception {
- onServerContact();
+ public void action(Version serverVersion)
+ throws Exception {
+ onServerContact(serverVersion);
for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
.rec()) {
/**
* This is the method that is called each time a client contact us.
*/
- protected void onClientContact() {
- getTraceHandler().trace(">>> CLIENT ");
+ protected void onClientContact(Version clientVersion) {
+ getTraceHandler().trace(">>> CLIENT " + clientVersion);
}
/**
* This is the method that is called each time a client contact us.
*/
- protected void onServerContact() {
- getTraceHandler().trace("<<< SERVER");
+ protected void onServerContact(Version serverVersion) {
+ getTraceHandler().trace("<<< SERVER " + serverVersion);
getTraceHandler().trace("");
}
import java.net.Socket;
import java.net.UnknownHostException;
+import be.nikiroo.utils.Version;
+
/**
* This class implements a simple server that can listen for connections and
* send/receive objects.
protected ConnectActionServer createConnectActionServer(Socket s) {
return new ConnectActionServerObject(s, key) {
@Override
- public void action() throws Exception {
+ public void action(Version clientVersion) throws Exception {
long id = getNextId();
try {
for (Object data = rec(); true; data = rec()) {
import java.net.Socket;
import java.net.UnknownHostException;
+import be.nikiroo.utils.Version;
+
/**
* This class implements a simple server that can listen for connections and
* send/receive Strings.
protected ConnectActionServer createConnectActionServer(Socket s) {
return new ConnectActionServerString(s, key) {
@Override
- public void action() throws Exception {
+ public void action(Version clientVersion) throws Exception {
long id = getNextId();
for (String data = rec(); data != null; data = rec()) {
String rep = null;
import java.net.URL;
-import be.nikiroo.utils.TraceHandler;
+import be.nikiroo.utils.Version;
import be.nikiroo.utils.serial.server.ConnectActionClientObject;
import be.nikiroo.utils.serial.server.ConnectActionClientString;
import be.nikiroo.utils.serial.server.ConnectActionServerObject;
try {
new ConnectActionClientString(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
rec[0] = "ok";
}
}.connect();
try {
new ConnectActionClientString(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
recd[0] = send("ping");
}
}.connect();
try {
new ConnectActionClientString(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
recd[0] = send("ping");
recd[1] = send("ping2");
}
try {
new ConnectActionClientString(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
for (int i = 0; i < 3; i++) {
recd[i] = send("" + i);
}
try {
new ConnectActionClientObject(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
rec[0] = true;
}
try {
new ConnectActionClientObject(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
recd[0] = send("ping");
}
}.connect();
try {
new ConnectActionClientObject(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
recd[0] = send("ping");
recd[1] = send("ping2");
}
try {
new ConnectActionClientObject(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
recd[0] = send(new Object[] {
"key",
new URL(
try {
new ConnectActionClientObject(null, port, key) {
@Override
- public void action() throws Exception {
+ public void action(Version version)
+ throws Exception {
for (int i = 0; i < 3; i++) {
recd[i] = send(i);
}