init
This commit is contained in:
66
server/src/main/java/org/orinprojects/Server.java
Normal file
66
server/src/main/java/org/orinprojects/Server.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package org.orinprojects;
|
||||
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.orinprojects.encryption.Encryption;
|
||||
import org.orinprojects.exceptions.ArgumentsException;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.security.KeyPair;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PublicKey;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
public class Server {
|
||||
|
||||
public static List<ClientHandler> clients = new ArrayList<>();
|
||||
|
||||
public static HashMap<String, PublicKey> clientKeys = new HashMap<>();
|
||||
|
||||
public static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(20);
|
||||
|
||||
public static KeyPair serverKeys;
|
||||
|
||||
public static SecretKey aesKey;
|
||||
|
||||
public static void main(String[] args) throws IOException, ArgumentsException, NoSuchAlgorithmException {
|
||||
Server.serverKeys = Encryption.generateRSAKey();
|
||||
Server.aesKey = Encryption.generateAESKey();
|
||||
|
||||
int portNumber = getPortNumber(args);
|
||||
|
||||
ServerSocket server = new ServerSocket(portNumber);
|
||||
while (!server.isClosed()) {
|
||||
Socket clientSocket = server.accept();
|
||||
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||
clients.add(clientHandler);
|
||||
|
||||
executor.execute(clientHandler);
|
||||
}
|
||||
}
|
||||
|
||||
private static int getPortNumber(String[] args) throws ArgumentsException {
|
||||
if (args.length != 2)
|
||||
throw new ArgumentsException("Not enough or too much arguments");
|
||||
|
||||
if (!args[0].equals("-p"))
|
||||
throw new ArgumentsException("No port parameter. Use -p to specify port");
|
||||
|
||||
int port;
|
||||
try {
|
||||
port = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException numberFormatException) {
|
||||
throw new ArgumentsException("Wrong number format! Example: -p 6666");
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user