WCF Peer to Peer, Are There Nodes Out There?

.net, c#, wcf

Solution

See Peer-to-Peer Programming with WCF and .NET Framework 3.5: Peer Name by Amit Bahree and Chris Peiris:

The final logical step after creating and publishing a peer is resolving a peer. What good is it to publish something to the cloud if another peer cannot find you? We use the `PeerNameResolver` class to resolve for a specific peer in a given cloud. The `PeerNameResolver` can resolve a peer to either a `PeerRecord` or a cloud, depending on the parameters that are passed. The resolution process finishes either when the maximum number of record entries for the `PeerRecordCollection` is reached or when it has reached the end of various clouds.

The `PeerNameResolver` class exposes an overloaded method that is called `Resolve` and is used to resolve a given peer synchronously.

Listing 17 shows us how to try to resolve for a peer that is called `MySecurePeer`. The Resolve method returns a collection of type `PeerNameRecordCollection` through which we iterate. Listing 18 shows the result of this when running on a computer that has three network cards.

PeerName myPeer = new PeerName("MySecurePeer", PeerNameType.Secured);
PeerNameResolver resolver = new PeerNameResolver();

PeerNameRecordCollection results = resolver.Resolve(myPeer);

Console.WriteLine("{0} Peers Found:", results.Count.ToString());
int i = 1;

foreach (PeerNameRecord peer in results)
{
    Console.WriteLine("{0} Peer:{1}", i++, peer.PeerName.ToString());
    foreach (IPEndPoint ip in peer.EndPointCollection)
    {
        Console.WriteLine("\t Endpoint: {0}", ip.ToString());
    }
}

So, I guess you should check out `PeerNameResolver.Resolve` Method:

This method associates peer names to clouds. Calling the `PeerNameResolver` method is similar to calling the `Resolve` method for each peer name record in the `PeerNameRecordCollection`. Note that using the `Resolve` method on an individual peer name record does not invalidate resolving multiple peer names.

For every `Resolve` method, there is an equivalent `ResolveAsync` method. They are identical in the parameters they are passed, except that the `ResolveAsync` method includes a system token in its parameter list for asynchronous event handling.

Problem

I am using WCF in .NET 3.5 to implement a peer to peer networking application. To resolve peer nodes I am using PNRP. IGlobalStoreServiceContract is my contract as shown below, ``` [ServiceContract(Namespace = "http://GlobalStoreEventDriven.API", CallbackContract = typeof(IGlobalStoreServiceContract))] internal interface IGlobalStoreServiceContract { [OperationContract(IsOneWay = true)] void NotifyGlobalStoreDataInserted(string globalGroup, DateTime maxDateTime); [OperationContract(IsOneWay = true)] void RegisterNode(); [OperationContract(IsOneWay = true)] void SynchronizeMemberList(Guid clientId); } ``` I am using some code like this to join each node to the peer to peer network. ``` DuplexChannelFactory<IGlobalStoreChannel> channelFactory = new DuplexChannelFactory<IGlobalStoreChannel>(instance, "GlobalStoreAPIEndPoint"); IGlobalStoreChannel globalStoreChannel = channelFactory.CreateChannel(); globalStoreChannel.Open(); ``` My question is as soon as I have opened the channel how can I best tell if other peer nodes are on the network? For instance I could call one of the methods in my contract RegisterNode, and each node in the network could use a callback to call SynchronizeMemberList. I would then know whether other nodes were there. The trouble with that is it is all asynchronous. If I call RegisterNode and no one replies, it doesn't really mean no one is there, it could just mean that I didn't wait long enough. What do you reckon? Any suggestions?

Original source