StockManager() { LoadStock(); var serverRabbitMQ = new ServerMessaging("localhost", "stock_queue"); serverRabbitMQ.Listen((ea, json) => { if (json == null) { serverRabbitMQ.Send(ea, ServerMessaging.BuildErrorResponse("Bad request formatting.")); } try { Dictionary <string, object> response; string action = (string)json["action"]; if (action == "list") { response = ListItems(); } else { string productName = (string)json["product"]; int quantity = (int)(long)json["quantity"]; if (action == "reserve") { response = ReserveItem(quantity, productName); } else if (action == "release") { response = ReleaseItem(quantity, productName); } else { throw new Exception("Unhandled action."); } } serverRabbitMQ.Send(ea, response); } catch (Exception e) { serverRabbitMQ.Send(ea, ServerMessaging.BuildErrorResponse(e.Message)); } }); Console.WriteLine("Press any key to exit."); Console.ReadKey(); serverRabbitMQ.Close(); }
UserManager() { LoadUser(); var serverRabbitMQ = new ServerMessaging("localhost", "user_queue"); serverRabbitMQ.Listen((ea, json) => { if (json == null) { serverRabbitMQ.Send(ea, BuildErrorResponse("Bad request formatting.")); } try { string username = (string)json["Username"]; Dictionary <string, object> response; if (_user.ContainsKey(username)) { var user = _user[username]; response = new Dictionary <string, object> { { "user", JsonConvert.SerializeObject(user) } }; } else { response = BuildErrorResponse("Cannot find user."); } serverRabbitMQ.Send(ea, response); } catch (Exception e) { Console.WriteLine(e); serverRabbitMQ.Send(ea, BuildErrorResponse(e.Message)); } }); Console.WriteLine("Press any key to exit."); Console.ReadKey(); serverRabbitMQ.Close(); }
BillManager() { var serverRabbitMQ = new ServerMessaging("localhost", "bill_queue"); serverRabbitMQ.Listen((ea, requestBody) => { if (requestBody == null) { serverRabbitMQ.Send(ea, ServerMessaging.BuildErrorResponse("Bad request formatting.")); } try { var response = BuildBill(requestBody); serverRabbitMQ.Send(ea, response); } catch (Exception e) { serverRabbitMQ.Send(ea, ServerMessaging.BuildErrorResponse(e.Message)); } }); Console.WriteLine("Press any key to exit."); Console.ReadKey(); serverRabbitMQ.Close(); }