コード例 #1
0
 public void notifyListeners(Message msg)
 {
     for (int i=0; i<listeners.Count; i++ )
     {
         ((ClientConnection)listeners[i]).sendMessage(msg);
     }
 }
コード例 #2
0
 public void checkMessage(Message msg)
 {
     mutex.WaitOne();
     sendLocalMessage(msg.From, msg);
     sendLocalMessage(msg.To, msg);
     sendGlobalMessage(msg);
     mutex.ReleaseMutex();
 }
コード例 #3
0
 public Message(Message m)
 {
     this.from = m.from;
     this.to = m.to;
     this.thread = m.thread;
     this.subject = m.subject;
     this.body = m.body;
 }
コード例 #4
0
 public void addMessage(Message msg)
 {
     if (keepRunning)
     {
         if (stack.Count >= maxSendQueueSize)
         {
             Context.getInstance().getLogger().log("Max Send Queue Size exceeded", name, Logger.LEVEL_DEBUG);
         }
         else
         {
             stack.Enqueue(msg);
         }
     }
 }
コード例 #5
0
 private void sendLocalMessage(string to, Message msg)
 {
     ArrayList list = (ArrayList)eavesDroppers[to];
     if (list != null)
     {
         IEnumerator e = list.GetEnumerator();
         while (e.MoveNext())
         {
             ((ClientConnection)e.Current).sendMessage(msg);
         }
     }
 }
コード例 #6
0
 private void sendGlobalMessage(Message msg)
 {
     IEnumerator e = globalEavesDroppers.GetEnumerator();
     while (e.MoveNext())
     {
         ((ClientConnection)e.Current).sendMessage(msg);
     }
 }
コード例 #7
0
 public void deregisterClient()
 {
     Context.getInstance().getConnectionRegistry().deregisterConnection(this.name);
     //notify listeners
     Message msg = new Message();
     msg.Subject = "offline";
     msg.Body = name;
     Context.getInstance().getListenerRegistry().notifyListeners(msg);
 }
コード例 #8
0
 private void routeMessage(Message msg)
 {
     ClientConnection targetConnection = Context.getInstance().getConnectionRegistry().findConnection(msg.To);
     if (Context.getInstance().isEavesDroppingEnabled())
     {
         Context.getInstance().getEavesDropRegistry().checkMessage(msg);
     }
     if (targetConnection != null)
     {
         if (routingProfileMap[msg.To] != null)
         {
             routingProfileMap[msg.To] = (int)(routingProfileMap[msg.To]) + 1;
         }
         else
         {
             routingProfileMap[msg.To] = 1;
         }
         if (Context.getInstance().getSyncSend())
         {
             targetConnection.sendMessageNow(msg);
         }
         else
         {
             targetConnection.sendMessage(msg);
         }
     }
     else
     {
         Message reply = new Message();
         reply.Subject = "ERROR";
         reply.To = msg.From;
         reply.Body = "Unknown client: " + msg.To;
         sendMessage(reply);
     }
 }
コード例 #9
0
 private void registerClient(string name)
 {
     this.name = name;
     sender.Name = name;
     Context.getInstance().getConnectionRegistry().registerConnection(this, name);
     //notify any listeners of this registration
     Message msg = new Message();
     msg.Subject = "online";
     msg.Body = name;
     Context.getInstance().getListenerRegistry().notifyListeners(msg);
 }
コード例 #10
0
 private bool processMessage(Message msg)
 {
     ServerStats.getInstance().incrementIncomingMsgCount();
     incomingMessageCount++;
     if ((msg.To != null) && (msg.To != ""))
     {
         Context.getInstance().getLogger().log(msg.From, msg.To, msg.Subject, msg.Body, Logger.LEVEL_INFO);
         routeMessage(msg);
         return true;
     }
     else
     {
         return handleMessage(msg);
     }
 }
