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         serverInfo = response["data"];
49 
50         /* TODO: Implement me */
51         return serverInfo;
52     }
53 
54     public bool isClient(string username)
55     {
56         /* TODO: Implement me */
57         return true;
58     }
59 
60     public string[] getClients()
61     {
62         string[] clientList;
63 
64         JSONValue message;
65 
66         JSONValue commandBlock;
67         commandBlock["type"] = "listClients";
68 
69         message["command"] = commandBlock;
70 
71         sendMessage(informerSocket, message);
72 
73         JSONValue response;
74         receiveMessage(informerSocket, response);
75         
76         string statusCode = response["status"].str();
77 
78         if(cmp(statusCode, "0") == 0)
79         {
80             JSONValue[] clientListJSON = response["data"].array();
81             for(ulong i = 0; i < clientListJSON.length; i++)
82             {
83                 clientList ~= clientListJSON[i].str();
84             }
85         }
86         else
87         {
88             /* error */
89         }
90 
91         return clientList;
92     }
93 }