public void requestChat(ChatBuddy cb) { //set the client status is chatting so other invites will be rejected out right this._client._chatPending = true; //we now put a thread out there to send out the request ThreadPool.QueueUserWorkItem(new WaitCallback(this._crh.chatInvite), cb); }
public void removeClient(ClientHandler ch) { lock (this._clients) { //we tell all the clients that a new client has logged on to the server this._clients.Remove(ch.getClientID()); ChatBuddy newChatBuddy = new ChatBuddy(ch._client.getIP(), ch._client.getChatName(), RuntimeConstants.BUDDY_DISCONNECTING, ch._client.getID()); if (this._clients.Count > 0) { System.Collections.IDictionaryEnumerator ide = this._clients.GetEnumerator(); ide.MoveNext(); while (ide.Current != null) { //Logger.getLogger().log(Logger.DEBUG_PRIORITY, "What type of object is this: " + ide.Current.GetType()); ClientHandler tempCH = (ClientHandler)((DictionaryEntry)ide.Current).Value; ThreadPool.QueueUserWorkItem(new WaitCallback(tempCH._responseHandler.buddyUpdate), newChatBuddy); if (!ide.MoveNext()) { break; } } } // we add the client //we need to tell all the other clients that this person has left } ch._client._status = RuntimeConstants.DISCONNECTED; Logger.getLogger().log(Logger.INFO_PRIORITY, "The following client has left the chat: " + ch._client.ToString()); //System.GC.Collect(); }
public ClientHandler lookupClient(ChatBuddy client) { lock (this._clients) { return((ClientHandler)this._clients[client.getID()]); } }
public void buddyUpdate(object client) { ChatBuddy cb = (ChatBuddy)client; try { lock (this._sb) { this._sb.Remove(0, this._sb.Length); PVCTDMessage.chatBuddiesUpdate(this._xtw, this._ch._server.getServerID(), false, this._ch.getMessageID().ToString(), cb); string cmd = _sb.ToString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "The String to be sent: " + cmd); cmd += RuntimeConstants.XML_MESSAGE_END; byte[] sender = System.Text.Encoding.ASCII.GetBytes(cmd); lock (this._client) { this._client.Send(sender, sender.Length, 0); } } } catch (Exception eee) { Logger.getLogger().log(Logger.EXCEPTION_PRIORITY, "Problem sending.", eee); this._ch._client._status = RuntimeConstants.DISCONNECTED; } }
/// <summary> /// Called by the the listener. /// </summary> /// <param name="ch"></param> public void addClient(ClientHandler ch) { if (this._isrunning) { lock (this._clients) { //we tell all the clients that a new client has logged on to the server ChatBuddy newChatBuddy = new ChatBuddy(ch._client.getIP(), ch._client.getChatName(), RuntimeConstants.NEW_BUDDY, ch._client.getID()); if (this._clients.Count > 0) { System.Collections.IDictionaryEnumerator ide = this._clients.GetEnumerator(); ide.MoveNext(); while (ide.Current != null) { //Logger.getLogger().log(Logger.DEBUG_PRIORITY, "What type of object is this: " + ide.Current.GetType()); ClientHandler tempCH = (ClientHandler)((DictionaryEntry)ide.Current).Value; ThreadPool.QueueUserWorkItem(new WaitCallback(tempCH._responseHandler.buddyUpdate), newChatBuddy); if (!ide.MoveNext()) { break; } } } // we add the client this._clients.Add(ch.getClientID(), ch); } } else { Logger.getLogger().log(Logger.INFO_PRIORITY, "Server is not running"); } }
private void button1_Click(object sender, System.EventArgs e) { if (this.listBox1.SelectedItem == null) { System.Windows.Forms.MessageBox.Show("You must select a buddy to chat with.", "Error"); } else { this.timer1.Enabled = true; this.panel2.BringToFront(); this.panel2.Visible = true; //this.panel2.BringToFront(); ChatBuddy cb = (ChatBuddy)this.listBox1.SelectedItem; //if this returns true we are connected and the client should be moved to the other box this._clientHandler.requestChat(cb); } }
public void updateChatBuddy(ChatBuddy cb) { string status = cb.getStatus(); if (status.Equals(RuntimeConstants.CAN_CHAT) || status.Equals(RuntimeConstants.CANNOT_CHAT)) { System.Collections.IEnumerator ie = this._clientInterface.listBox1.Items.GetEnumerator(); ie.MoveNext(); while (ie.Current != null) { if (((ChatBuddy)ie.Current).getID().Equals(cb.getID())) { ((ChatBuddy)ie.Current).setStatus(status); break; } ie.MoveNext(); } } else if (status.Equals(RuntimeConstants.NEW_BUDDY)) { this._clientInterface.listBox1.Items.Add(cb); } else if (status.Equals(RuntimeConstants.BUDDY_DISCONNECTING)) { System.Collections.IEnumerator ie = this._clientInterface.listBox1.Items.GetEnumerator(); ie.MoveNext(); while (ie.Current != null) { if (((ChatBuddy)ie.Current).getID().Equals(cb.getID())) { this._clientInterface.listBox1.Items.Remove(ie.Current); break; } ie.MoveNext(); } } }
public ChatRequest(ChatBuddy chatBuddy, string chatID) { this._chatBuddy = chatBuddy; this._chatID = chatID; }
public static void chatBuddiesUpdate(XmlTextWriter xtw, string id, bool client, string messageID, ChatBuddy chatBuddy) { xtw.WriteStartDocument(); //open pvctd tag xtw.WriteStartElement("pvctd", "columbia-psl-memento-pervasive-hermes"); //open message element xtw.WriteStartElement("message"); //open connect element of message xtw.WriteStartElement("chatBuddiesUpdate"); xtw.WriteStartElement("chatBuddy"); xtw.WriteStartElement("ip"); xtw.WriteString(chatBuddy.getIP()); xtw.WriteEndElement(); xtw.WriteStartElement("chatName"); xtw.WriteString(chatBuddy.getChatName()); xtw.WriteEndElement(); xtw.WriteStartElement("status"); xtw.WriteString(chatBuddy.getStatus()); xtw.WriteEndElement(); xtw.WriteStartElement("clientID"); xtw.WriteString(chatBuddy.getID()); xtw.WriteEndElement(); xtw.WriteEndElement(); //********** //close xtw.WriteEndElement(); //close message xtw.WriteEndElement(); //open time element xtw.WriteStartElement("time"); xtw.WriteString(System.DateTime.Now.ToString()); xtw.WriteEndElement(); //open id xtw.WriteStartElement("id"); xtw.WriteString(id); xtw.WriteEndElement(); //clientID or serverID if (client) { xtw.WriteStartElement("clientID"); } else { xtw.WriteStartElement("serverID"); } xtw.WriteString(id); xtw.WriteEndElement(); //close it all up xtw.WriteEndElement(); xtw.WriteEndDocument(); xtw.Flush(); }
public static void confirmConnect(XmlTextWriter xtw, string id, bool client, string messageID, string clientID, ArrayList chatBuddies, string serverID) { xtw.WriteStartDocument(); //open pvctd tag xtw.WriteStartElement("pvctd", "columbia-psl-memento-pervasive-hermes"); //open message element xtw.WriteStartElement("message"); //open connect element of message xtw.WriteStartElement("confirmConnect"); //open and close all your internal elements here xtw.WriteStartElement("clientID"); xtw.WriteString(clientID); xtw.WriteEndElement(); xtw.WriteStartElement("chatBuddies"); //we add a chat buddy for each one in the list of chat buddies object[] buddies = chatBuddies.ToArray(); xtw.WriteAttributeString("chatBuddiesCount", buddies.Length.ToString()); //xtw.WriteAttributeString("chatBuddies", "buddyCount", "columbia-psl-memento-pervasive-hermes", buddies.Length.ToString()); //xtw.WriteEndAttribute(); if (buddies.Length > 0) { for (int i = 0; i < buddies.Length; i++) { ChatBuddy chatBuddy = (ChatBuddy)buddies[i]; xtw.WriteStartElement("chatBuddy"); xtw.WriteStartElement("ip"); xtw.WriteString(chatBuddy.getIP()); xtw.WriteEndElement(); xtw.WriteStartElement("chatName"); xtw.WriteString(chatBuddy.getChatName()); xtw.WriteEndElement(); xtw.WriteStartElement("status"); xtw.WriteString(chatBuddy.getStatus()); xtw.WriteEndElement(); xtw.WriteStartElement("clientID"); xtw.WriteString(chatBuddy.getID()); xtw.WriteEndElement(); xtw.WriteEndElement(); } } xtw.WriteEndElement(); //serverID element xtw.WriteStartElement("serverID"); xtw.WriteString(serverID); xtw.WriteEndElement(); //********** //close xtw.WriteEndElement(); //close message xtw.WriteEndElement(); //open time element xtw.WriteStartElement("time"); xtw.WriteString(System.DateTime.Now.ToString()); xtw.WriteEndElement(); //open id xtw.WriteStartElement("id"); xtw.WriteString(id); xtw.WriteEndElement(); //clientID or serverID if (client) { xtw.WriteStartElement("clientID"); } else { xtw.WriteStartElement("serverID"); } xtw.WriteString(id); xtw.WriteEndElement(); //close it all up xtw.WriteEndElement(); xtw.WriteEndDocument(); xtw.Flush(); }
//Start listening to incoming messages public void start() { try { string clientmessage = null; bool rcount = false; Logger.getLogger().log(Logger.DEBUG_PRIORITY, "We have started this thread up and are ready to listen for things coming in."); while (this._server.Connected) { clientmessage = null; Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Getting a message."); rcount = this._xsp.getXMLMessage(ref clientmessage); if (rcount) { try { Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is what they said: " + clientmessage + " and length : " + rcount); this._xmlReader = new XmlTextReader(new StringReader(clientmessage)); this._xmlReader.ReadStartElement(); this._xmlReader.ReadStartElement(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current node:" + this._xmlReader.Name); //we read off the type of message and go through the if else to find out what message //we are getting from the client if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CONFIRM_CONNECT) { this._ch.updateStatusBar("Connected."); this._ch._client.setClientID(this._xmlReader.ReadElementString()); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element with attribute:" + this._xmlReader.Name); int chatBuddiesCount = System.Int16.Parse(this._xmlReader.GetAttribute(0)); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is what we got for our chatBuddiesCount:" + chatBuddiesCount); ChatBuddy[] buddies = new ChatBuddy[chatBuddiesCount]; if (chatBuddiesCount > 0) { string ip = null; string chatName = null; string status = null; string clientID = null; this._xmlReader.ReadStartElement(); //this._xmlReader.ReadStartElement(); for (int i = 0; i < chatBuddiesCount; i++) { //read the chatBuddy //this._xmlReader.ReadStartElement(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of " + i + " value buddy " + this._xmlReader.Name); //read the ip Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of " + i + " value ip" + this._xmlReader.Name); ip = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of " + i + " value chatName " + this._xmlReader.Name); chatName = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of " + i + " value status " + this._xmlReader.Name); status = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of " + i + " value clientID " + this._xmlReader.Name); clientID = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is my problem." + i); buddies[i] = new ChatBuddy(ip, chatName, status, clientID); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is my problem." + i); this._xmlReader.ReadEndElement(); } } this._ch.populateChatBuddies(buddies); } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DISCONNECT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHAT_INVITE) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CONFIRM_CHAT_INVITE) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_REQUEST_CHAT) { //if a chat is pending or the client is in a chat string ip; string chatName; string status; string id; string chatID; //no we go ahead and parse and make the request this._xmlReader.ReadStartElement(); this._xmlReader.ReadStartElement(); ip = this._xmlReader.ReadElementString(); chatName = this._xmlReader.ReadElementString(); status = this._xmlReader.ReadElementString(); id = this._xmlReader.ReadElementString(); this._xmlReader.ReadEndElement(); chatID = this._xmlReader.ReadElementString(); ChatRequest chatRequest = new ChatRequest(new ChatBuddy(ip, chatName, status, id), chatID); if (this._ch._client._chatPending || this._ch._client._isChatting) { ThreadPool.QueueUserWorkItem(new WaitCallback(this._ch._crh.chatReject), "Currently in Chat."); } else { ThreadPool.QueueUserWorkItem(new WaitCallback(this._ch._clientInterface.incomingChatInvite), chatRequest); } } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHAT_INVITE_REJECT) { string reason; //this._xmlReader.ReadStartElement(); this._xmlReader.ReadStartElement(); reason = this._xmlReader.ReadElementString(); this._ch._client._chatPending = false; ThreadPool.QueueUserWorkItem(new WaitCallback(this._ch._clientInterface.chatInviteReject), reason); } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_ACCEPT_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DECLINE_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_EXIT_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHAT_UPDATE) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CLOSE_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHATTER) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DATA) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DECODED_CHATTER) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHAT_BUDDIES_UPDATE) { string ip = null; string chatName = null; string status = null; string clientID = null; this._xmlReader.ReadStartElement(); //this._xmlReader.ReadStartElement(); //read the chatBuddy //this._xmlReader.ReadStartElement(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of value buddy " + this._xmlReader.Name); //read the ip Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of value ip" + this._xmlReader.Name); ip = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of value chatName " + this._xmlReader.Name); chatName = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of value status " + this._xmlReader.Name); status = this._xmlReader.ReadElementString(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the current element of value clientID " + this._xmlReader.Name); clientID = this._xmlReader.ReadElementString(); ChatBuddy cb = new ChatBuddy(ip, chatName, status, clientID); //check what we should do with our list of peeps this._ch.updateChatBuddy(cb); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "And we keep going."); } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_ERROR) { } //xml message is not recognized else { } } catch (System.Xml.XmlException xmlE) { Logger.getLogger().log(Logger.EXCEPTION_PRIORITY, "Could not parse last message", xmlE); //send out error xml here } } else { break; } } } catch (Exception eeee) { Logger.getLogger().log(Logger.EXCEPTION_PRIORITY, "Stream is now closed.", eeee); } }
public void start() { //begin decoding try { string clientmessage = null; bool rcount = false; //Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Check for client stats and socket info: " + this._handler._client._status + // " and " + this._clientsock.Connected); #region Decoding and Action Loop while (this._clientsock.Connected && (this._handler._client._status == RuntimeConstants.PENDING || this._handler._client._status == RuntimeConstants.CONNECTED)) { // Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is the status of our connection: " + this._clientsock.Connected); clientmessage = null; rcount = this._xsp.getXMLMessage(ref clientmessage); //rcount = this._clientsock.Receive(recs,recs.Length,0); if (rcount) { try { //clientmessage = System.Text.Encoding.ASCII.GetString(recs) ; //clientmessage = clientmessage.Substring(0,rcount); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "Here is what they said: " + clientmessage + " and length : " + rcount); this._xmlReader = new XmlTextReader(new StringReader(clientmessage)); this._xmlReader.ReadStartElement(); this._xmlReader.ReadStartElement(); Logger.getLogger().log(Logger.DEBUG_PRIORITY, "next node: " + this._xmlReader.Name); /* * * Logger.getLogger().log(Logger.DEBUG_PRIORITY, "loaded xml reader: " + this._xmlReader.Name); * * Logger.getLogger().log(Logger.DEBUG_PRIORITY, "next node: " + this._xmlReader.Name); */ //this._xmlReader.Name //the current xmlReader.Name is the message name we care about so now we do our if else statements //the client is requesting a connect here if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CONNECT) { //first we check if the client is in the pending connect state //if not pending then this request should not be honored if (!this._handler._client._status.Equals(RuntimeConstants.PENDING)) { Logger.getLogger().log(Logger.INFO_PRIORITY, "Client - " + this._handler._client.getChatName() + "@" + this._handler._client.getIP() + " send connect request with client status set to: " + this._handler._client._status); } //we handle all the connect stuff else { //we need to pull all the info off the message //ip address this._xmlReader.ReadStartElement(); this._handler._client.setIP(this._xmlReader.ReadElementString()); this._handler._client.setChatName(this._xmlReader.ReadElementString()); //now we need to assign the client a unique ID, add client to the hashtable //of connected clients and return a connection element. this._handler._client.setID(this._handler._server.getClientID()); this._handler._client._status = RuntimeConstants.CONNECTED; this._handler._server.addClient(this._handler); Logger.getLogger().log(Logger.DEBUG_PRIORITY, this._handler._client.ToString()); ThreadPool.QueueUserWorkItem(new WaitCallback(this._handler._responseHandler.connectConfirm)); } } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CONFIRM_CONNECT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DISCONNECT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHAT_INVITE) { //the client wants to chat with someone if (this._handler._client._status.Equals(RuntimeConstants.PENDING)) { Logger.getLogger().log(Logger.INFO_PRIORITY, "Client must send connection first."); // SHOULD SEND ERROR MESSAGE BACK TO CLIENT } else { string ip; string chatName; string status; string id; //no we go ahead and parse and make the request this._xmlReader.ReadStartElement(); this._xmlReader.ReadStartElement(); ip = this._xmlReader.ReadElementString(); chatName = this._xmlReader.ReadElementString(); status = this._xmlReader.ReadElementString(); id = this._xmlReader.ReadElementString(); ChatBuddy cb = new ChatBuddy(ip, chatName, status, id); ChatRequest cr = new ChatRequest(this._handler, this._handler._server.lookupClient(cb), this._handler._server.getChatID()); //now that we have the buddy they want to chat with let's make the right call ThreadPool.QueueUserWorkItem(new WaitCallback(this._handler._server._chatRequestHandler.chatRequest), cr); } } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CONFIRM_CHAT_INVITE) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_REQUEST_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_ACCEPT_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DECLINE_CHAT) { //decline chat we now need to tell the sorry sucker who wanted to //chat with this buddy and remove the ChatRequest from the pending chat requests //hashtable in the ChatRequestHandler //we let the ChatRequestHandler take care of all of this string chatID; //decode the chatID //this._xmlReader.ReadStartElement(); chatID = this._xmlReader.ReadElementString(); ThreadPool.QueueUserWorkItem(new WaitCallback(this._handler._server._chatRequestHandler.chatRequestReject), chatID); } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_EXIT_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHAT_UPDATE) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CLOSE_CHAT) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_CHATTER) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DATA) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_DECODED_CHATTER) { } else if (this._xmlReader.Name == RuntimeConstants.XML_MESSAGE_ERROR) { } //xml message is not recognized else { } } catch (System.Xml.XmlException xmlE) { Logger.getLogger().log(Logger.EXCEPTION_PRIORITY, "Could not parse last message", xmlE); //send out error xml here } } else { break; } } //while #endregion } catch (System.Exception eee) { Logger.getLogger().log(Logger.EXCEPTION_PRIORITY, "Unexpected exception: " + eee.Message, eee); } finally { this.cleanup(); } }