public void Listener()// A Thread that runs and polls for commands from server. Process commands and return results { StivLibrary.DataCollectors SDC = new DataCollectors(); ThreadClass TC = new ThreadClass(); string strHostName = ""; strHostName = System.Net.Dns.GetHostName(); IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; NetTcpBinding bindbert = new NetTcpBinding(); bindbert.Security.Mode = SecurityMode.None; var ConsoleContract = new ChannelFactory <TaskConsoleWCFService.ContractDefinition>(bindbert); var ConsoleService = ConsoleContract.CreateChannel(new EndpointAddress(GV.ConsoleEndPoint)); try//Just a quick test to make sure the server responds.... { string getthread = ConsoleService.GetThreadData(); } catch { GV.CurrentActionForLog = "Listener croaked over trying to hit the service endpoint. \n"; GV.Whackme = true; GV.ServerCommunicationReceived = DateTime.Now; } while (!GV.Whackme) { try { TaskConsoleWCFService.Jobber myjob = ConsoleService.GetJob(Dns.GetHostName(), SDC.GetHostIP(Dns.GetHostName())); GV.ServerCommunicationReceived = DateTime.Now; // Here is where we need to process any recieved jobs. if (myjob.Taskname.Equals("No jobs for joo!")) { if (GV.Verbose) { GV.CurrentActionForLog = "No new jobs for me at: " + GV.ConsoleEndPoint; } } else { myjob.timerecieved = DateTime.Now; myjob.status = "Recieved"; ConsoleService.UpdateJob(myjob); GV.Tasklist.Add(myjob.ThisTaskGuid, myjob); GV.CurrentActionForLog = "Recieved new job " + myjob.Taskname; LeWorker Lew = new LeWorker(); Lew.Job2Do = myjob; Thread Wthread = new Thread(new ThreadStart(Lew.StartWorking)); Wthread.IsBackground = true; Wthread.Name = "StivAgent Worker Thread"; Wthread.Start(); } //GV.CurrentActionForLog = ConsoleService.GetArgsToPass().ToString(); } catch { GV.CurrentActionForLog = "Lost communication to Console!"; GV.Whackme = true; } Thread.Sleep(5000); //5 Second wait to poll. } }
static void Main(string[] args) { CommandLineArgs ARGH = new CommandLineArgs(); ARGH.IgnoreCase = true; ARGH.PrefixRegexPatternList.Add("/{1}"); ARGH.PrefixRegexPatternList.Add("-{1,2}"); ARGH.ProcessCommandLineArgs(args); //Argument processing: // We need arguments for the following: // 1: -IP // 2: -Port // 3: -Verbose // -endpoint net.tcp://10.160.147.48:8383/TaskConsole/ -verbose string myendpoint = @"net.tcp://" + ARGH["ip"] + ":" + ARGH["port"] + @"/StivTaskConsole/"; string path; path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); GV.Exedir = path.Remove(0, 6) + @"\"; GV.LogFile = path.Remove(0, 6) + @"\_StivAgent.log"; if (File.Exists(GV.LogFile)) { File.Delete(GV.LogFile); } GV.CurrentActionForLog = "Starting up! Connecting unto " + myendpoint; NetTcpBinding bindbert = new NetTcpBinding(); bindbert.Security.Mode = SecurityMode.None; GV.ConsoleContract = new ChannelFactory <TaskConsoleWCFService.ContractDefinition>(bindbert); GV.ConsoleEndPoint = myendpoint; if (ARGH.ContainsSwitch("verbose")) { GV.Verbose = true; } else { GV.Verbose = false; } //This agent handles system requests from the Task Console. //Options -StopServices [servicelist] -StartServices [servicelist] -RestartServices [servicelist] -Restart -CheckWSUSUpdates -IISRESET -IISSTOP -IISSTART -InstallUpdates // -AddUsers [Userlist] -AddAdmins [UserList] -DisableUsers [Userlist] -EnableUsers [Userlist] // -Terminate //http://www.nullskull.com/a/1592/install-windows-updates-using-c--wuapi.aspx ThreadClass TC = new ThreadClass(); Thread WhackerThread = new Thread(new ThreadStart(TC.Whacker)) { IsBackground = true }; WhackerThread.Start(); Thread ListenThread = new Thread(new ThreadStart(TC.Listener)) { IsBackground = true }; ListenThread.Start(); Thread.Sleep(System.Threading.Timeout.Infinite); }