/// <summary> /// Handle the service start event by reading the settings file and starting all specified proxy instances. /// </summary> protected override void OnStart(string[] args) { radioClient = new FIpcClient(); radioClient.Initialize(s.UIPort); try { //Initialize Workers int w = (int)FConstants.WorkerType.CPU; try { if (s.MinerSwitch[w] == true) { Workers[w] = new Worker((FConstants.WorkerType)w, s, radioClient, logger); Workers[w].AlwaysRun = RegSetting.hasRight(FConstants.FeatureByte.AlwaysRun) ? true : false; Workers[w].Enable = true; logger.WriteLine($@"[Service] Initialize Worker {w} AlwaysRun:{Workers[w].AlwaysRun}|Enable:{Workers[w].Enable}"); } w = (int)FConstants.WorkerType.AMD; if (s.MinerSwitch[w] == true) { Workers[w] = new Worker((FConstants.WorkerType)w, s, radioClient, logger); Workers[w].AlwaysRun = RegSetting.hasRight(FConstants.FeatureByte.AlwaysRun) ? true : false; Workers[w].Enable = true; logger.WriteLine($@"[Service] Initialize Worker {w} AlwaysRun:{Workers[w].AlwaysRun}|Enable:{Workers[w].Enable}"); } w = (int)FConstants.WorkerType.nVidia; if (s.MinerSwitch[w] == true) { Workers[w] = new Worker((FConstants.WorkerType)w, s, radioClient, logger); Workers[w].AlwaysRun = RegSetting.hasRight(FConstants.FeatureByte.AlwaysRun) ? true : false; Workers[w].Enable = true; logger.WriteLine($@"[Service] Initialize Worker {w} AlwaysRun:{Workers[w].AlwaysRun}|Enable:{Workers[w].Enable}"); } } catch (Exception ex) { logger.WriteLine($@"[Service] Initialize Worker {w} fail, excpetion: {ex.Message}"); } //Initialize IPC Server, try 3 times for (int i = 0; i < 3; i++) { int serverPort = FFunc.FreePortHelper.GetFreePort(13000); FFunc.SetRegKey("ServicePort1", serverPort); try { radioServer = new IpcServer(); radioServer.Start(serverPort); radioServer.ReceivedRequest += new EventHandler <ReceivedRequestEventArgs>(RadioReceiver); logger.WriteLine($@"[Service] Service Radio started at port {serverPort}"); break; } catch (Exception ex) { logger.WriteLine($@"[Service] Service Start radioServer at port {serverPort} fail, excpetion: {ex.Message}"); } } FFunc.SetRegKey("FreyaDirectory", System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase); StartProxies(); CleanupMiner(); logger.WriteLine("[Service] FreyaService Initialized."); } catch (Exception ex) { logger.WriteLine("[Service] FreyaService Service Start Fail:" + ex.Message); if (radioClient != null) { radioClient.Send(JsonConvert.SerializeObject(new FMsg { Type = "MSG", Data = "[DBG] Service Start Fail:" + ex.Message, Loglevel = FConstants.FreyaLogLevel.FreyaInfo })); } } }
private void RadioReceiver(object sender, ReceivedRequestEventArgs args) { FMsg fMsg = JsonConvert.DeserializeObject <FMsg>(args.Request); if (fMsg.Type.Equals("CMD")) { Console.WriteLine("Get CMD: " + fMsg.Data); switch (fMsg.Data) { case "SetIdleTime": foreach (Worker w in Workers) { if (w != null) { w.SetIdleTime(Convert.ToDouble(fMsg.Data2)); } } args.Response = JsonConvert.SerializeObject(s); break; case "GetWorkerStatus": string workerstatus = ""; foreach (Worker w in Workers) { if (w != null) { if (w.Type == FConstants.WorkerType.CPU) { workerstatus += (w.isRunning)? "C:" : "c:"; } else if (w.Type == FConstants.WorkerType.AMD) { workerstatus += (w.isRunning) ? "A:" : "a:"; } else if (w.Type == FConstants.WorkerType.nVidia) { workerstatus += (w.isRunning) ? "N:" : "n:"; } workerstatus = workerstatus + w.Message + "\n"; } } args.Response = workerstatus; break; case "MinerDisable": foreach (Worker w in Workers) { if (w != null) { w.Enable = false; } } s.MinerEnable = false; args.Response = JsonConvert.SerializeObject(s); break; case "MinerEnable": foreach (Worker w in Workers) { if (w != null) { w.Enable = true; } } s.MinerEnable = true; args.Response = JsonConvert.SerializeObject(s); break; case "GetStatus": for (int w = 0; w < Workers.Length; w++) { if (Workers[w] != null) { s.MinerIsActive[w] = Workers[w].isRunning; } else { s.MinerIsActive[w] = false; } } logger.WriteLine($"[Service] s.MinerIsActive {s.MinerIsActive[0]}|{s.MinerIsActive[1]}|{s.MinerIsActive[2]}"); args.Response = JsonConvert.SerializeObject(s); break; case "SetUIPort": s.UIPort = Convert.ToInt32(fMsg.Data2); radioClient.SetPort(s.UIPort); FFunc.SetRegKey("FreyaUIPort", s.UIPort); logger.WriteLine($"[Service] Receive UI Port at {s.UIPort}"); args.Response = JsonConvert.SerializeObject(s); break; case "RadioTest": string ss = radioClient.Send(JsonConvert.SerializeObject(new FMsg { Type = "MSG", Data = $"Service to UI radio test ... [Service Server Port {radioServer.Port}], [UI Port {radioClient.GetPort()}]", Loglevel = FConstants.FreyaLogLevel.FreyaInfo })); args.Response = $"[Service] s.MinerIsActive {s.MinerIsActive[0]}|{s.MinerIsActive[1]}|{s.MinerIsActive[2]}"; logger.WriteLine($"[Service] RadioTest sUIPort: {s.UIPort}, GetPort:{radioClient.GetPort()}"); break; case "StartProxy": StartProxies(); args.Response = "OK"; break; case "StopProxy": StopProxies(); args.Response = "OK"; break; case "WriteRegistry": if (fMsg.Data2.Length > 0) { RegSetting.SetSettingsToRegisry(fMsg.Data2); args.Response = "OK"; logger.WriteLine("[Service] Options writed to registry."); } else { args.Response = "NG"; } s.UpdateMinerSwitch(); foreach (Worker w in Workers) { if (w != null) { w.AlwaysRun = RegSetting.hasRight(FConstants.FeatureByte.AlwaysRun) ? true : false; } } string strbuf = ""; foreach (bool s in s.MinerSwitch) { strbuf = strbuf + s.ToString() + " |"; } logger.WriteLine($"[Service] WorkerSwitch : {strbuf}"); break; default: args.Response = "SCNG"; break; } } else { args.Response = "SCNG1"; } args.Handled = true; }