|
|
@ -11,28 +11,28 @@ import java.util.Queue; |
|
|
|
* Подключения ставятся в очередь |
|
|
|
* Подключения ставятся в очередь |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ServerLauncher implements Runnable{ |
|
|
|
public class ServerLauncher implements Runnable{ |
|
|
|
private int tcpPort = 8081; |
|
|
|
private int port = 8081; |
|
|
|
private ServerSocketChannel channel = null; |
|
|
|
private ServerSocketChannel channel = null; |
|
|
|
private Queue<Connection> connectionQueue = null; |
|
|
|
private Queue<Connection> connectionQueue = null; |
|
|
|
private boolean isActive = false;//флаг проверки, запущен ли сервер
|
|
|
|
private boolean isActive = false;//флаг проверки, запущен ли сервер
|
|
|
|
|
|
|
|
|
|
|
|
ServerLauncher(int port, Queue<Connection> connectionQueue){ |
|
|
|
ServerLauncher(int port, Queue<Connection> connectionQueue){ |
|
|
|
tcpPort = port; |
|
|
|
this.port = port; |
|
|
|
this.connectionQueue = connectionQueue; |
|
|
|
this.connectionQueue = connectionQueue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
try{ |
|
|
|
try{ |
|
|
|
this.channel = ServerSocketChannel.open(); |
|
|
|
channel = ServerSocketChannel.open(); |
|
|
|
this.channel.bind(new InetSocketAddress(tcpPort)); |
|
|
|
channel.bind(new InetSocketAddress(port)); |
|
|
|
} catch(IOException e){ |
|
|
|
} catch(IOException e){ |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
while(true){ |
|
|
|
while(true){ |
|
|
|
try{ |
|
|
|
try{ |
|
|
|
SocketChannel accepted = this.channel.accept(); |
|
|
|
SocketChannel accepted = channel.accept(); |
|
|
|
System.out.println("Socket accepted: " + accepted); |
|
|
|
System.out.println("Socket accepted: " + accepted); |
|
|
|
//todo check if the queue can even accept more sockets.
|
|
|
|
//todo check if the queue can even accept more sockets.
|
|
|
|
this.connectionQueue.add(new Connection(accepted)); |
|
|
|
this.connectionQueue.add(new Connection(accepted)); |
|
|
|