private void button1_Click(object sender, EventArgs e) { DisableConfiguration(); var ContinueRegistration = true; var ContinueRegistrationYetFail = false; //AppendTextLine("Registering " + textBox3.Text + " as " + ); // start web server var vsr = new VirtualServerRack { Ports = PortTextbox.Text.ToInt32Array(), }; vsr.CommandRequest += (s, path) => { var sendname = new sendname(); var findname = new findname(); var asknames = new asknames(); var sendmessage = new sendmessage(); findname.BeforeInvoke = delegate { // let the discovery service know // that somebody wants that name // if we do we should let the poor sap know about it! if (findname.name == this.NicknameTextbox.Text) { this.AppendTextLine("Thats my name! I claimed it first!"); SendCommandSendName(findname); } else { var IsUnknown = true; foreach (var k in CurrentConfiguration) { if (k.Name == findname.name) { IsUnknown = false; if (k.Target == findname.myip) { this.AppendTextLine("Records confirm " + findname.name); } else { this.AppendTextLine("We seem to know about " + findname.name); } } } if (IsUnknown) { this.AppendTextLine("Maybe others know about " + findname.name); SetRequester(findname); // ask others! } } }; sendname.BeforeInvoke = delegate { if (this.NicknameTextbox.Text == sendname.name && this.RegisteringTimer.Enabled) { ContinueRegistrationYetFail = true; AppendTextLine("Name already taken!"); return; } UpdateConfiguration(sendname); }; #region asknames // http://localhost:6666/chat/asknames asknames.BeforeInvoke = delegate { s.WriteWebContent( GetCurrentConfigurationString(), "text/plain" ); }; #endregion #region sendmessage // http://localhost:6666/chat/sendmessage?message=hi sendmessage.BeforeInvoke = delegate { Say(sendmessage); s.WriteWebContent("Thank you!"); }; #endregion path.Chop("/chat").GetArguments().AsParametersTo( sendname.Invoke, findname.Invoke, asknames.Invoke, sendmessage.Invoke ); }; vsr.Start(); this.Stop += delegate { if (vsr.Stop == null) return; vsr.Stop(); }; // populate network // claim the name label6.Show(); RegisteringTimer.Start(); // we have alloted ourselves some time // we should now go ahead and see if anyone has this name foreach (var k in CurrentConfiguration) { SendCommandFindName(k, NicknameTextbox.Text); } RegisteringTimer.Tick += delegate { if (!ContinueRegistration) return; ContinueRegistration = false; if (ContinueRegistrationYetFail) { AppendTextLine("Go ahead and try another name and port :)"); RegistrationFail(); return; } AppendTextLine("Your name has been registered!"); RegistrationReady(); foreach (var k in CurrentConfiguration) { SendCommandSendName(k, NicknameTextbox.Text); } }; }
public void PollerGotDataLine(string path, string myname) { var sendname = new sendname(); var findname = new findname(); var asknames = new asknames(); var sendmessage = new sendmessage(); findname.BeforeInvoke = delegate { // let the discovery service know // that somebody wants that name // if we do we should let the poor sap know about it! if (findname.name == this.Nickname) { if (findname.myname == this.Pseudoname) { // we swallow our own question! } else { this.AppendTextLine(findname.name + " is registered to me!"); ahmanize( new ChatRequest.Requests.sendname( // to whom? findname.myname, // from whom? Nickname, // name we know about Nickname, "0" ) ); } return; } var r = default(FriendStatus); foreach (FriendStatus item in this.FriendStatusList) { if (item.Name == sendmessage.myname) r = item; } if (r != null) { if (r.IsOnline) { this.AppendTextLine(findname.name + " is registered to a friend!"); ahmanize( new ChatRequest.Requests.sendname( // to whom? findname.myname, // from whom? r.Name, // name we know about r.Name, "0" ) ); } } }; sendname.BeforeInvoke = delegate { if (sendname.name == Nickname) { this.AppendTextLine("*** Nickname " + Nickname + " already taken! Better luck next time!"); this.RegistrationTimeout.Enabled = false; this.Poller.Enabled = false; return; } }; sendmessage.BeforeInvoke = delegate { EncodedMessage m = sendmessage.message; m.Sender = sendmessage.myname; if (m.Message == Message_SeeYouLater) { this.AppendTextLine("*** " + m.Sender + " has left the chat!"); var r = default(FriendStatus); foreach (FriendStatus item in this.FriendStatusList) { if (item.Name == sendmessage.myname) r = item; } if (r != null) this.FriendStatusList.Remove(r); return; } if (m.Message == Message_Ping) { var x = new EncodedMessage { Message = Message_Pong }; ahmanize( new ChatRequest.Requests.sendmessage( sendmessage.myname, Nickname, "0", x.ToString(), "100" ) ); var r = default(FriendStatus); foreach (FriendStatus item in this.FriendStatusList) { if (item.Name == sendmessage.myname) r = item; } if (r == null) this.FriendStatusList.Add(new FriendStatus { Name = sendmessage.myname }); return; } if (m.Message == Message_Pong) { foreach (FriendStatus item in this.FriendStatusList) { if (item.Name == sendmessage.myname) item.LastSeen = DateTime.Now.Ticks; } return; } if (m.Message == Message_Catchup) { this.AppendTextLine("*** " + m.Sender + " wants to catch up..."); foreach (EncodedMessage mm in this.Messages) { var xx = new EncodedMessage { Time = mm.Time, Sender = Nickname, Message = mm.Sender + " said " + mm.Message }; BroadcastMessage(xx, new[] { m.Sender }); } return; } AppendMessage(m); }; path.Chop("/chat").GetArguments().AsParametersTo( sendname.Invoke, findname.Invoke, asknames.Invoke, sendmessage.Invoke ); }