예제 #1
0
 // Run a query and wait for the predicate to be satisfied.
 // Return the list of resources which satisfied the predicate or null if canceled before the predicate was satisfied.
 public static IEnumerable <IDiscoveryResource> QueryAndWaitForResourcesPredicate(
     IDiscoveryAgent svc, string type,
     Func <IEnumerable <IDiscoveryResource>, bool> pred, CancellationToken token)
 {
     using (var discovery = svc.Subscribe(type))
     {
         return(QueryAndWaitForResourcesPredicate(discovery, pred, token));
     }
 }
예제 #2
0
 private void SendAndReceive(IDiscoveryAgent agent)
 {
     // Do simple operations to verify that sending and receiving packets doesn't fail.
     using (var cts = new CancellationTokenSource(Utils.TestTimeoutMs))
     {
         var _ = agent.PublishAsync("Test", "http://resource1", null, cts.Token).Result;
     }
     using (var task = agent.Subscribe("Category")) { }
 }
예제 #3
0
        /// <summary>
        /// Factory method for creating a DiscoveryTransport.  The Discovery Transport wraps the
        /// given ICompositeTransport and will add and remove Transport URIs as they are discovered.
        /// </summary>
        public static DiscoveryTransport CreateTransport(ICompositeTransport compositeTransport, URISupport.CompositeData compositeData, StringDictionary options)
        {
            DiscoveryTransport transport = new DiscoveryTransport(compositeTransport);

            URISupport.SetProperties(transport, options, "transport.");
            transport.Properties = options;

            Uri             discoveryAgentURI = compositeData.Components[0];
            IDiscoveryAgent discoveryAgent    = DiscoveryAgentFactory.CreateAgent(discoveryAgentURI);

            transport.DiscoveryAgent = discoveryAgent;

            return(transport);
        }
예제 #4
0
        public void Run(string username, IPAddress broadcastAddress)
        {
            _username        = username;
            _localAddrString = GetLocalIPAddress().ToString();
            _chatServer      = new TcpListener(IPAddress.Any, ChatPort);
            _chatServer.Start();
            Task.Run(ListenForConnections);

            // Initialize the discovery agent
            _discoveryAgent = new PeerDiscoveryAgent(new UdpPeerDiscoveryTransport(broadcastAddress, DiscoveryPort));

            // Publish a resource exposing the local IP address for connections and the user name as an attribute.
            var connection = GetLocalIPAddress().ToString();
            var attributes = new Dictionary <string, string> {
                [NameKey] = username
            };

            _discoveryAgent.PublishAsync(ParticipantCategory, connection, attributes);

            // Subscribe to other participant resources.
            _discoverySubscription          = _discoveryAgent.Subscribe(ParticipantCategory);
            _discoverySubscription.Updated +=
                (IDiscoverySubscription subscription) =>
            {
                // Parse discovered resources.
                var activePeers = new Dictionary <IPAddress, string>();
                foreach (var res in subscription.Resources)
                {
                    if (res.Connection == _localAddrString)
                    {
                        // Exclude the local resource.
                        continue;
                    }
                    try
                    {
                        var address = IPAddress.Parse(res.Connection);
                        var name    = res.Attributes[NameKey];
                        activePeers.Add(address, name);
                    }
                    catch (Exception e)
                    {
                        // Invalid resource format, or multiple resources per host.
                        Debug.WriteLine($"Invalid resource: {e}");
                        continue;
                    }
                }

                // Create reader connections to the active peers.
                RefreshReaderConnections(activePeers);
            };

            // Loop waiting for input.
            Console.CursorTop = Console.WindowHeight;
            Console.Write(Prompt);
            while (true)
            {
                string message = Console.ReadLine();
                PostLocalMessageToConsole(message);
                PostLocalMessageToPeers(message);
            }
        }