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