コード例 #11
0
        private bool handleMessage(Message msg)
        {
            string subject = msg.Subject;
            Context.getInstance().getLogger().log(msg.From, "server", msg.Subject, msg.Body, Logger.LEVEL_INFO);
            if (Context.getInstance().isEavesDroppingEnabled()) {
                 Context.getInstance().getEavesDropRegistry().checkMessage(msg);
            }
            try
            {
                Message reply = new Message();
                reply.Thread = msg.Thread;
                reply.To = msg.From;

                if (subject == "connect")
                {  //handle the connect request
                    //check for an existing connection
                    if (Context.getInstance().getConnectionRegistry().findConnection(msg.Body) != null)
                    {
                        if (!Context.getInstance().getAllowDuplucateConnections())
                        {
                            reply.Subject = "ERROR";
                            reply.Body = "duplicate registrations not allowed";
                            Context.getInstance().getLogger().log("Attempt to login under duplicate user name", msg.Body, Logger.LEVEL_WARN);
                            sendMessageNow(reply);
                            return false;
                        }
                        else
                        { //close the original connection and process the new request
                            ClientConnection cc = Context.getInstance().getConnectionRegistry().findConnection(msg.Body);
                            Context.getInstance().getLogger().log("Duplicate connection request...closing original connection.", msg.Body, Logger.LEVEL_WARN);
                            cc.closeNow();
                        }
                        reply.Subject = "connected";
                        reply.To = msg.Body;
                        registerClient(msg.Body);
                    }
                    else
                    {
                        reply.Subject = "connected";
                        reply.To = msg.Body;
                        registerClient(msg.Body);
                    }
                }
                else if (subject == "disconnect")
                {
                    return false;
                }
                else if (subject == "list")
                {  //handle the list request
                    reply.Subject = "list";
                    string list = Context.getInstance().getConnectionRegistry().listConnections();
                    Context.getInstance().getLogger().log("Current List", list, Logger.LEVEL_INFO);
                    reply.Body = list;
                }
                else if (subject == "register")
                {
                    Context.getInstance().getListenerRegistry().registerListener(this);
                    reply.Subject = "registered";
                }
                else if (subject == "deregister")
                {
                    Context.getInstance().getListenerRegistry().deregisterListener(this);
                    reply.Subject = "deregistered";
                }
                else if (subject == "version")
                {
                    reply.Subject = "version";
                    reply.Body = VERSION;
                }
                else if (subject == "eavesdrop")
                {
                    if (Context.getInstance().isEavesDroppingEnabled())
                    {
                        Context.getInstance().getEavesDropRegistry().registerEavesDropper(msg.Body, this);
                        reply.Subject = "eavesdrop enabled";
                        reply.Body = msg.Body;
                    }
                }
                else if (subject == "globaleavesdrop")
                {
                    if (Context.getInstance().isEavesDroppingEnabled())
                    {
                        Context.getInstance().getEavesDropRegistry().registerGlobalEavesDropper(this);
                        reply.Subject = "globaleavesdrop enabled";
                    }
                }
                else if (subject == "uneavesdrop")
                {
                    if (Context.getInstance().isEavesDroppingEnabled())
                    {
                        Context.getInstance().getEavesDropRegistry().deregisterEavesDropper(msg.Body, this);
                        reply.Subject = "eavesdrop disabled";
                        reply.Body = msg.Body;
                    }
                }
                else if(subject == "uneavesdropall")
                {
                    if (Context.getInstance().isEavesDroppingEnabled())
                    {
                        Context.getInstance().getEavesDropRegistry().deregisterAllEavesDroppers(this);
                        reply.Subject = "all eavesdroppers disabled";
                    }
                }
                else if (subject == "unglobaleavesdrop")
                {
                    if (Context.getInstance().isEavesDroppingEnabled())
                    {
                        Context.getInstance().getEavesDropRegistry().deregisterGlobalEavesDropper(this);
                        reply.Subject = "globaleavesdrop disabled";
                    }
                }
                else if (subject == "enable eavesdropping")
                {
                    Context.getInstance().enableEavesDropping();
                    reply.Subject = "eavesdropping enabled";
                }
                else if (subject == "disable eavesdropping")
                {
                    Context.getInstance().disableEavesdropping();
                    reply.Subject = "eavesdropping disabled";
                }
                else if (subject == "enable error messages")
                {
                    Context.getInstance().enableErrorMessages();
                    reply.Subject = "error messages enabled";
                }
                else if (subject == "disable error messages")
                {
                    Context.getInstance().disableErrorMessages();
                    reply.Subject = "error messages disabled";
                }
                else if (subject == "get stats")
                {
                    reply.Subject = "stats";
                    reply.Body = ServerStats.getInstance().getStatsStr();
                }
                else if (subject == "reset stats")
                {
                    ServerStats.getInstance().resetStats();
                    reply.Subject = "stats reset";
                }
                else if (subject == "kill connection")
                {
                    reply.Body = msg.Body;
                    Context.getInstance().getLogger().log(msg.From, "SERVER", "Request to kill connection", msg.Body, Logger.LEVEL_DEBUG);
                    ClientConnection cc = Context.getInstance().getConnectionRegistry().findConnection(msg.Body);
                    if (cc != null)
                    {
                        cc.closeNow();
                        reply.Subject = "connection killed";
                    }
                    else
                    {
                        reply.Subject = "unable to kill connection";
                    }
                }
                else if (subject.StartsWith("set log level"))
                {
                    if (subject.IndexOf("info") >= 0)
                    {
                        Context.getInstance().getLogger().CurrentLevel = Logger.LEVEL_INFO;
                        reply.Subject = "log level set to INFO";
                    }
                    else if (subject.IndexOf("warn") >= 0)
                    {
                        Context.getInstance().getLogger().CurrentLevel = Logger.LEVEL_WARN;
                        reply.Subject = "log level set to WARN";
                    }
                    else if (subject.IndexOf("debug") >= 0)
                    {
                        Context.getInstance().getLogger().CurrentLevel = Logger.LEVEL_DEBUG;
                        reply.Subject = "log level set to DEBUG";
                    }
                    else if (subject.IndexOf("shout") >= 0)
                    {
                        Context.getInstance().getLogger().CurrentLevel = Logger.LEVEL_SHOUT;
                        reply.Subject = "log level set to SHOUT";
                    }
                    else
                    {
                        reply.Subject = "unknown log level";
                    }
                    reply.To = msg.From;
                }
                else if (subject == "enable logging")
                {
                    Context.getInstance().getLogger().enable();
                    reply.Subject = "logging enabled";
                }
                else if (subject == "disable logging")
                {
                    Context.getInstance().getLogger().disable();
                    reply.Subject = "logging disabled";
                }
                else if (subject == "help")
                {
                    reply.Subject = "Command List";
                    reply.Body = CMD_LIST;
                }
                else if (subject == "get connection profile")
                {
                    reply.Subject = "connection profile";
                    reply.Body = Context.getInstance().getConnectionRegistry().getConnectionProfiles();
                }
                else if (subject == "get connection stats")
                {
                    reply.Subject = "connection stats";
                    reply.Body = Context.getInstance().getConnectionRegistry().getConnectionStatsStr();
                }
                else if (subject == "get send queue stats")
                {
                    reply.Subject = "send queue stats";
                    reply.Body = Context.getInstance().getConnectionRegistry().getSendQueueStats();
                }
                else if (subject == "get max send queue size")
                {
                    reply.Body = "Current max send queue size: " + Context.getInstance().getMaxSendQueueSize();
                }
                else if (subject == "set max send queue size")
                {
                    int val = Int32.Parse(msg.Body);
                    if (val > 0)
                    {
                        reply.Subject = "Max send queue size changed from " + Context.getInstance().getMaxSendQueueSize() + " to " + val;
                        Context.getInstance().setMaxSendQueueSize(val);
                    }
                    else
                    {
                        reply.Subject = "set max send queue size";
                        reply.Body = "invalid value specified";
                    }
                }
                else if (subject == "log to file")
                {
                    Context.getInstance().logToFile();
                    reply.Subject = "logging to file";
                }
                else if (subject == "log to stdout")
                {
                    Context.getInstance().logToStdOut();
                    reply.Subject = "logging to stdout";
                }
                else if (subject == "get sync send status")
                {
                    reply.Subject = "sync send status";
                    if (Context.getInstance().getSyncSend())
                    {
                        reply.Body = "enabled";
                    }
                    else
                    {
                        reply.Body = "disabled";
                    }
                }
                else if (subject == "enable sync send")
                {
                    Context.getInstance().setSyncSend(true);
                    reply.Subject = "sync send enabled";
                }
                else if (subject == "disable sync send")
                {
                    Context.getInstance().setSyncSend(false);
                    reply.Subject = "sync send disabled";
                }
                else if (subject == "get stat logging")
                {
                    reply.Subject = "stat logging status";
                    if (Context.getInstance().getStatLogging())
                    {
                        reply.Body = "enabled";
                    }
                    else
                    {
                        reply.Body = "disabled";
                    }
                }
                else if (subject == "enable stat logging")
                {
                    Context.getInstance().enableStatLogging();
                    reply.Subject = "stat logging enabled";
                }
                else if (subject == "disable stat logging")
                {
                    Context.getInstance().disableStatLogging();
                    reply.Subject = "stat logging disabled";
                }
                else
                {
                    if (Context.getInstance().errorMessageEnabled()) //check if error messaging is enabled
                    {
                        //send an error reply
                        reply.To = msg.From;
                        reply.Subject = "ERROR";
                        reply.Body = "Unknown command";
                    }
                    else  //if not then just return
                    {
                        return true;
                    }
                }
                sendMessage(reply);

            }
            catch (Exception ex)
            {
                Context.getInstance().getLogger().forceLog(ex.Message);
            }
            return true;
        }
