Exemplo n.º 1
0
        private void Decode(NodeMessage message)
        {
            try{
                if (message == null)
                {
                    return;
                }
                if (LogEvent != null)
                {
                    LogEvent(this.Name, true, message.ToString());
                }

                switch (message.Context)
                {
                case MessageContext.Authentication:
                    HandleAuthMessage(message);
                    break;

                case MessageContext.Generic:                         // only for authenticated nodes
                    if (!verified)
                    {
                        throw new NodeSecurityException();
                    }
                    HandleGenericMessage(message);
                    break;

                case MessageContext.Task:                         // only for authenticated nodes
                    if (!verified)
                    {
                        throw new NodeSecurityException();
                    }
                    HandleTaskMessage(message);
                    break;

                default:
                    throw new P2PBackup.Common.ProtocolViolationException(message);
                }
                if (message.Synchroneous && SyncMessageReceived != null)
                {
                    Console.WriteLine("Decode() : raising event for received sync message");
                    SyncMessageReceived(message);
                }
            }
            catch (Exception ex) {           // most likely received a badly formatted message, or a message from a non authorized/authenticated node
                Logger.Append("CLIENT", Severity.WARNING, "Node #" + this.Id + " : " + ex.ToString());
            }
        }
Exemplo n.º 2
0
        private NodeMessage SendMessage(NodeMessage message)
        {
            //Prevent sending a message unrelated to authentication
            //if node is not authenticated and approved
            if (message.Context != MessageContext.Authentication && !this.verified)
            {
                Logger.Append("HUBRN", Severity.ERROR, "Node #" + this.Id + " : cannot send message with context '" + message.Context + "' while node is not authenticated");
                throw new NodeSecurityException("Cannot send message with context '" + message.Context + "' while node is not authenticated");
            }
            if (message.Id > 0)           // existing sync message, we're called from the event handler
            {
                syncMessages[message.Id]  = message;
                this.SyncMessageReceived -= SendMessage;
                syncMsg.Set();
                return(null);
            }
            else if (message.Synchroneous)
            {
                message.Id = new Random().Next(100000);
                syncMessages.Add(message.Id, null);
                this.SyncMessageReceived += SendMessage;
            }
            byte[] byteMsg = Encoding.UTF8.GetBytes(message.ToJson <NodeMessage>());
            int    msgSize = byteMsg.Length;

            byte[] header = BitConverter.GetBytes(msgSize);             // header always has int size (4 bytes)
            try{
                lock (hubStream){
                    hubStream.Write(header);
                    hubStream.Write(byteMsg);
                    hubStream.Flush();
                }
                Logger.Append("HUBRN", Severity.TRIVIA, "Sent message to node #" + this.Id + " (" + this.Name + ") : " + message.ToJson <NodeMessage>());
                if (message.Synchroneous)
                {
                    if (!syncMsg.WaitOne(30000))
                    {
                        throw new TimeoutException("Timed out waiting for synchronous message response from Node #" + this.Id + ". Message was : " + message.ToString());
                    }
                    syncMsg.Reset();
                    if (syncMessages.ContainsKey(message.Id))
                    {
                        NodeMessage m = syncMessages[message.Id];
                        lock (syncMessages){
                            syncMessages.Remove(message.Id);
                        }
                        return(m);
                    }
                }
            }
            catch (Exception ex) {
                Disconnect();
                Logger.Append("HUBRN", Severity.ERROR, "Unable to send message to node #" + this.Id + " (" + this.Name + ") : " + ex.Message);
            }
            return(null);
        }