static void Main(string[] args) { var cluster = new LockClusterManager(); cluster.StartControlNodes(3).ConfigureAwait(false).GetAwaiter().GetResult(); // while (true) { Console.ReadLine(); } while (true) { Console.WriteLine("before lock operation,press enter"); Console.ReadLine(); LockOper op = new LockOper() { Key = "key", Oper = "lock", Session = "session1" }; cluster.BeginRequest(op); } Console.ReadLine(); }
async Task Process(IChannelHandlerContext ctx, IHttpRequest request) { string uri = request.Uri; switch (uri) { case "/lock": LockOper op2 = new LockOper() { Key = "key", Oper = "lock", Session = "session1" }; var ack1 = Task.Run(async() => { return(await this.BeginRequest(op2)); }).GetAwaiter().GetResult(); //byte[] json2 = Encoding.UTF8.GetBytes(NewMessage2().ToJsonFormat()); //this.WriteResponse(ctx, Unpooled.WrappedBuffer(json2), TypeJson, JsonClheaderValue); string str = "null"; if (ack1 != null) { str = Newtonsoft.Json.JsonConvert.SerializeObject(ack1); } byte[] json2 = Encoding.UTF8.GetBytes(new MessageBody(str).ToJsonFormat()); var length = json2.Length; this.WriteResponse(ctx, Unpooled.WrappedBuffer(json2), TypeJson, AsciiString.Cached($"{length}")); break; case "/plaintext": LockOper op = new LockOper() { Key = "key", Oper = "lock", Session = "session1" }; var ack = Task.Run(async() => { return(await this.BeginRequest(op)); }).GetAwaiter().GetResult(); this.WriteResponse(ctx, PlaintextContentBuffer.Duplicate(), TypePlain, PlaintextClheaderValue); break; case "/json": byte[] json = Encoding.UTF8.GetBytes(NewMessage().ToJsonFormat()); this.WriteResponse(ctx, Unpooled.WrappedBuffer(json), TypeJson, JsonClheaderValue); break; default: var response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.NotFound, Unpooled.Empty, false); await ctx.WriteAndFlushAsync(response); await ctx.CloseAsync(); break; } }
public async Task <object> BeginRequest(LockOper command) { string data = Newtonsoft.Json.JsonConvert.SerializeObject(command); var node = this.node; //Console.WriteLine("start lock oper" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond); var result = await Task.Run(async() => { var result = await this.node.AddLogEntryRequestAsync(System.Text.Encoding.UTF8.GetBytes(data)).ConfigureAwait(false); return(result); } ); // Console.WriteLine("await finished" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond); return(result); }