private bool ProceedWrite(string command, string key, string value) { var commandMessage = new DBUpdateMessage { Command = command, Key = key, Value = value, ServerId = Config.ServerId(), Version = _db.VersionForKey(key) + 1 }; if (!commandMessage.isValid()) { return(false); } try { _publishSocket.Send(commandMessage.Encode()); return(true); } catch { Console.WriteLine("Exception while trying to publish db updates"); return(false); } }
public static DBUpdateMessage Decode(byte[] bytes) { try { var message = new DBUpdateMessage(); var decoded = MPack.ParseFromBytes(bytes); message.Command = decoded["command"].To <string>(); message.Key = decoded["key"].To <string>(); message.Value = (message.Command == "remove") ? null : decoded["value"].To <string>(); message.Version = decoded["version"].To <int>(); message.ServerId = decoded["serverId"].To <int>(); return(message); } catch { return(null); } }
private void ProceedWriteCommand(DBUpdateMessage message) { var dbValue = new DBValue { Value = message.Value, Version = message.Version, ServerId = message.ServerId }; if (message.Command == "set") { _db.Set(message.Key, dbValue); } else if (message.Command == "remove") { _db.Remove(message.Key, dbValue); } else { Console.WriteLine("Unknown command"); } }
public void Execute() { byte[] buffer; while (true) { Console.WriteLine("Listening..."); try { buffer = _socket.Receive(); var message = DBUpdateMessage.Decode(buffer); Console.WriteLine("\t[" + message.ServerId + "] Received command '" + message.Command + "' for key: " + message.Key); ProceedWriteCommand(message); Console.WriteLine("\tOK"); } catch { Console.WriteLine("Catch exception in subscriber"); } } }