コード例 #12
0
        private Message getMessage()
        {
            byte[] packet_header = new byte[PACKET_HEADER_SIZE];

            getHeaderData(ref packet_header, PACKET_HEADER_SIZE);

            //examine packet header to determine total message length
            byte toLength = packet_header[0];
            byte fromLength = packet_header[1];
            byte threadLength = packet_header[2];
            byte subjectLength = packet_header[3];
            long bodyLength =0;
            bodyLength |= packet_header[4];
            bodyLength <<= 8;
            bodyLength |= packet_header[5];
            bodyLength <<= 8;
            bodyLength |= packet_header[6];
            bodyLength <<= 8;
            bodyLength |= packet_header[7];
            long totalLength = toLength+fromLength+threadLength+subjectLength+bodyLength;
            packetData = new byte[totalLength];

            getData(ref packetData, totalLength);

            //now construct the Message object
            Message msg = new Message();
            int index = 0;
            string messagedata = Encoding.ASCII.GetString(packetData);
            if (toLength != 0)
            {
                msg.To = messagedata.Substring(0, toLength);
                index += toLength;
            }
            if (fromLength != 0)
            {
                msg.From = messagedata.Substring(index, fromLength);
                index += fromLength;
            }
            if (threadLength != 0)
            {
                msg.Thread = messagedata.Substring(index, threadLength);
                index += threadLength;
            }
            if (subjectLength != 0)
            {
                msg.Subject = messagedata.Substring(index, subjectLength);
                index += subjectLength;
            }
            if (bodyLength != 0)
            {
                msg.Body = messagedata.Substring(index, (int)bodyLength);
            }

            return msg;
        }
