1 module libinformer;
2 
3 import std.socket;
4 import bmessage;
5 import std.json;
6 import std.string;
7 import std.stdio;
8 
9 public final class BesterInformerClient
10 {
11     /* The informer socket */
12     private Socket informerSocket;
13 
14     this(string socketPath)
15     {
16         try
17         {
18             informerSocket = new Socket(AddressFamily.UNIX, SocketType.STREAM);
19             informerSocket.connect(new UnixAddress(socketPath));
20         }
21         catch(AddressException e)
22         {
23 
24         }
25         catch(SocketException e)
26         {
27 
28         }
29     }
30 
31     public JSONValue getServerInfo()
32     {
33         /* The server info */
34         JSONValue serverInfo;
35 
36          JSONValue message;
37 
38         JSONValue commandBlock;
39         commandBlock["type"] = "serverInfo";
40 
41         message["command"] = commandBlock;
42 
43         sendMessage(informerSocket, message);
44 
45         JSONValue response;
46         receiveMessage(informerSocket, response);
47 
48         /* TODO: Implement me */
49         return serverInfo;
50     }
51 
52     public bool isClient(string username)
53     {
54         /* TODO: Implement me */
55         return true;
56     }
57 
58     public string[] getClients()
59     {
60         string[] clientList;
61 
62         JSONValue message;
63 
64         JSONValue commandBlock;
65         commandBlock["type"] = "listClients";
66 
67         message["command"] = commandBlock;
68 
69         sendMessage(informerSocket, message);
70 
71         JSONValue response;
72         receiveMessage(informerSocket, response);
73         
74         string statusCode = response["status"].str();
75 
76         if(cmp(statusCode, "0") == 0)
77         {
78             JSONValue[] clientListJSON = response["data"].array();
79             for(ulong i = 0; i < clientListJSON.length; i++)
80             {
81                 clientList ~= clientListJSON[i].str();
82             }
83         }
84         else
85         {
86             /* error */
87         }
88 
89         return clientList;
90     }
91 }