[en] Discovery

Topics: Technical Support
Jul 10, 2015 at 10:36 PM
Hi,

There is a way to register the server service over network so that the clients can discover it?

My idea is when the devices are on same network, the others clients can connect automatically on server.

It is possible?

Regards...

Nelson
Coordinator
Jul 11, 2015 at 12:59 AM
Edited Jul 11, 2015 at 1:02 AM
Yes, sure.

On the server side use ZyanComponentHost.EnableDiscovery method as follows:
var host = new ZyanComponentHost("MyApplication", protocol);
host.RegisterComponents... etc
host.EnableDiscovery();
On the client side, use static method ZyanConnection.DiscoverHosts:
// start service discovery
ZyanConnection.DiscoverHosts("MyApplication", ServerDiscovered);

...
// this callback will fire on every discovered server
private void ServerDiscovered(DiscoveryResponse response)
{
     // note: this callback is called from a worker thread
     // Android, WinForms, WPF projects will need to re-route the callback to the main UI thread
     // in console application we can call WriteLine from any thread:
     Console.WriteLine(response.HostUrl);
}
Make sure you use the same application name on both sides (like "MyApplication" string above).

Regards, Alex

P.S. Look at MiniChat example project for a working sample of service discovery.
MiniChat client populates its combo box with server addresses discovered in the local network.
Jul 11, 2015 at 3:16 PM
Hi Alex,

I have tested the above code on some scenarios (on same router, using hot spot by one of the devices and on same network that I have on emulator provided by genymotion ), but none of them worked.

On all network described, direct connect on client worked ok!

There are some situations that the discovery wont work?

Another question is about the time out of the discovery. There are a property that we can modify this setting?

Thank you!

Nelson
Coordinator
Jul 11, 2015 at 6:51 PM
Hi Nelson,
 
...some situations that the discovery wont work?
 
Discovery feature requires that UDP broadcast is enabled on your network.
I guess it may be disabled via router settings and/or a firewall.
I'm not aware of any other problems that may block service discovery.

Have you tried running discovery on your PC?
I suggest that you try playing with it on the computer rather then the device.
It's easier to analyze network traffic when you're on a PC.
Does MiniChat client work on your wifi network?
 
Another question is about the time out of the discovery. There are a property that we can modify this setting?
 
Discovery feature is still a bit experimental, and the API is not yet stable.
There are settings that control timeout, retry count, etc, but they are currently private,
so if you need them you'll need to build Zyan from sources. If you find them useful
for your scenario, I can make them public in the next release.

ZyanConnection.DiscoverServers method is just a thin wrapper for DiscoveryClient class,
located in Zyan.Communication.Discovery namespace. It demonstrates the usage of the discovery client:
var request = new DiscoveryRequest(name, version);
var dc = new DiscoveryClient(request, port);
dc.Discovered += (s, e) =>
{
    var response = e.Metadata as DiscoveryResponse;
    if (response != null)
    {
        responseHandler(response);
    }
};

dc.StartDiscovery();
DiscoveryClient has private properties: RetryCount and RetryTimeout.
You'll need to make them public and rebuild Zyan from sources to play with them in your application.
However, I'm not sure that you really need to change them when the problem in found.

Hope this helps,
Alex
Coordinator
Jul 11, 2015 at 9:32 PM
Hi Nelson,

I've created a test project using Zyan 2.6 nuget, and it looks to me like discovery feature doesn't work properly.
It finds the server running on the same machine, but doesn't find the server started on another machine in the same network.

This must be a bug, so I've filed an issue: https://zyan.codeplex.com/workitem/2509

Regards, Alex.
Jul 12, 2015 at 7:18 PM
Hi Alex...

Thanks for your attention... no problem!

This feature would be great, but not important, and don't will interrupt my project.

I will wait until this can be solved.

Please, let me know when this occurs so I can test it for you.


Regards...

Nelson
Coordinator
Jul 12, 2015 at 10:59 PM
Edited Jul 12, 2015 at 11:00 PM
Looks like UDP broadcast is blocked by my router, so I can't get the broadcast working on my home network at all.
I guess you can try to integrate another service discovery library along with Zyan into your application.

There are a few .NET service discovery libraries I know of:
Also, Android has its own service discovery tools, available since version 4.0 (Jelly Bean):
http://developer.xamarin.com/guides/android/platform_features/introduction_to_jelly_bean/
http://developer.xamarin.com/api/type/Android.Net.Wifi.P2p.WifiP2pManager/

But I myself haven't used any of them, sorry.

Hope that helps,
Alex