コード例 #13
0
 public void sendMessageNow(Message msg)
 {
     ServerStats.getInstance().incrementOutgoingMsgCount();
     outgoingMessageCount++;
     sender.sendMessage(msg);
 }
コード例 #14
0
        public Queue getMessages()
        {
            byte[] readbuffer = new byte[MAX_BUF_SIZE];
            Queue messages = new Queue();
            //we want to fill the packetBuffer with as much data as we can get
            long actualSize = ss.Receive(readbuffer,0, MAX_BUF_SIZE, SocketFlags.None);
            long residualPacketLength = packetBufferSize-packetBufferPos;
            byte[] tmpBuf = new byte[actualSize + residualPacketLength];  //create a temporary buffer to hold the data;
            //since the tmpBuf will become the packetBuffer, set the size now
            packetBufferSize = actualSize + residualPacketLength;
            //pack the residual data from the packet buffer into the tmp buffer
            pack(packetBuffer, (int)packetBufferPos, (int)residualPacketLength, ref tmpBuf, 0);
            //pack the newly read data into the tmp buffer
            pack(readbuffer, 0, (int)actualSize, ref tmpBuf, (int)residualPacketLength);
            packetBuffer = tmpBuf;  //set the packet buffer to the tmp buffer

            packetBufferPos = 0;  //set the packetBufferPos to the beginning of the buffer
            //if there aren't even 8 bytes in the buffer there's not even enough for a header so
            //there's no use in parsing it yet
            if (packetBufferSize < PACKET_HEADER_SIZE)
            {
                return messages;
            }

            //now parse through the packet buffer to pull out as many messages as possible
            while ((packetBufferPos+PACKET_HEADER_SIZE) < packetBufferSize)
            { //make sure there's enough room for the next packet header
                //process the current packet header
                byte toLength = packetBuffer[packetBufferPos]; packetBufferPos++;
                byte fromLength = packetBuffer[packetBufferPos]; packetBufferPos++;
                byte threadLength = packetBuffer[packetBufferPos]; packetBufferPos++;
                byte subjectLength = packetBuffer[packetBufferPos]; packetBufferPos++;
                long bodyLength =0;
                bodyLength |= packetBuffer[packetBufferPos]; packetBufferPos++;
                bodyLength <<= 8;
                bodyLength |= packetBuffer[packetBufferPos]; packetBufferPos++;
                bodyLength <<= 8;
                bodyLength |= packetBuffer[packetBufferPos]; packetBufferPos++;
                bodyLength <<= 8;
                bodyLength |= packetBuffer[packetBufferPos]; packetBufferPos++;
                long totalLength = toLength+fromLength+threadLength+subjectLength+bodyLength;
                //check to make sure the remainder of the packetBuffer fully contains this packet
                if ((packetBufferSize-packetBufferPos) < totalLength)
                {
                    packetBufferPos -= PACKET_HEADER_SIZE; //step back to tbe beginning of this packet
                    return messages;  //return the current list of messages
                }
                //otherwise, we need to constuct the Message
                Message msg = new Message();
                byte[] data = createSubStr(packetBuffer, (int)packetBufferPos, (int)totalLength);
                string packetBufferStr = Encoding.ASCII.GetString(data);
                int pos = 0;
                if (toLength != 0)
                {
                    msg.To = packetBufferStr.Substring(pos, toLength);
                    pos += toLength;
                }
                if (fromLength != 0)
                {
                    msg.From = packetBufferStr.Substring(pos, fromLength);
                    pos += fromLength;
                }
                if (threadLength != 0)
                {
                    msg.Thread = packetBufferStr.Substring(pos, threadLength);
                    pos += threadLength;
                }
                if (subjectLength != 0)
                {
                    msg.Subject = packetBufferStr.Substring(pos, subjectLength);
                    pos += subjectLength;
                }
                if (bodyLength != 0)
                {
                    msg.Body = packetBufferStr.Substring(pos, (int)bodyLength);
                    pos += (int)bodyLength;
                }
                messages.Enqueue(msg);
                packetBufferPos += totalLength;  //update the packet buffer pos
            }
            return messages;
        }
コード例 #15
0
 public void sendMessage(Message msg)
 {
     sendLock.WaitOne();
     try
     {
         byte[] sendBuffer = new byte[ClientConnection.PACKET_HEADER_SIZE+msg.MessageData.Length];
         byte[] header = (byte[])msg.MessageHeader;
         for (int i=0; i<ClientConnection.PACKET_HEADER_SIZE; i++)
         {
             sendBuffer[i] = header[i];
         }
         int index = ClientConnection.PACKET_HEADER_SIZE;
         char[] data = msg.MessageData.ToCharArray();
         for (int i=0; i<data.Length; i++)
         {
             sendBuffer[index++] = (byte)data[i];
         }
         ss.Send(sendBuffer);
     }
     catch (Exception ex)
     {
         Context.getInstance().getLogger().forceLog("Message Sender; Error in sendMessage(): " + ex.Message);
     }
     sendLock.ReleaseMutex();
 }