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