Home › Forums › Syphon › Syphon Development – Developer › getting list of servers
Tagged: server query objective-c
- This topic has 11 replies, 2 voices, and was last updated 8 years, 3 months ago by andres.
-
AuthorPosts
-
September 12, 2012 at 3:21 pm #6335andresParticipant
Hello guys, could you point me to some snippet of native code to retrive the list of all the syphon servers currently running on the system?
Thanks!September 12, 2012 at 3:57 pm #6336bangnoiseKeymasterDead simple –
http://syphon.v002.info/FrameworkDocumentation/#finding-servers
or in use
Usually you’d want to watch for the notifications SyphonServerDirectory emits so you can continue to update the list. Note that immediately after app launch the list may be empty, as server discovery takes a small amount of time.
September 13, 2012 at 6:49 am #6337andresParticipantgreat, thanks for the info!
December 5, 2012 at 1:21 pm #11274andresParticipantHi, I opened an issue about this:
http://code.google.com/p/syphon-implementations/issues/detail?id=26
and did some coding. I started by adding the following native method to JSyphon:
JNIEXPORT jobject JNICALL Java_jsyphon_JSyphonServerList_getList(JNIEnv * env, jobject jobj)
{
jobject serverlist = nil;JNF_COCOA_ENTER(env);
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];NSLog(@”Getting list of servers”);
NSArray *servers = [[SyphonServerDirectory sharedDirectory] servers];
NSMutableArray *output = [NSMutableArray arrayWithCapacity:[servers count]];
NSLog(@”Number of servers %i”, [servers count]);for (NSDictionary *description in servers)
{
NSLog(@”Adding server = %@”, description);
NSDictionary *simple = [NSDictionary dictionaryWithObjectsAndKeys:[description objectForKey:SyphonServerDescriptionNameKey], @”Name”, [description objectForKey:SyphonServerDescriptionAppNameKey], @”App Name”, nil];
[output addObject:simple];
}JNFTypeCoercer* coecer = [JNFDefaultCoercions defaultCoercer];
[JNFDefaultCoercions addMapCoercionTo:coecer];serverlist = [coecer coerceNSObject:servers withEnv:env];
[pool drain];
JNF_COCOA_EXIT(env);return serverlist;
}I can run it from Java, but gives 0 as the server count, even though I have several servers running already, which I can connect to with the Client object.
What am I missing here? In the QC server list plugin I also see some additional initialization, in particular:
[[SyphonServerDirectory sharedDirectory] addObserver:self forKeyPath:@”servers” options:0 context:nil];
Do I need to do something similar, or there is it a way to query the SyphonServerDirectory only one time in order to get the list of available servers at the moment of the query?
December 5, 2012 at 4:01 pm #11298bangnoiseKeymasterAh I get to type at you in two channels now 😉
Are you doing this as soon as Syphon is loaded? It takes a moment for server discovery to happen. Otherwise the code looks good.
December 5, 2012 at 4:08 pm #11310andresParticipantYes, maybe I need to add a Thread.sleep() somewhere to give some time to the server discovery?
December 5, 2012 at 4:31 pm #11311bangnoiseKeymasterIf sleep happens on the main thread, it may block the discovery process too. Just as an experiment try doing it on a key press or somesuch, and see if you get any valid servers.
December 5, 2012 at 11:20 pm #11343andresParticipantI added this code to listServers():
ArrayList tempList = null;
int size0 = 0;
int count = 0;
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
tempList = JSyphonServerList.getList();
if (tempList.size() == size0) {
count++;
}
size0 = tempList.size();
if (10 < count) {
break;
}
}
it is not pretty, but works. However, the numbers (10ms, 50 tot attempts, etc) are basically arbitrary guesses. Also, if there are many servers, the 10 < count condition might not be enough. So I wonder if there is a better way to do this.
December 6, 2012 at 3:54 am #11357bangnoiseKeymasterAssuming polling is what people are used to in Processing:
I’d rather avoid the sleep. How about registering for SyphonServerDirectory’s notifications, and have a ServerListHasChanged() method? Users can poll that and it will return true if you’ve received a notification since they last queried it or called listServers. At that point they can call listServers().
December 6, 2012 at 1:11 pm #11429andresParticipantThe main reference I’m using is the capture functionality in the core video library. Basically, there you just have a static Capture.list() method that returns an array of strings containing the list of available capture devices. Clearly, this is what I’m using as a model here.
However, it seems to me that the way the server discovery works in Syphon is not conducing to this kind of approach. Correct me if I’m wrong, but what SyphonServerDirectory does is not to query the available devices at a given time, but instead it passively waits to be contacted by the servers and then adds them to its internal list of servers, right?
An alternative polling mechanism where an event handling method would be called with the appropriate object holding the names of the registered server it is certainly possible, but the list() method would be preferred. Although the code I mentioned earlier works, it seems to me like a “problematic hack” that goes against the way server discovery works.
December 7, 2012 at 12:46 pm #11635bangnoiseKeymasterThe list of available servers is liable to change fairly often, so providing a way to know if it has changed seems a useful thing. The most obvious scenario is when a user launches their Processing sketch and then launches a server app afterwards – you’ll have a new server appear a few seconds/mouse-clicks after launch.
Internally, SyphonServerDirectory passively requests servers announce themselves when it loads and maintains a list locally which is returned by the servers method. As in the rest of the framework, not blocking is a feature, to avoid hung applications hanging other apps.
Unless Processing provides a way to deliver notifications and users are accustomed to using that, having a ServerListHasChanged() (or similar) method paired with a non-blocking ServerList() method is the simplest arrangement I can think of.
I don’t expect being able to list servers to be a much-used feature for Processing – most people will be happy to hard-code a particular server by name, or any available server if you permit that as per my comment on the serverName/appName issue on google code.
Forgive slow responses – currently working on a show.
Thanks for your recent activities on this – really good to see Processing support become solid.
December 7, 2012 at 3:45 pm #11679andresParticipantThanks for your feedback. For the time being I will just go ahead and post a new package of the library, accompanying the 2.0b7 release of Processing and using the current list method. I will improve the server detection for the next version.
-
AuthorPosts
- You must be logged in to reply to this topic.