91 lines
2.4 KiB
Java
91 lines
2.4 KiB
Java
package com.alterdekim;
|
|
|
|
|
|
import com.alterdekim.db.Database;
|
|
import com.alterdekim.hearthhack.util.Util;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.net.ssl.*;
|
|
import java.io.IOException;
|
|
import java.util.LinkedList;
|
|
|
|
@Slf4j
|
|
public class Server implements Runnable {
|
|
|
|
private SSLServerSocket serverSocket;
|
|
private int portNumber;
|
|
private Thread acceptThread;
|
|
|
|
private LinkedList<Connection> connections;
|
|
|
|
private Database db;
|
|
|
|
public Server(int port) {
|
|
portNumber = port;
|
|
connections = new LinkedList<>();
|
|
|
|
db = new Database("localhost", 5984, "hs_lobby");
|
|
try {
|
|
//SSLServerSocketFactory sslssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
|
|
|
serverSocket = (SSLServerSocket) Util.getSocketFactory(
|
|
"test.com.crt",
|
|
"test.com.crt",
|
|
"test.com.key",
|
|
""
|
|
).createServerSocket(portNumber,15);
|
|
|
|
|
|
db.start();
|
|
|
|
} catch (Exception e) {
|
|
log.error("Init failed: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
public void startListening() {
|
|
acceptThread = new Thread(this);
|
|
acceptThread.start();
|
|
}
|
|
public void stopListening() {
|
|
for(Connection c:connections) {
|
|
c.stopListeningAndDisconnect();
|
|
}
|
|
|
|
try {
|
|
serverSocket.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
while(true) {
|
|
SSLSocket s = (SSLSocket) serverSocket.accept();
|
|
Connection c = new Connection(s, db);
|
|
connections.add(c);
|
|
log.info("New Connection Established From "+s.getInetAddress().toString());
|
|
}
|
|
} catch(java.net.SocketException e) {
|
|
log.error("Listening thread terminated with exception: " + e.getMessage());
|
|
} catch(IOException e) {
|
|
log.error(e.getMessage());
|
|
}
|
|
}
|
|
|
|
public void removeConnection(Connection c) {
|
|
connections.remove(c);
|
|
}
|
|
|
|
public void printConnections() {
|
|
log.info("Number of connections "+connections.toString());
|
|
|
|
for(int i=0; i<connections.size(); i++) {
|
|
log.info(connections.toString());
|
|
}
|
|
}
|
|
}
|