Exemplo n.º 1
0
        public TrustCoordinatorModule(TimeSpan keyRotationTime, params MessageTag[] tags) : base(tags)
        {
            KeyRotationTime = keyRotationTime;

            this.MessageClientRegistered += TrustCoordinatorModule_MessageClientRegistered;

            keyUpdateTimer = new ReusableThreadSafeTimer()
            {
                Interval  = (KeyRotationTime - UpdateLeadTime).TotalMilliseconds,
                AutoReset = false
            };

            keyUpdateTimer.Elapsed += KeyUpdateTimer_Elapsed;
            keyUpdateTimer.Start();
        }
        public void OpenXBee()
        {
            xBee.Open();

            var network = xBee.GetNetwork();

            xBeeWasOpen = true;
            checkXBeeTimer.Start();

            //xBeeNetwork = xBee.GetNetwork();

            xBee.DataReceived            += XBee_DataReceived;
            xBee.ModemStatusReceived     += XBee_ModemStatusReceived;
            xBee.IOSampleReceived        += XBee_IOSampleReceived;
            xBee.MicroPythonDataReceived += XBee_MicroPythonDataReceived;
            xBee.PacketReceived          += XBee_PacketReceived;
        }
Exemplo n.º 3
0
        /// <summary>
        /// When called, updates the trust zone's shared key.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void KeyUpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var newKey = CreateNewKey(currentKey.TrustZoneName, currentKey.ValidTo);

            var updateMessage = new TrustZoneSharedKeyUpdate(newKey);

            this.MessageClient.BroadcastToAllClients(updateMessage, true);

            keyUpdateTimer.Interval = (newKey.ValidTo - DateTime.Now - UpdateLeadTime).TotalMilliseconds;
            keyUpdateTimer.Start();

            Timer tempTimer = new Timer()
            {
                Interval = (newKey.ValidFrom - DateTime.Now).TotalMilliseconds,
                Enabled  = true
            };

            tempTimer.Elapsed += (sender, e) =>
            {
                currentKey = newKey;
                tempTimer?.Dispose();
            };
        }
        public void ThreadSafeTimerTest()
        {
            bool elapsedEventOccurred = false;

            ReusableThreadSafeTimer timer = new ReusableThreadSafeTimer()
            {
                AutoReset = false,
                Enabled   = false,
                Interval  = 100
            };

            timer.Elapsed += (sender, e) => elapsedEventOccurred = true;

            Assert.IsFalse(timer.AutoReset);
            Assert.IsFalse(timer.Enabled);
            Assert.AreEqual(timer.Interval, 100);

            timer.Start();
            Assert.IsTrue(timer.Enabled);
            Assert.IsFalse(elapsedEventOccurred);
            Thread.Sleep(50);
            Assert.IsTrue(timer.Enabled);
            Assert.IsFalse(elapsedEventOccurred);
            Thread.Sleep(100);
            Assert.IsFalse(timer.Enabled);
            Assert.IsTrue(elapsedEventOccurred);

            Thread.Sleep(1000);
            Assert.IsFalse(timer.AutoReset);
            Assert.IsFalse(timer.Enabled);
            Assert.AreEqual(timer.Interval, 100);

            timer.Dispose();

            timer = new ReusableThreadSafeTimer(100)
            {
                AutoReset = true
            };

            Assert.IsFalse(timer.Enabled);
            Assert.IsTrue(timer.AutoReset);
            Assert.AreEqual(timer.Interval, 100);

            int count = 0;

            timer.Elapsed += (sender, e) => count++;

            timer.Start();
            Thread.Sleep(1000);
            timer.Stop();
            Assert.IsFalse(timer.Enabled);
            Assert.IsTrue(count >= 9);

            timer.Dispose();

            timer = new ReusableThreadSafeTimer(TimeSpan.FromSeconds(2));

            Assert.AreEqual(timer.Interval, 2000);

            timer.Dispose();

            timer = new ReusableThreadSafeTimer(1000);
            DateTime finishTime = DateTime.Now;
            DateTime startTime;

            timer.Elapsed += (sender, e) => finishTime = DateTime.Now;

            startTime = DateTime.Now;
            timer.Start();
            Thread.Sleep(500);
            timer.Restart();
            Thread.Sleep(1250);

            TimeSpan totalTime = finishTime - startTime;

            Assert.IsTrue(totalTime > TimeSpan.FromMilliseconds(1200) && totalTime < TimeSpan.FromMilliseconds(1700));

            timer.Dispose();
        }
        /// <summary>
        /// Connects the server's queues to the RabbitMQ server and begins processing of messages.
        /// </summary>
        /// <param name="timeout">The maximum amount of time to wait for a successful connection.  An exception occurs upon timeout.</param>
        /// <param name="subscriptionTags"></param>
        public void Connect(TimeSpan timeout, MessageTagCollection subscriptionTags)
        {
            //TODO: Add subscription of broadcast tags

            SetupConnections(timeout, subscriptionTags);

            DedicatedQueue = MessageQueue.CreateExchangeBoundMessageQueue <ReadableMessageQueue>(this, Channel, ClientName, BroadcastExchangeName, DedicatedQueueName, this.QueuesAreDurable, this.AutoDeleteQueuesOnClose);
            DedicatedQueue.MessageReceived       += DedicatedQueue_MessageReceived;
            DedicatedQueue.AsynchronousException += (sender, eventArgs) => FireAsynchronousExceptionEvent(this, eventArgs);

            DedicatedQueue.BeginFullConsume(true);

            // Announce client to server
            ClientAnnounceMessage message = new ClientAnnounceMessage(PublicKeystore.ParentClientInfo);

            //TODO: Encrypt this?
            this.BroadcastToAllClients(message, false);

            //var result = this.WriteToServer(message, out response, out rawResponse, (int)timeout.TotalMilliseconds);

#if false
            if (result.CallResult == RpcCallResult.Timeout)
            {
                throw new ConnectionException("Timeout trying to communicate with the server.");
            }

            var resultMessage = (ClientAnnounceResponseMessage)result.ResponseMessageEnvelope.Message;

            switch (resultMessage.Response)
            {
            case AnnounceResponse.Accepted:
                clientServerHandshakeComplete = true;
                //serverName = result.ResponseMessageEnvelope.SenderName;
                PublicKeystore.SystemSharedKeys[resultMessage.SystemSharedKeyId] = resultMessage.SystemSharedKey;
                break;

            case AnnounceResponse.Rejected:
                throw new ConnectionException($"Client rejected by server with the following message: {result.ResponseMessageEnvelope.Message.MessageText}.");
            }

            //result.ResponseMessageEnvelope.ReverifySignature(PublicKeystore[result.ResponseMessageEnvelope.SenderName].Dsa);

            //TODO: This is where we need to determine what to do with a new unstrusted signature

            if (result.ResponseMessageEnvelope.SignatureVerificationStatus != SignatureVerificationStatus.SignatureValid &&
                result.ResponseMessageEnvelope.SignatureVerificationStatus != SignatureVerificationStatus.SignatureValidButUntrusted)
            {
                throw new Exception("Bad server key.");
            }
            else
            {
                //PublicKeystore.Merge(((ClientAnnounceResponseMessage)result.ResponseMessageEnvelope.Message).PublicKeystore);
                //TODO: Fix PublicKeystore[result.ResponseMessageEnvelope.SenderName].Iv = ((ClientAnnounceResponseMessage)result.ResponseMessageEnvelope.Message).Iv;

                if (HeartbeatInterval > 0)
                {
                    serverHeartbeatTimer.Interval = HeartbeatInterval * 2;
                    serverHeartbeatTimer.Elapsed += ServerHeartbeatTimer_Elapsed;
                    serverHeartbeatTimer.Start();

                    heartbeatSendTimer.Interval = HeartbeatInterval;
                    heartbeatSendTimer.Elapsed += HeartbeatSendTimer_Elapsed;
                    heartbeatSendTimer.Start();
                }
            }
#endif
            IsConnected = true;
        }