Ejemplo n.º 1
0
        /// <summary>
        /// Stops this instance.
        /// </summary>
        public void StopInternal()
        {
            try
            {
                // stop the broadcast
                if (_broadcast != null)
                {
                    _broadcast.PeerDiscovered -= OnBroadcastPeerDiscovered;
                    _broadcast.Stop();
                    _broadcast = null;
                }

                // Signal stop and wake up consumer
                if (_cancellationTokenSource != null)
                {
                    _cancellationTokenSource.Cancel();
                }
                if (_dataAvailable != null)
                {
                    _dataAvailable.Set();
                }

                // Release poll task
                if (_mailboxPollTask != null)
                {
                    _mailboxPollTask.Wait(TimeSpan.FromSeconds(5));
                    _mailboxPollTask.Dispose();
                    _mailboxPollTask = null;
                }

                // Release poll task
                if (_mailboxProcessTask != null)
                {
                    _mailboxProcessTask.Wait(TimeSpan.FromSeconds(5));
                    _mailboxProcessTask.Dispose();
                    _mailboxProcessTask = null;
                }

                // Release signal
                if (_dataAvailable != null)
                {
                    _dataAvailable.Dispose();
                    _dataAvailable = null;
                }

                // Release token source
                if (_cancellationTokenSource != null)
                {
                    _cancellationTokenSource.Dispose();
                    _cancellationTokenSource = null;
                }

                // Release mailbox router
                if (_mailbox != null)
                {
                    _mailbox.Close();
                    _mailbox.Dispose();
                    _mailbox = null;
                }

                // Release peers
                {
                    var peers = _peers;
                    if (peers != null)
                    {
                        foreach (var kvp in peers)
                        {
                            DisconnectPeer(kvp.Key);
                        }
                        peers.Clear();
                        _peers = null;
                    }
                }

                // Release inbound queue
                if (_mailboxMessageQueue != null)
                {
                    ZmqMessage discarded;
                    while (_mailboxMessageQueue.TryDequeue(out discarded)) {}
                    _mailboxMessageQueue = null;
                }

                // Lose context
                if (_context != null)
                {
                    _context.Terminate();
                    _context.Dispose();
                    _context = null;
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("An error occured during disposal: {0}", e.Message);
                throw;
            }
            finally
            {
                // Oink
                Started = false;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts this instance.
        /// </summary>
        public bool Start()
        {
            if (Started) return true;

            // Prepare peer asynchronous operation
            _cancellationTokenSource = new CancellationTokenSource();
            _peers = new ConcurrentDictionary<Guid, Node>();
            _mailboxMessageQueue = new ConcurrentQueue<ZmqMessage>();
            _dataAvailable = new AutoResetEvent(false);

            // Create the context
            _context = ZmqContext.Create();

            // Create the mailbox router and connect it
            _mailbox = _context.CreateSocket(SocketType.ROUTER);
            _mailbox.Bind("tcp://*:*");

            // Port ermitteln
            var port = _mailbox.LastEndpoint.Split(':').Last();
            if (!UInt16.TryParse(port, NumberStyles.Integer, CultureInfo.InvariantCulture, out _mailboxPort))
            {
                Trace.TraceError("Could not parse ZRE router port '{0}'", port);
                StopInternal();
                return false;
            }

            // Start mailbox processing task
            _mailboxPollTask = Task.Factory.StartNew(MailboxPollTask, new ConsumerTaskState(_mailbox, _cancellationTokenSource.Token, _dataAvailable, _mailboxMessageQueue), TaskCreationOptions.LongRunning);
            _mailboxProcessTask = Task.Factory.StartNew(MailboxProcessTask, new ConsumerTaskState(null, _cancellationTokenSource.Token, _dataAvailable, _mailboxMessageQueue), TaskCreationOptions.LongRunning);

            // Broadcast erzeugen
            Trace.Assert(_broadcastFactory != null);
            _broadcast = _broadcastFactory.Create(_uuid, _mailboxPort);
            _broadcast.PeerDiscovered += OnBroadcastPeerDiscovered;
            _broadcast.Start();

            // Oink
            Started = true;
            return true;
        }