-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.d
157 lines (143 loc) · 5.15 KB
/
processor.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module processor;
/****************************************************************************
**
** This file is part of the SMQTT server project.
**
** Copyright (c) 2017 Olagoke Adedamola Farouq
**
** Contact: [email protected]
**
** SMQTT server is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** SMQTT server is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with SMQTT server. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
public import services;
/**
* This is what i call a "hot object". it will be used by
* different threads at different times on different cores.
*/
class Processor{
//private char[1024] buffer;
private Socket sock;
private Services services;
private ResponseFormat response;
private int bufferSize;
this(){
this.response = ResponseFormat();
this.services = new Services();
this.bufferSize = to!int(Util.settings["buffer_size"]);
}
public void processNewClient(Socket client){
auto newSocket = client.accept();
Util.connectedClients ~= newSocket; //save time stamp, let another thread check if time > 3secs(2actually)
Util.echo(this.response.executed().and(ResponseFormat.CONNECTED), client);
}
public void processClientMessage(Socket client, int clientID){
//only 200 bytes allowed
char[200] buffer; //use char[].init
try{
auto got = client.receive(buffer); //do injection checks
string rcID = this._getRegisteredClient(clientID);
if(got <= 0){
Util.echo(this.response.notExecuted().and(ResponseFormat.EMPTY_MESSAGE), client);
if(rcID != null){
writeln(format("Client[%s] has been disconnected!", rcID));
this.services.closeClientSession(rcID);
}
this.closeClientConn(client,clientID);
return;
} else if(got > this.bufferSize)
got = this.bufferSize; //control buffer size
string data_ = buffer[0 .. got].idup; //we should log this data
string[] data = split(data_, to!char(Util.settings["api_token"]));
switch(buffer[0]){
case '0':
if(data.length < 2){
Util.echo(this.response.notExecuted().and(ResponseFormat.MALFORMED_MESSAGE), client);
break;
}
if(rcID == null){ //not registered!!
Util.echo(this.response.notExecuted().and(ResponseFormat.NOT_REGISTERED), client);
break;
}
Util.echo(this.services.subscribe(data[1], rcID), client);
break;
case '1':
if(data.length < 3){
Util.echo(this.response.notExecuted().and(ResponseFormat.MALFORMED_MESSAGE), client);
break;
}
if(rcID == null){ //not registered!!
Util.echo(this.response.notExecuted().and(ResponseFormat.NOT_REGISTERED), client);
break;
}
Util.echo(this.services.publish(data[2], data[1], rcID), client);
break;
case '2':
if(data.length < 3){
Util.echo(this.response.notExecuted().and(ResponseFormat.MALFORMED_MESSAGE), client);
break;
}
if(rcID == null){ //not registered!!
Util.echo(this.response.notExecuted().and(ResponseFormat.NOT_REGISTERED), client);
break;
}
Util.echo(this.services.publish(data[2], data[1], rcID), client);
break;
case '3':
if(data.length < 2){
Util.echo(this.response.notExecuted().and(ResponseFormat.MALFORMED_MESSAGE), client);
break;
}
Util.echo(this.services.registerClient(data[1], client),client);
break;
default:
Util.echo(this.response.notExecuted().and(ResponseFormat.INVALID_SERVICE), client);
break;
}
writefln("treated client[%s]> [raw] '%s'", clientID, data_);
} catch (Exception err){
Util.logError(format("%s in %s(&s)",err.msg, err.file, err.line));
writeln("--debug: Fatal Server Error 3!");
Util.echo(this.response.notExecuted().and(ResponseFormat.SERVER_ERROR), client);
} catch (Error err){
Util.logError(format("%s in %s(&s)",err.msg, err.file, err.line));
writeln("--debug: Fatal Server Error 4!");
Util.echo(this.response.notExecuted().and(ResponseFormat.SERVER_ERROR), client);
}
}
private string _getRegisteredClient(int cclientID){ //connected client id
foreach(rcID, client; Util.registeredClients)
if(client == Util.connectedClients[cclientID])
return rcID;
return null;
}
private void closeClientConn(Socket client, int clientID=-1){ //implement in body
if(clientID != -1){
Util.connectedClients = remove(Util.connectedClients, clientID);
}
client.shutdown(SocketShutdown.BOTH);
client.close();
}
}
/+
** Data Format
* COMMAND, channel, DATA
* 0 | 1 | 2 | 3, <byte>, <byte> == sub, pub , init, not
** Return format
* ACKNOWLEDGEMENT, EXECUTION
* 1 | 2, 1 | 2 -> 1 - good | bad
* 1|2 - publish|notice , <data>
+/
// sub to notif self/notify/+
// you can only sub to notifications under a particular topic