1 module mars.protosub; 2 3 import std.conv; 4 import std.variant; 5 import std.experimental.logger; 6 import vibe.data.json; 7 8 import mars.client; 9 import mars.msg; 10 11 12 13 14 void protoSubscribe(S)(MarsClient* client, S socket) { 15 auto state = RequestState.executed; 16 17 18 auto req = socket.binaryAs!SubscribeReq; 19 // XXX handle wrong parameters 20 //if( req.tableIndex < 0 || req.tableIndex > marsServer.tables.length ){ 21 // state = RequestState.rejectedAsWrongParameter; 22 //} 23 24 infof("===> %s ", req.select, req.parameters); 25 26 // ... let's put the parameters that are inside the json in a variant AA 27 // in that way we are isolating the encoding, vibe json, from the rest ... 28 Variant[string] parameters; 29 if(req.parameters){ 30 auto json = parseJsonString(req.parameters); 31 foreach (string name, value; json){ 32 if( value.type == Json.Type..string ){ parameters[name] = value.get!string; } 33 // ... vibe doc says Json int is a '64bit integer value' 34 else if(value.type == Json.Type.int_){ parameters[name] = value.get!long.to!int; } 35 else if(value.type == Json.Type.bool_){ parameters[name] = value.get!bool; } 36 37 else if(value.type == Json.Type.null_){ 38 state = RequestState.rejectedAsWrongParameter; 39 errorf("mars - C ... S - something wrong with query parameter, null json data type"); 40 } 41 else assert(false, value.type.to!string); // XXX reply back error 42 } 43 } 44 45 string stringified = "[]"; 46 if( state == state.init ){ // XXX 47 auto json = client.vueSubscribe(req.select, parameters, state); 48 stringified = json.toString(); 49 } 50 51 final switch(state) with(RequestState){ 52 case rejectedAsWrongParameter: 53 case internalServerError: 54 case rejectedAsDecodingFailed: 55 case rejectedAsNotAuthorised: 56 case rejectedAsForeignKeyViolation: 57 case rejectedAsPGSqlError: 58 socket.sendReply(req, SubscribeRep(state, stringified)); 59 break; 60 case executed: 61 socket.sendReply(req, SubscribeRep(state, stringified)); 62 break; 63 } 64 65 }