1 module mars.msg;
2 
3 enum RequestState 
4 {
5     executed,
6 
7     // ... client side bugs or tampering of the request
8     rejectedAsDecodingFailed, /// the deconding of the data is failed
9     rejectedAsWrongParameter, /// the value of one of the request parameters is wrong.
10     rejectedAsNotAuthorised,  /// the client is not authorised for the request (ex, subscription before of authentication)
11     rejectedAsPGSqlError,     /// PostgreSQL unhandled error
12     internalServerError,
13 }
14 enum MsgTypeStoC {
15     autologinReq     = 60, autologinRep,
16     syncOperationReq = 62, syncOperationRep,
17     importRecordsReq = 64, importRecordsRep,
18     deleteRecordsReq = 66, deleteRecordsRep,
19     insertRecordsReq = 68, insertRecordsRep,
20     updateRecordsReq = 70, updateRecordsRep,
21 }
22 
23 
24 
25 struct AutologinReq
26 {
27     static immutable type = MsgTypeStoC.autologinReq;
28     string username;                   /// the username that has performed the autologin.
29     string sqlCreateDatabase;          /// sql statements to execute for the creation of client side tables
30     immutable(string)[] sqlStatements; /// sql statements to prepare, for further operations on the tables
31     immutable(string)[] jsStatements;  /// javascript statements to eval, like constraints, key extraction, etc.
32 }
33 
34 struct DeleteRecordsReq 
35 {
36     static immutable type = MsgTypeStoC.deleteRecordsReq;
37     ulong tableIndex;
38     ulong statementIndex;
39     immutable(ubyte)[] encodedRecords = []; 
40 }
41 
42 struct DeleteRecordsRep
43 {
44     static immutable type =  MsgTypeStoC.deleteRecordsRep;
45 }
46 
47 struct InsertRecordsReq
48 {
49     static immutable type = MsgTypeStoC.insertRecordsReq;
50     ulong tableIndex;
51     ulong statementIndex;
52     immutable(ubyte)[] encodedRecords;
53 }
54 
55 struct InsertRecordsRep
56 {
57     static immutable type = MsgTypeStoC.insertRecordsRep;
58 }
59 
60 struct ImportRecordsReq {
61     static immutable type = MsgTypeStoC.importRecordsReq;
62     ulong tableIndex;
63     ulong statementIndex;
64     immutable(ubyte)[] encodedRecords;
65 }
66 
67 struct UpdateRecordsReq {
68     static immutable type = MsgTypeStoC.updateRecordsReq;
69     ulong tableIndex;
70     immutable(ubyte)[] encodedRecords;
71 }
72 
73 struct ImportRecordsRep {
74     static immutable type = MsgTypeStoC.importRecordsRep;
75 }
76 
77 struct SyncOperationReq
78 {
79     enum SyncOperation { starting, completed }
80 
81     static immutable type = MsgTypeStoC.syncOperationReq;
82     SyncOperation operation; 
83 }
84 
85 struct SyncOperationReply
86 {
87     static immutable type = MsgTypeStoC.syncOperationRep;
88 }
89 
90 // ----
91 
92 enum MsgType {
93     authenticationRequest, authenticationReply,
94     authenticateRequest, authenticateReply,
95     discardAuthenticationRequest, discardAuthenticationReply,
96 
97     importValuesRequest  = 22, importValuesReply,
98     insertValuesRequest  = 24, insertValuesReply,
99     updateValuesRequest  = 26, updateValuesReply,
100     deleteRecordRequest  = 28, deleteRecordReply,
101 
102     optUpdateReq = 50, optUpdateRep = 51, // request the server to perform an update and confirm an optimistic one
103     subscribeReq = 52, subscribeRep = 52, // request to subscribe to a query
104 
105 
106 
107     welcomeBroadcast = 100, goodbyBroadcast,
108 
109 
110     callServerMethodRequest = 150, callServerMethodReply,
111 
112     disconnectRequest = 200,
113     aborting
114 }
115 
116 
117 
118 struct AuthenticationRequest {
119     static immutable type = MsgType.authenticationRequest;
120     string username;
121 }
122 
123 struct AuthenticationReply {
124     enum { seedProvided, 
125         invalidUsername, 
126         alreadyAuthorised, /// one user is already logged in, and authorised.
127     }
128     static immutable type = MsgType.authenticationReply;
129     int status;
130     string seed;
131 }
132 
133 struct AuthenticateRequest {
134     static immutable type = MsgType.authenticateRequest;
135     string hash;
136 }
137 
138 /// Warning, this enum is checked in the client also!
139 enum AuthoriseError {
140     assertCheck,
141 
142     authorised,              
143     databaseOffline,         /// the database is offline, so can't autorise
144     wrongUsernameOrPassword, /// password authentication failed for user "user"
145     unknownError,            /// unknown or not handled error code.
146 }
147 struct AuthenticateReply {
148     static immutable type = MsgType.authenticateReply;
149     int status;
150     string sqlCreateDatabase;
151     immutable(string)[] sqlStatements;
152     immutable(string)[] jsStatements;  /// javascript statements to eval, like constraints, key extraction, etc.
153 }
154 
155 struct DiscardAuthenticationRequest {
156     static immutable type = MsgType.discardAuthenticationRequest;
157 }
158 
159 struct DiscardAuthenticationReply {
160     static immutable type = MsgType.discardAuthenticationReply;
161 }
162 
163 // 
164 struct SubscribeReq 
165 {
166     static immutable type = MsgType.subscribeReq;
167     string select;
168     string parameters;
169 }
170 
171 struct SubscribeRep 
172 {
173     static immutable type = MsgType.subscribeRep;
174     RequestState state;
175     string json;
176 }
177 
178 struct ImportValuesRequest {
179     static immutable type = MsgType.importValuesRequest;
180     int statementIndex;
181     immutable(ubyte)[] bytes;
182 }
183 
184 struct ImportValuesReply {
185     static immutable type = MsgType.importValuesReply;
186     int donno;
187 }
188 
189 
190 
191 // S --> C in the op
192 struct InsertValuesRequest {
193     static immutable type = MsgType.insertValuesRequest;
194     int statementIndex;
195     immutable(ubyte)[] bytes;
196 }
197 
198 enum InsertError {
199     assertCheck,
200     inserted,
201     duplicateKeyViolations,
202     unknownError,
203 }
204 // sent from server to client validating or rejecting the optimistic update
205 struct InsertValuesReply {
206     static immutable type = MsgType.insertValuesReply;
207     int insertStatus; // the insert error
208     immutable(ubyte)[] bytes = []; // the server inserted record
209     immutable(ubyte)[] clientKeys = [];
210     int tableIndex = -1;
211     int statementIndex = -1; // the sql statement to use for emending the client with the server data
212     int statementIndex2 = -1; // idem. For example, on errors, the first one is used to update the deco, then delete
213 }
214 
215 struct DeleteRecordRequest {
216     static immutable type = MsgType.deleteRecordRequest;
217     int statementIndex;
218     immutable(ubyte)[] bytes = []; 
219 }
220 
221 enum DeleteError {
222     assertCheck,
223     deleted,
224     unknownError,
225 }
226 
227 // the reply is flowing from server to client
228 struct DeleteRecordReply {
229     static immutable type =  MsgType.deleteRecordReply;
230     int deleteStatus;
231     immutable(ubyte)[] serverRecord = []; // if we can't delete the record, we must re-insert it into the client
232     int tableIndex;
233     int statementIndex;
234 }
235 
236 // request an update of a record to the server, that the client has optimistically updated
237 struct OptUpdateReq 
238 {
239     static immutable type = MsgType.optUpdateReq;
240     ulong tableIndex;           /// the index identifier of the updated table.
241     immutable(ubyte)[] keys;    /// the primary keys of the record to update
242     immutable(ubyte)[] record;  /// the new values for that record
243 }
244 
245 struct OptUpdateRep 
246 {
247     static immutable type = MsgType.optUpdateRep;
248     RequestState state;
249 }
250 
251 struct UpdateValuesRequest {
252     static immutable type = MsgType.updateValuesRequest;
253     int statementIndex;
254     immutable(ubyte)[] bytes;
255 }
256 
257 struct UpdateValuesReply {
258     static immutable type = MsgType.updateValuesReply;
259     int donno;
260 }
261 
262 struct CallServerMethodRequest {
263     static immutable type = MsgType.callServerMethodRequest;
264     string method; immutable(ubyte)[] parameters;
265 }
266 
267 struct CallServerMethodReply {
268     static immutable type = MsgType.callServerMethodReply;
269     string returns;
270 }