import java.net.Socket;
import java.net.UnknownHostException;
+import javax.net.ssl.SSLException;
+
/**
* Base class used for the client basic handling.
* <p>
action = new ConnectAction(s, false, key) {
@Override
protected void action() throws Exception {
+ ConnectActionClient.this.clientHello();
ConnectActionClient.this.action();
}
};
}
+ /**
+ * Send the HELLO message (send a String "HELLO" to the server, to check I/O
+ * and encryption modes).
+ * <p>
+ * Will automatically handle the answer (the server must answer "HELLO" in
+ * kind).
+ *
+ * @throws IOException
+ * in case of I/O error
+ * @throws SSLException
+ * in case of encryption error
+ */
+ protected void clientHello() throws IOException {
+ String HELLO = action.sendString("HELLO");
+ if (!"HELLO".equals(HELLO)) {
+ throw new SSLException("Server did not accept the encryption key");
+ }
+ }
+
/**
* Actually start the process and call the action (synchronous).
*/
import java.net.Socket;
import java.net.UnknownHostException;
-import be.nikiroo.utils.Version;
-
/**
* Class used for the client basic handling.
* <p>
*/
public class ConnectActionClientObject extends ConnectActionClient {
/**
- * Create a new {@link ConnectActionClientObject} with the current
- * application version (see {@link Version#getCurrentVersion()}) as the
- * client version.
+ * Create a new {@link ConnectActionClientObject} .
*
* @param s
* the socket to bind to
}
/**
- * Create a new {@link ConnectActionClientObject} with the current
- * application version (see {@link Version#getCurrentVersion()}) as the
- * client version.
+ * Create a new {@link ConnectActionClientObject}.
*
* @param host
* the host to bind to
import java.net.Socket;
import java.net.UnknownHostException;
-import be.nikiroo.utils.Version;
-
/**
* Class used for the client basic handling.
* <p>
*/
public class ConnectActionClientString extends ConnectActionClient {
/**
- * Create a new {@link ConnectActionClientString} with the current
- * application version (see {@link Version#getCurrentVersion()}) as the
- * client version.
+ * Create a new {@link ConnectActionClientString}.
*
* @param s
* the socket to bind to
}
/**
- * Create a new {@link ConnectActionClientString} with the current
- * application version (see {@link Version#getCurrentVersion()}) as the
- * client version.
+ * Create a new {@link ConnectActionClientString}.
*
* @param host
* the host to bind to
package be.nikiroo.utils.serial.server;
+import java.io.IOException;
import java.net.Socket;
+import javax.net.ssl.SSLException;
+
/**
* Base class used for the server basic handling.
* <p>
action = new ConnectAction(s, true, key) {
@Override
protected void action() throws Exception {
+ ConnectActionServer.this.serverHello();
ConnectActionServer.this.action();
}
};
}
+ /**
+ * Send the HELLO message (check that the client sends a String "HELLO" and
+ * send it back, to check I/O and encryption modes).
+ *
+ * @throws IOException
+ * in case of I/O error
+ * @throws SSLException
+ * in case of encryption error
+ */
+ protected void serverHello() throws IOException, SSLException {
+ String HELLO = action.recString();
+ if (!"HELLO".equals(HELLO)) {
+ throw new SSLException("Server did not accept the encryption key");
+ }
+ action.sendString(HELLO);
+ }
+
/**
* Actually start the process and call the action (synchronous).
*/
import java.io.IOException;
import java.net.Socket;
-import be.nikiroo.utils.Version;
-
/**
* Class used for the server basic handling.
* <p>
*/
public class ConnectActionServerObject extends ConnectActionServer {
/**
- * Create a new {@link ConnectActionServerObject} with the current
- * application version (see {@link Version#getCurrentVersion()}) as the
- * server version.
+ * Create a new {@link ConnectActionServerObject} as the server version.
*
* @param s
* the socket to bind to
import java.io.IOException;
import java.net.Socket;
-import be.nikiroo.utils.Version;
-
/**
* Class used for the server basic handling.
* <p>
*/
public class ConnectActionServerString extends ConnectActionServer {
/**
- * Create a new {@link ConnectActionServerString} with the current
- * application version (see {@link Version#getCurrentVersion()}) as the
- * server version.
+ * Create a new {@link ConnectActionServerString} as the server version.
*
* @param s
* the socket to bind to
exiting = true;
try {
- new ConnectActionClientObject(new Socket((String) null,
- port), key).connect();
+ getConnectionToMe().connect();
long time = 0;
while (ss != null && timeout > 0 && timeout > time) {
Thread.sleep(10);
}
}
+ /**
+ * Return a connection to this server (used by the Exit code to send an exit
+ * message).
+ *
+ * @return the connection
+ *
+ * @throws UnknownHostException
+ * the host should always be NULL (localhost)
+ * @throws IOException
+ * in case of I/O error
+ */
+ abstract protected ConnectActionClient getConnectionToMe()
+ throws UnknownHostException, IOException;
+
/**
* Change the number of currently serviced actions.
*
@SuppressWarnings("unused")
protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
}
-
- /**
- * Create a {@link Socket}.
- *
- * @param host
- * the host to connect to
- * @param port
- * the port to connect to
- *
- * @return the {@link Socket}
- *
- * @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
- */
- @Deprecated
- static Socket createSocket(String host, int port) throws IOException {
- return new Socket(host, port);
- }
-
- /**
- * Create a {@link ServerSocket}.
- *
- * @param port
- * the port to accept connections on
- *
- * @return the {@link ServerSocket}
- *
- * @throws IOException
- * in case of I/O error
- * @throws UnknownHostException
- * if the IP address of the host could not be determined
- * @throws IllegalArgumentException
- * if the port parameter is outside the specified range of valid
- * port values, which is between 0 and 65535, inclusive
- */
- @Deprecated
- static ServerSocket createSocketServer(int port) throws IOException {
- return new ServerSocket(port);
- }
}
trace("<<< SERVER", data);
}
+ @Override
+ protected ConnectActionClient getConnectionToMe()
+ throws UnknownHostException, IOException {
+ return new ConnectActionClientString(new Socket((String) null,
+ getPort()), key);
+ }
+
@Override
public void run() {
getTraceHandler().trace(
};
}
+ @Override
+ protected ConnectActionClient getConnectionToMe()
+ throws UnknownHostException, IOException {
+ return new ConnectActionClientObject(new Socket((String) null,
+ getPort()), key);
+ }
+
/**
* This is the method that is called on each client request.
* <p>
};
}
+ @Override
+ protected ConnectActionClient getConnectionToMe()
+ throws UnknownHostException, IOException {
+ return new ConnectActionClientString(new Socket((String) null,
+ getPort()), key);
+ }
+
/**
* This is the method that is called on each client request.
* <p>
import java.net.URL;
+import be.nikiroo.utils.TraceHandler;
import be.nikiroo.utils.serial.server.ConnectActionClientObject;
import be.nikiroo.utils.serial.server.ConnectActionClientString;
import be.nikiroo.utils.serial.server.ConnectActionServerObject;
try {
try {
- new ConnectActionClientObject(null, port, key) {
+ new ConnectActionClientString(null, port, key) {
@Override
public void action() throws Exception {
rec[0] = "ok";