1 module mars.msg;
2 
3 enum MsgTypeStoC {
4     autologinReq     = 60, autologinRep,
5     syncOperationReq = 62, syncOperationRep,
6     importRecordsReq = 64, importRecordsRep,
7     deleteRecordsReq = 66, deleteRecordsRep,
8     insertRecordsReq = 68, insertRecordsRep,
9 }
10 
11 struct AutologinReq
12 {
13     static immutable type = MsgTypeStoC.autologinReq;
14     string username;                   /// the username that has performed the autologin.
15     string sqlCreateDatabase;          /// sql statements to execute for the creation of client side tables
16     immutable(string)[] sqlStatements; /// sql statements to prepare, for further operations on the tables
17 }
18 
19 struct DeleteRecordsReq 
20 {
21     static immutable type = MsgTypeStoC.deleteRecordsReq;
22     ulong tableIndex;
23     ulong statementIndex;
24     immutable(ubyte)[] encodedRecords = []; 
25 }
26 
27 struct DeleteRecordsRep
28 {
29     static immutable type =  MsgTypeStoC.deleteRecordsRep;
30 }
31 
32 struct InsertRecordsReq
33 {
34     static immutable type = MsgTypeStoC.insertRecordsReq;
35     ulong tableIndex;
36     ulong statementIndex;
37     immutable(ubyte)[] encodedRecords;
38 }
39 
40 struct InsertRecordsRep
41 {
42     static immutable type = MsgTypeStoC.insertRecordsRep;
43 }
44 
45 struct ImportRecordsReq {
46     static immutable type = MsgTypeStoC.importRecordsReq;
47     ulong tableIndex;
48     ulong statementIndex;
49     immutable(ubyte)[] encodedRecords;
50 }
51 
52 struct ImportRecordsRep {
53     static immutable type = MsgTypeStoC.importRecordsRep;
54 }
55 
56 struct SyncOperationReq
57 {
58     enum SyncOperation { starting, completed }
59 
60     static immutable type = MsgTypeStoC.syncOperationReq;
61     SyncOperation operation; 
62 }
63 
64 struct SyncOperationReply
65 {
66     static immutable type = MsgTypeStoC.syncOperationRep;
67 }
68 
69 // ----
70 
71 enum MsgType {
72     authenticationRequest, authenticationReply,
73     authenticateRequest, authenticateReply,
74     discardAuthenticationRequest, discardAuthenticationReply,
75 
76     importValuesRequest  = 22, importValuesReply,
77     insertValuesRequest  = 24, insertValuesReply,
78     updateValuesRequest  = 26, updateValuesReply,
79     deleteRecordRequest  = 28, deleteRecordReply,
80 
81     welcomeBroadcast = 100, goodbyBroadcast,
82 
83 
84     callServerMethodRequest = 150, callServerMethodReply,
85 
86     disconnectRequest = 200,
87     aborting
88 }
89 
90 
91 
92 struct AuthenticationRequest {
93     static immutable type = MsgType.authenticationRequest;
94     string username;
95 }
96 
97 struct AuthenticationReply {
98     enum { seedProvided, invalidUsername }
99     static immutable type = MsgType.authenticationReply;
100     int status;
101     string seed;
102 }
103 
104 struct AuthenticateRequest {
105     static immutable type = MsgType.authenticateRequest;
106     string hash;
107 }
108 
109 /// Warning, this enum is checked in the client also!
110 enum AuthoriseError {
111     assertCheck,
112 
113     authorised,              
114     databaseOffline,         /// the database is offline, so can't autorise
115     wrongUsernameOrPassword, /// password authentication failed for user "user"
116     unknownError,            /// unknown or not handled error code.
117 }
118 struct AuthenticateReply {
119     static immutable type = MsgType.authenticateReply;
120     int status;
121     string sqlCreateDatabase;
122     immutable(string)[] sqlStatements;
123 }
124 
125 struct DiscardAuthenticationRequest {
126     static immutable type = MsgType.discardAuthenticationRequest;
127 }
128 
129 struct DiscardAuthenticationReply {
130     static immutable type = MsgType.discardAuthenticationReply;
131 }
132 
133 struct ImportValuesRequest {
134     static immutable type = MsgType.importValuesRequest;
135     int statementIndex;
136     immutable(ubyte)[] bytes;
137 }
138 
139 struct ImportValuesReply {
140     static immutable type = MsgType.importValuesReply;
141     int donno;
142 }
143 
144 
145 
146 // S --> C in the op
147 struct InsertValuesRequest {
148     static immutable type = MsgType.insertValuesRequest;
149     int statementIndex;
150     immutable(ubyte)[] bytes;
151 }
152 
153 enum InsertError {
154     assertCheck,
155     inserted,
156     duplicateKeyViolations,
157     unknownError,
158 }
159 struct InsertValuesReply {
160     static immutable type = MsgType.insertValuesReply;
161     int insertStatus; // the insert error
162     immutable(ubyte)[] bytes = []; // the server inserted record
163     immutable(ubyte)[] clientKeys = [];
164     int tableIndex = -1;
165     int statementIndex = -1; // the sql statement to use for emending the client with the server data
166 }
167 
168 struct DeleteRecordRequest {
169     static immutable type = MsgType.deleteRecordRequest;
170     int statementIndex;
171     immutable(ubyte)[] bytes = []; 
172 }
173 
174 enum DeleteError {
175     assertCheck,
176     deleted,
177     unknownError,
178 }
179 // flowing from server to client
180 struct DeleteRecordReply {
181     static immutable type =  MsgType.deleteRecordReply;
182     int deleteStatus;
183     immutable(ubyte)[] serverRecord = []; // if we can't delete the record, we must re-insert it into the client
184     int tableIndex;
185     int statementIndex;
186 }
187 
188 struct UpdateValuesRequest {
189     static immutable type = MsgType.updateValuesRequest;
190     int statementIndex;
191     immutable(ubyte)[] bytes;
192 }
193 
194 struct UpdateValuesReply {
195     static immutable type = MsgType.updateValuesReply;
196     int donno;
197 }
198 
199 struct CallServerMethodRequest {
200     static immutable type = MsgType.callServerMethodRequest;
201     string method; immutable(ubyte)[] parameters;
202 }
203 
204 struct CallServerMethodReply {
205     static immutable type = MsgType.callServerMethodReply;
206     string returns;
207 }