/*
         * public void Write(MessageBase message, EncryptionOption encryptionOptions, MessageTagCollection tags)
         * {
         *  Write(message, "", encryptionOptions, tags);
         * }
         *
         * public void Write(MessageBase message, IBasicProperties basicProperties, EncryptionOption encryptionOptions, MessageTagCollection tags)
         * {
         *  if (basicProperties is null)
         *  {
         *      throw new ArgumentNullException(nameof(basicProperties));
         *  }
         *
         *  Write(message, "", basicProperties, encryptionOptions, tags);
         * }
         */

        public void WriteToBoundExchange(MessageBase message, bool encryptMessage, MessageTagCollection tags)
        {
            var encryptionOption = encryptMessage switch
            {
                true => EncryptionOption.EncryptWithSystemSharedKey,
                false => EncryptionOption.None
            };

            Write(message, boundExchangeName, encryptionOption, tags);
        }
Exemple #2
0
 public TrustCoordinatorModule(TimeSpan keyRotationTime, MessageTagCollection tags) : this(keyRotationTime, tags.ToArray())
 {
     // No implementation here.  Use the other constructor.
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exchangeName"></param>
        /// <param name="basicProperties"></param>
        private void Write(MessageBase message, string exchangeName, IBasicProperties basicProperties, EncryptionOption encryptionOption, MessageTagCollection tags)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            //message.ClientName = _clientName;

            var envelope = MessageEnvelope.WrapMessage(message, "", this.ParentMessageClient, encryptionOption);

            byte[] hashedMessage = envelope.Serialize();

            //TODO: Move this into the message serialization?
            basicProperties.Headers = new Dictionary <string, object>();
            basicProperties.Headers.Add(new MessageSenderTag(this._clientName).GetMangledTagAndValue(), "");

            foreach (var tag in tags)
            {
                string mangledTag = tag.GetMangledTagName();

                if (!basicProperties.Headers.ContainsKey(mangledTag))
                {
                    basicProperties.Headers.Add(mangledTag, "");
                }

                // If this is a tag with a value (not just a tag)
                if (tag.HasValue)
                {
                    basicProperties.Headers.Add(tag.GetMangledTagAndValue(), "");
                }
            }

            lock (Channel)
            {
                Channel.BasicPublish(
                    exchange: exchangeName,
                    routingKey: queueInfo.QueueName,
                    basicProperties: basicProperties,
                    hashedMessage);
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="message"></param>
 /// <param name="exchangeName"></param>
 private void Write(MessageBase message, string exchangeName, EncryptionOption encryptionOptions, MessageTagCollection tags)
 {
     Write(message, exchangeName, Channel.CreateBasicProperties(), encryptionOptions, tags);
 }
        /// <summary>
        /// Declares a queue and binds it to an exchange.  Exchanges allow messages to be sent to more than one queue.  Exchange-bound queues are useful when a
        /// receiver needs to consume broadcast messages.
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="queueName"></param>
        internal static TQueueType CreateExchangeBoundMessageQueue <TQueueType>(MessageClientBase parentMessageClient, IModel channel, string clientName, string exchangeName, string queueName, bool durable, bool autoDelete, MessageTagCollection subscriptionTags) where TQueueType : MessageQueue, new()
        {
            var queue = new TQueueType()
            {
                Channel   = channel,
                queueInfo = channel.QueueDeclare(
                    queue: queueName,
                    durable: durable,
                    exclusive: true,
                    autoDelete: autoDelete),
                _clientName         = clientName,
                ParentMessageClient = parentMessageClient,
                boundExchangeName   = exchangeName
            };

            if (!subscriptionTags.Any())
            {
                channel.QueueBind(queueName, exchangeName, "");
            }
            else
            {
                var map = new Dictionary <string, object>();

                map.Add("x-match", "any");

                foreach (var item in subscriptionTags)
                {
                    map.Add(item.GetMangledTagAndValue(), "");
                }

                channel.QueueBind(queueName, exchangeName, "", map);
            }

            return(queue);
        }
 public PushoverModule(string pushoverAppApiKey, string pushoverUserKey, MessageTagCollection tags) : this(pushoverAppApiKey, pushoverAppApiKey, tags.ToArray())
 {
 }
 public XBeeNetworkGatewayModule(string xbeeComPortName, XBeeLibrary.Windows.Connection.Serial.SerialPortParameters xbeeComPortParameters, MessageTagCollection tags) :
     this(xbeeComPortName, xbeeComPortParameters, tags.ToArray())
 {
     // Implement in constructor below:
 }
    protected override void SetupConnections(TimeSpan timeout, MessageTagCollection subscriptionTags)
    {
        base.SetupConnections(timeout, subscriptionTags);

        ServerQueue = MessageQueue.CreateMessageQueue <TServerQueue>(this, Channel, ClientName, ServerQueueName, this.QueuesAreDurable, this.AutoDeleteQueuesOnClose);
    }
Exemple #9
0
 public AzureIoTModule(string azureIoTHubConnectionString, MessageTagCollection tags) : this(azureIoTHubConnectionString, tags.ToArray())
 {
     // Do not implement
 }
        /// <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;
        }
Exemple #11
0
 public ClientModule(MessageTagCollection tags) : this(tags.ToArray())
 {
 }