private HubRouter CreateHub(string root, string name, string cloudEP) { const string settings = @" MsgRouter.RouterEP = physical://{0}/{1} MsgRouter.ParentEP = MsgRouter.DiscoveryMode = MULTICAST MsgRouter.CloudEP = {2} MsgRouter.CloudAdapter = ANY MsgRouter.UdpEP = ANY:0 MsgRouter.TcpEP = ANY:0 MsgRouter.TcpBacklog = 100 MsgRouter.TcpDelay = off MsgRouter.BkInterval = 1s MsgRouter.MaxIdle = 5m MsgRouter.AdvertiseTime = 30s MsgRouter.KeepAliveTime = 1m MsgRouter.DefMsgTTL = 5 MsgRouter.SharedKey = PLAINTEXT MsgRouter.SessionCacheTime = 2m MsgRouter.SessionRetries = 3 MsgRouter.SessionTimeout = 10s MsgRouter.DownlinkEP[0] = logical://* "; HubRouter router; Config.SetConfig(string.Format(settings, root, name, cloudEP)); router = new HubRouter(); router.Dispatcher.AddTarget(this); router.Dispatcher.AddLogical(new MsgHandlerDelegate(OnMsg), "logical://" + name, typeof(PropertyMsg), false, null); router.Start(); return(router); }
/// <summary> /// Starts the service, associating it with an <see cref="IServiceHost" /> instance. /// </summary> /// <param name="serviceHost">The service user interface.</param> /// <param name="args">Command line arguments.</param> public void Start(IServiceHost serviceHost, string[] args) { lock (syncLock) { if (router != null) { return; // Already started } // Global initialization NetTrace.Start(); Program.Config = new Config("LillTek.Datacenter.RouterService"); Program.PerfCounters = null; // $todo(jeff.lill): new PerfCounterSet(true,Const.RouterServicePerf,Const.RouterServiceName); // Service initialization this.serviceHost = serviceHost; try { RootRouter rootRouter; HubRouter hubRouter; switch (Program.Config.Get("Mode", "HUB").ToUpper()) { case "ROOT": SysLog.LogInformation("Router Service v{0}: Starting as ROOT", Helper.GetVersionString()); router = rootRouter = new RootRouter(); rootRouter.Start(); break; case "HUB": default: SysLog.LogInformation("Router Service v{0}: Starting as HUB", Helper.GetVersionString()); router = hubRouter = new HubRouter(); hubRouter.Start(); break; } state = ServiceState.Running; } catch (Exception e) { if (router != null) { router.Stop(); router = null; } SysLog.LogException(e); throw; } } }
public void RouterService_RootMode() { // Verify that the router service actually deploys a root router // by starting an instance of the application and a couple of // hub routers and making sure that the service is routing messages // between the two hubs. Process svcProcess = null; HubRouter hub1 = null; HubRouter hub2 = null; ConfigRewriter rewriter; Assembly assembly; string iniPath; assembly = typeof(LillTek.Datacenter.RouterService.Program).Assembly; iniPath = Config.GetConfigPath(assembly); rewriter = new ConfigRewriter(iniPath); try { // Rewrite the config file to have it start in root mode // and then start the router service as a form application. rewriter.Rewrite(new ConfigRewriteTag[] { new ConfigRewriteTag("Mode", "#undef HUBMODE\r\n#define ROOTMODE\r\n#define HUBNAME hub\r\n") }); svcProcess = Helper.StartProcess(assembly, "-mode:form -start"); Thread.Sleep(10000); // Give the process a chance to spin up // Crank up a couple of hub routers and send a message from // one to the other. hub1 = CreateHub(Helper.MachineName + ":" + Const.DCRootPort.ToString(), "hub1", group1); hub2 = CreateHub(Helper.MachineName + ":" + Const.DCRootPort.ToString(), "hub2", group2); Thread.Sleep(2000); recvMsg = null; hub1.SendTo("logical://hub2", new PropertyMsg()); Thread.Sleep(1000); Assert.IsNotNull(recvMsg); } finally { rewriter.Restore(); if (svcProcess != null) { svcProcess.Kill(); svcProcess.Close(); } if (hub1 != null) { hub1.Stop(); } if (hub2 != null) { hub2.Stop(); } } }