/// <summary><see cref="IDisposable.Dispose"/></summary>
        public void Dispose()
        {
            // Stop all running tasks
            if (_cancellationTokenSource != null)
            {
                _cancellationTokenSource.Cancel();
            }
            // Stop the connection with the large file service
            if (_fileServiceClient != null)
            {
                _fileServiceClient.Dispose();
            }

            // Dispose all created producers
            foreach (IAbstractProducer producer in _producers.Values)
            {
                ((IDisposable)producer).Dispose();
            }
            // Dispose all created consumers
            foreach (IAbstractConsumer consumer in _consumers.Values)
            {
                ((IDisposable)consumer).Dispose();
            }

            // Dispose all system producers
            if (_heartbeatProducer != null)
            {
                _heartbeatProducer.Flush();
                _heartbeatProducer.Dispose();
            }
            if (_logProducer != null)
            {
                _logProducer.Flush();
                _logProducer.Dispose();
            }

            // Dispose all system consumers
            if (_heartbeatConsumer != null)
            {
                _heartbeatConsumer.Close();
                _heartbeatConsumer.Dispose();
            }
            if (_timeConsumer != null)
            {
                _timeConsumer.Close();
                _timeConsumer.Dispose();
            }
            if (_timeControlConsumer != null)
            {
                _timeControlConsumer.Close();
                _timeControlConsumer.Dispose();
            }
            if (_topicInviteConsumer != null)
            {
                _topicInviteConsumer.Close();
                _topicInviteConsumer.Dispose();
            }

            _instance = null;
        }
 /// <summary>
 /// Singleton implementation to make sure only one adapter instance is running for one tool application
 /// </summary>
 /// <returns>The instance of the test-bed adapter</returns>
 public static TestBedAdapter GetInstance()
 {
     if (_instance == null)
     {
         _instance = new TestBedAdapter();
     }
     return(_instance);
 }
 /// <summary>
 /// Method for notifying the connected application of a consumption
 /// </summary>
 /// <param name="message">The message that was consumed</param>
 private void NotifyConsumption(ConsumeResult <EDXLDistribution, T> message)
 {
     if (TestBedAdapter.GetInstance().State == TestBedAdapter.States.Enabled || TestBedAdapter.GetInstance().State == TestBedAdapter.States.Debug)
     {
         // Make sure this message is allowed to be received from the topic
         if (TestBedAdapter.GetInstance().State == TestBedAdapter.States.Debug || TestBedAdapter.GetInstance().AllowedTopicsReceive.Contains(message.Topic))
         {
             _consumerHandler?.Invoke(message.Key.senderID, message.Topic, message.Value);
         }
     }
     else
     {
         _messageQueue.Enqueue(message);
     }
 }
 /// <summary>
 /// Method for sending the given message over the given topic
 /// </summary>
 /// <param name="message">The message to be send</param>
 /// <param name="topic">The name of the topic to send the message over</param>
 internal void SendMessage(T message, string topic)
 {
     // Only send the message whenever the adapter is enabled
     if (TestBedAdapter.GetInstance().State == TestBedAdapter.States.Enabled || TestBedAdapter.GetInstance().State == TestBedAdapter.States.Debug)
     {
         // Make sure this message is allowed to be sent on the topic
         if (TestBedAdapter.GetInstance().State == TestBedAdapter.States.Debug || TestBedAdapter.GetInstance().AllowedTopicsSend.Contains(topic))
         {
             // Send the message
             Message <EDXLDistribution, T> m = new Message <EDXLDistribution, T>()
             {
                 Key   = CreateKey(),
                 Value = message,
             };
             Task <DeliveryResult <EDXLDistribution, T> > task = _producer.ProduceAsync(topic, m);
             task.ContinueWith(t =>
             {
                 if (t.IsFaulted)
                 {
                     OnError?.Invoke(this, new Error(ErrorCode.Local_Fail, t.Exception.ToString()));
                 }
             });
             // Wait for sending this message if the settings property was set
             if (_configuration.Settings.sendsync)
             {
                 task.Wait();
             }
         }
         else
         {
             throw new CommunicationException($"cannot send message, since the topic ({topic}) is restricted");
         }
     }
     else
     {
         // If this isn't the case, report and queue the message for sending later
         TestBedAdapter.GetInstance().Log(log4net.Core.Level.Notice, $"Could not send message to topic {topic}, because adapter is disabled! Enqueuing message for sending later.");
         _messageQueue.Enqueue(new KeyValuePair <T, string>(message, topic));
     }
 }
        /// <summary>
        /// Method for letting the adapter upload the file at the given path to the large file service
        /// </summary>
        /// <param name="filePath">The path directing to the file to upload</param>
        /// <param name="dataType">The type of file that is going to be uploaded</param>
        /// <param name="obfuscate">Indication if this should be a private upload, therefore generating a obfuscated link to the uploaded file</param>
        /// <param name="sendToTestbed">Indication if the adapter should automatically update the test-bed via the <see cref="eu.driver.model.core.LargeDataUpdate"/> message</param>
        /// <returns>The <see cref="Task"/> handling the upload, resulting in a <see cref="HttpRequestMessage"/>, or null if the file couldn't be found</returns>
        public Task <HttpResponseMessage> Upload(string filePath, DataType dataType, bool obfuscate, bool sendToTestbed)
        {
            // Make sure the file actually exists
            if (System.IO.File.Exists(filePath))
            {
                // Retrieve the large file service client for the upload
                System.Net.Http.HttpClient client = TestBedAdapter.GetInstance().GetLargeFileServiceClient();

                // Create and enter the POST parameters
                MultipartFormDataContent content = new MultipartFormDataContent();
                // the file to upload
                StreamContent file = new StreamContent(System.IO.File.OpenRead(filePath));
                file.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                {
                    Name     = "uploadFile",
                    FileName = System.IO.Path.GetFileName(filePath),
                };
                content.Add(file);
                // the indication if this upload needs to be obfuscated or not
                content.Add(new StringContent(obfuscate.ToString().ToLower()), "private");

                // Send the POST
                Task <HttpResponseMessage> res = client.PostAsync("/upload", content);

                // Wait for the task whenever this adapter needs to send the update as well
                if (sendToTestbed)
                {
                    WaitForUploadCompletion(System.IO.Path.GetFileName(filePath), dataType, res);
                }

                return(res);
            }
            else
            {
                return(null);
            }
        }