1 module listeners.listener;
2 
3 import utils.debugging : debugPrint;
4 import std.conv : to;
5 import std.socket : Socket, AddressFamily, SocketType, ProtocolType, parseAddress, SocketFlags, Address;
6 import core.thread : Thread;
7 import std.stdio : writeln, File;
8 import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON;
9 import std.string : cmp;
10 import handlers.handler;
11 import server.server;
12 import connection.connection;
13 
14 
15 /* TODO: Implement me */
16 /* All this will do is accept incoming connections
17  * but they will be pooled in the BesterServer.
18  */
19 public class BesterListener : Thread
20 {
21 
22 	/* The associated BesterServer */
23 	private BesterServer server;
24 
25 	/* The server's socket */
26 	private Socket serverSocket;
27 
28 	this(BesterServer besterServer)
29 	{
30 		/* Set the function address to be called as the worker function */
31 		super(&run);
32 
33 		/* Set this listener's BesterServer */
34 		this.server = besterServer;
35 	}
36 
37 	public void setServerSocket(Socket serverSocket)
38 	{
39 		/* Set the server socket */
40 		this.serverSocket = serverSocket;
41 	}
42 
43 
44 	/* Start listen loop */
45 	public void run()
46 	{
47 		serverSocket.listen(1); /* TODO: This value */
48 		debugPrint("Server listen loop started");
49 		while(true)
50 		{
51 			/* Wait for an incoming connection */
52 			Socket clientConnection = serverSocket.accept();
53 
54 			/* Create a new client connection handler and start its thread */
55 			BesterConnection besterConnection = new BesterConnection(clientConnection, server);
56 			besterConnection.start();
57 
58 			/* Add this client to the list of connected clients */
59 			server.clients ~= besterConnection;
60 		}
61 	}
62 	
63 }