/// <summary> /// a factory method which creates the listener and makes it start listening. /// </summary> /// <param name="appName"></param> /// <returns></returns> public static void StartListening(string appName, LinkEventHandler handler) { s_appName = appName; FwBroker broker = FwBroker.GetBroker(); s_port = broker.RegisterAndGetPort(appName); FwLinkListener.OnLinkRequest += handler; ChannelServices.RegisterChannel(new TcpChannel(s_port)); RemotingConfiguration.ApplicationName = appName; RemotingConfiguration.RegisterWellKnownServiceType(typeof(FwLinkListener), "listener", WellKnownObjectMode.Singleton ); //although we don't really need to use it, we create this object now //so that we can preload with appropriate properties. string path = FwLinkListener.GetPortPath(s_port, appName); FwLinkListener listener = (FwLinkListener)Activator.GetObject(typeof(FwLinkListener), path); if (listener == null) { throw new ApplicationException("Could not create the initial listener for this application"); } listener.Test(); }
public static FwBroker GetBroker() { //about tempChannel: this is the result of a better frustrated attempts to //understanding control automatic vs. explicit channel creation. //our current design calls for the broker to tell us what port we should use. //however, even in talking to the broker, we apparently get a port set up //by .net, automatically. This then prevents us from moving to the port we were assigned //by the broker. So, for now, the best clue to have come up with is to //just grab an arbitrary, hardcoded port temporarily for use in getting in touch with the broker. //then, we can register this port before relief this method. If, when this method //is entered, we already have a channel registered, then don't bother with the temporary //port at all. TcpChannel tempChannel = null; try { if (ChannelServices.RegisteredChannels.Length == 0) { ChannelServices.RegisterChannel(new TcpChannel(7129)); tempChannel = (TcpChannel)ChannelServices.RegisteredChannels[0]; } FwBroker broker = (FwBroker)Activator.GetObject(typeof(FwBroker), Url); //unfortunately, that GetObject call will appear to have succeeded even if it //actually did not get us anything useful. The only way I have figured out //how to check that it really work is to actually make a call on the proxy it returned. try { broker.Test(); return(broker); } catch (Exception error) { Process p = Process.Start(@"C:\WW\Src\Common\Linker\Broker\bin\Debug\Broker.exe"); broker = (FwBroker)Activator.GetObject(typeof(FwBroker), Url); broker.Test(); return(broker); } } finally { if (tempChannel != null) { ChannelServices.UnregisterChannel(tempChannel); } } return(null); }
/// <summary> /// Attempts to find an application which matches the link, and passes the link to that application. /// Does not attempt to launch new applications. /// </summary> /// <param name="link"></param> /// <returns>true if it successfully linked to a running application</returns> public static bool AttemptLink(FwLink link) { try { FwBroker broker = FwBroker.GetBroker(); int port = broker.GetRunningApplicationPort(link.ApplicationName); //note that this will not fail, even if no one is listening on that channel FwLinkListener listener = (FwLinkListener)Activator.GetObject(typeof(FwLinkListener), GetPortPath(port, link.ApplicationName)); //but this will fail if we did not really connect to a listener listener.Request(link); return(true); } catch (Exception error) { } return(false); }
/* /// <summary> * /// * /// </summary> * /// <param name="applicationName"></param> * /// <returns></returns> * protected static int RegisterNextAvailablePortForApplication (string applicationName) * { * string[] paths= new string[kMaxApplicationsOfOneType]; * //find a port that is not in use * int start =GetStartingPortForApplication(applicationName); * for(int port = start; port< start + kMaxApplicationsOfOneType;port++) * { * try * { * ChannelServices.RegisterChannel(new TcpChannel(port)); * } * catch(System.Net.Sockets.SocketException) * { * continue; * } * return port; * } * throw new ApplicationException("Could not find an open port for this application to use."); * } * * protected static string[] GetPossiblePathsToApplication(string applicationName) * { * int start = GetStartingPortForApplication(applicationName); * string[] paths= new string[kMaxApplicationsOfOneType]; * for(int port = start; port < start + kMaxApplicationsOfOneType; port++) * { * paths[port-start] = GetPortPath(port, applicationName); * } * return paths; * } */ public static void StopListening() { FwBroker broker = FwBroker.GetBroker(); broker.Unregister(s_appName, s_port); }