NSNetServices persists after stop method call? - cocoa

I am doing a simple bonjour broadcast using NSNetServices. Everything is working great when I start and stop the bonjour sharing at application launch/quit. However if I turn off bonjour using the stop method call, my app still appears to be broadcasting a dead service even after the netServiceDidStop: delegate message gets called, which prevents restarting of the service until after a relaunch of the app. Apple's documentation makes it look very straightforward, just publish or stop as needed... Am I missing something?

It seems that sometimes services are shown even when they are no longer available.
From Apple documentation:
Because failing to show a valid service is a bigger problem for the user than showing a stale service, Bonjour deliberately errs on the side of assuming that a service is still available.
.
.
although Bonjour generally discovers new services within a few
seconds, if a service goes away, the disappearance of the service may
not be discovered until your app tries to connect to it and gets no
response.
You should not assume that just because the Bonjour APIs report a
discovered service, the service is guaranteed to be available when the
software tries to access it.
Connecting to and Monitoring Network Services: When resolving fails

Related

Sonos control api: polling rate & subscription

We are controlling the sonos via a small IOT device. This device will be placed at the homes of our customers.
In our home automation system we need to know the playbackstate + volume of the players/groups.
At which polling rate can we ask these parameters ?
We cannot use a cloud server to handle the subscription events.
I tried to put a https webservice in the IOT device with a self signed certificate but this doesn't work.
After I have posted the subscription I get a request in my webservice but the cloud sonos server disconnects immediately.
Probably because of the certificate is self signed - Or can there be another reason ?
Is there another way to recieve status events ?
During discovery In the players Json object there is a key 'websocketUrl'.
The documentation says "The secure WebSocket URL for the device. See Connect for details".
But I cannot find more info about this.
Can this be used ?
At which polling rate can we ask these parameters?
Consider using subscriptions instead of polling. See this answer for more: https://stackoverflow.com/a/60893119/4902948
We cannot use a cloud server to handle the subscription events...
Or can there be another reason?
Is there another way to [receive] status events?
You must host a service that satisfies a set of requirements to receive events. See this answer for more: https://stackoverflow.com/a/57189717/4902948
Also see Details on API credentials and events in New features in versions 11.1, 11.2 (S1), & 12.0 (S2).
Can [the 'websocketUrl'] be used?
This is not publicly available for use.
It would be a lot easier to use the (undocumented but more useful) UPnP APIs. You could simply Subscribe to the UPnP RenderingControl endpoint, then you will get an event every time the volume changes. To find when the track changes, subscribe to AVTransport endpoint.
You can do this with a local server (this is how every Sonos app works after all) and no special cert requirements either.
UPnP events are described in the UPnP spec.
Your question doesn't provide details about the language you're using, but if the device is powerfull enough to run node, you have a good change with my sonos library.
You can just pull the required information from the device as often as you like. Or use the build-in event subscriptions. If you use the events, it will automatically setup an http listener and automatically subscribe to all the events you're listening for.
Events are instant (as in 50ms-100ms after the event took place on the device).
If you request information it is send directly to the device and the device will respond with the answer. Pulling multiple data points per second has never been a problem to me.
Warning as stated above, this uses the undocumented local UPNP api, but since their own app also uses it, I'm guessing that won't change soon.

Calling an embedded XPCService from external App

I have two independent applications (cocoa), one main, the other slave.
The main application application is embedding a XPCService, which is correctly launched when the application is opened. No problems so far for connecting and exchanging data with that service (I also see this XPCService in the Activity Monitor).
But I want the other slave application to connect and send data to that XPC Service (and then to the main first application) after it has been launched by the main one.
Using NSXPCConnection (with initWithMachServiceName), and the correct Service bundle identifier doesn't work: no NewConnection delegate is called so the slave application never reach the service.
What am I missing ? Is what I want is possible with NSXPCConnection ?
Is it because I need the XPCService (within plist) to be shared in some way ? In that case what should I do ?
I can't find any information about this sort connection !
Thanks a lot for your help !
"I have two independent applications "
As stated in Apple's Documentation for XPC:
an XPC service is private, and is available only to the main application that contains it.
For security, an application that contains an XPC helper app gets signed to ensure that only the main application is allowed to call up on its helper.
However, it should be noted that the documentation also states
A connection is a virtual endpoint; it is independent of whether an actual instance of the service binary is running. The service binary is launched on demand.
A connection can also be sent as a piece of data in an XPC message. Thus, you can pass a connection through XPC to allow one service to communicate with another service (for example).
Though I've not seen this in practice, it does imply that you may be able to initiate the connection from the main application to the XPC helper and send this connection to your 'slave' app.

.Net Remoting - How to recover from server crash

I have looked all over the place and have never seen an example of how to do this, even tho it's alluded to in blogs. But examples are never given.
I have a .Net remoting client that is connected to a server which is published as a well known type over tcp/ip. It holds an object reference to the server and instantiates it via the new operator.
If the server crashes, the easy part is detecting this, just catch the error.
Looping and waiting for the server to come back up will again be no problem.
But how to reset / reboot / unregister then re-register the remoting stack on the client?!?
If the client is restarted after the server comes back up, it works again, but obviously I want to just reconnect within the client application and not have to reboot it.
Windows Communication Foundation does not have the same issue. When the server goes down, the connection can be programatically re-established.

Detecting when Firefox is showing a Server not found message

I'm using Firefox for a digital signage application, and there a couple of scenarios where a Server not found might result.
Network outage on boot
DNS fails to resolve for the homepage
Server (its homepage) fails to respond
Boots and the network just isn't ready by the time Firefox is loaded
Browser crashes, process is restarted, but the network is down
In such cases I would like to detect this state and simply kill and restart the process after a minute. Any other tweaks or suggestions, I'm all ears.
You do not need to consider the case whereby the loaded Web application loses Internet connectivity. That scenario I think has been handled by the Web app itself, once it has loaded.
I don't want to go down the local httpd or local extension/addon route.
Thank you in advance,
I've discovered a simple solution of overriding Firefox netError.xhtml to do a simple location.reload every ten seconds.
Source code can be found from https://github.com/Webconverger/iceweasel-webconverger/blob/master/content/netError.xhtml#L410

Should you always disconnect from a WCF service between calls?

I am using a WCF service to administer a windows service running on a remote machine. We have an administration client we use for modifying the windows service configuration, as well as monitoring the state of the service in real-time. For the real-time monitoring, we poll the service for it's state every second.
Currently, we are leaving the client connected all the time while monitoring the service but I continually read that it is recommended to connect and disconnect for each call, much like you would for a database.
Would it be recommended in our situation where we are making frequent calls to the service, or would connecting and disconnecting add too much overhead to the process?
Thanks
By default and as a recommended best practice, you're using the per-call activation in WCF, e.g. each request to your WCF service gets a new instance of a service class, that instance handles your request, returns a result, and then is disposed.
In this case, I don't really see any point in constantly breaking and re-establishing the communication channel (e.g. constantly disposing and re-creating the proxy client). There's nothing on the WCF service side that "lingers around" in memory and takes up resources or anything like that. Also, contrary to most databases, there's usually no "per-connection" licensing or anything involved, either.
What you need to be able to deal with in this scenario would be a situation where your communication channel goes into "faulted state", e.g. when something bad happens - when the service call fails and throws an exception, or when a network fluke causes your channel to break. In such a case, you need to have recovery mechanisms on your client side to handle this and re-establish the connection again.
The situation might be a bit different if you have session-oriented WCF services - but those should definitely be the exception, and only used when needed by all means.

Resources