static void Main(string[] args) { string logFile = null; string settingsFile = null; string autoPurge = null; string numberOfFiles = null; // take a list of strings (refer to PathToData) and add it to a dictionary of paths + save the value to use Func <string, string, string, string> CheckOption = (string arg, string option, string data) => { try { if ((arg.StartsWith(option, true, null)) && option.Length < arg.Length && string.IsNullOrEmpty(data)) { return(arg.Substring(option.Length)); } return(data); } catch (Exception) { } return(null); }; // get start options foreach (string s in args) { // has the settings file been specified //if ((s.StartsWith(OPTION_SETTINGS_FILE_NAME, true, null)) && OPTION_SETTINGS_FILE_NAME.Length < s.Length && string.IsNullOrEmpty(settingsFile)) //settingsFile = s.Substring(OPTION_SETTINGS_FILE_NAME.Length); // has the log file been specified //if ((s.StartsWith(OPTION_LOG_FILE_NAME, true, null)) && OPTION_LOG_FILE_NAME.Length < s.Length && string.IsNullOrEmpty(logFile)) // logFile = s.Substring(OPTION_LOG_FILE_NAME.Length); settingsFile = CheckOption(s, OPTION_SETTINGS_FILE_NAME, settingsFile); logFile = CheckOption(s, OPTION_LOG_FILE_NAME, logFile); numberOfFiles = CheckOption(s, OPTION_LOG_NUMBER_OF_FILES, numberOfFiles); autoPurge = CheckOption(s, OPTION_LOG_AUTO_PURGE, autoPurge); } if (string.IsNullOrEmpty(settingsFile)) { settingsFile = LISTENER_SETTINGS_FILE; } if (string.IsNullOrEmpty(logFile)) { logFile = LISTENER_LOG_FILE; } try { CLog.AutoPurgeLogFiles = bool.Parse(autoPurge); } catch { CLog.AutoPurgeLogFiles = true; } try { CLog.NumberOfFilesToKeep = int.Parse(numberOfFiles); } catch { CLog.NumberOfFilesToKeep = 3; } CLog.LogFileName = logFile; CLogger.Add($"Using settings file: {settingsFile}"); MenuList menu = new MenuList(); menu.Add(TEST_LISTENER, new CMenu() { Text = "Test listener", Fnc = TestListener }); menu.Add(TEST_LISTENER_CREATE, new CMenu() { Text = "Generate listener test file", Fnc = TestListenerCreate }); menu.Add(DISPLAY_SETTINGS, new CMenu() { Text = "Display settings", Fnc = DisplaySettings }); menu.Add(RELOAD_SETTINGS, new CMenu() { Text = "Reload settings", Fnc = ReloadSettings }); //menu.Add(i++.ToString()[0], new CMenu() { Text = ACTIVATE_DISPLAY, Fnc = ActivityDisplay }); menu.Add('X', new CMenu() { Text = "Exit", Fnc = Exit }); // Start listener CListener listener = new CListener(); bool ok = listener.Start(settingsFile); TestListenerType testListenerType = new TestListenerType() { FileToUse = LISTENER_TEST_FILE, Port = listener.Port, IP = listener.IP }; while (ok) { CMenu entry = DisplayMenu(menu, out char option); if (null != entry) { switch (option) { case RELOAD_SETTINGS: { ReloadSettingsType type = new ReloadSettingsType() { SettingsFile = settingsFile, Listener = listener }; if (ok = entry.Fnc(menu, entry, option, type)) { settingsFile = type.SettingsFile; listener = type.Listener; } break; } case TEST_LISTENER: { ok = entry.Fnc(menu, entry, option, testListenerType); break; } case DISPLAY_SETTINGS: { ok = entry.Fnc(menu, entry, option, listener); break; } default: ok = entry.Fnc(menu, entry, option, null); break; } } } listener.Stop(); }
/// <summary> /// /// </summary> /// <param name="menu"></param> /// <param name="entry"></param> /// <param name="option"></param> /// <param name="o"></param> /// <returns></returns> static bool TestListener(MenuList menu, CMenu entry, char option, object o) { TestListenerType type = (TestListenerType)o; type.FileToUse = Input("File to use", LISTENER_TEST_FILE, out bool isdef); if (string.IsNullOrEmpty(type.FileToUse)) { Console.WriteLine("Invalid test file"); return(true); } type.IP = Input("Listener IP", CStream.Localhost(), out isdef); if (string.IsNullOrEmpty(type.IP)) { Console.WriteLine("Invalid IP"); return(true); } string sport = Input("Listener port to reach", type.Port.ToString(), out isdef); if (string.IsNullOrEmpty(sport)) { return(true); } try { type.Port = uint.Parse(sport); } catch (Exception) { Console.WriteLine("Invalid port number"); return(true); } //var json = new CJson<CListenerRequest>(fileToUse); //var request = json.ReadSettings(out bool except); //if (null != request) //{ // var sreply = CStream.ConnectSendReceive(new CStreamClientSettings() { IP = ip, Port = port }, CJson<CListenerRequest>.Serialize(request), out int size, out bool error); // if (!string.IsNullOrEmpty(sreply)) // { // CLogger.Add(sreply); // var reply = CJson<CListenerReply>.Deserialize(sreply, out except); // CLogger.Add(reply.ToString()); // } // else // CLogger.Add("No reply has been received"); //} var json = new CJson <CListenerRequest>(type.FileToUse); var request = json.ReadSettings(out bool except); if (null != request) { CStreamClientIO streamIO = CStream.Connect(new CStreamClientSettings() { IP = type.IP, Port = type.Port }); if (null != streamIO) { if (CStream.Send(streamIO, CJson <CListenerRequest> .Serialize(request))) { string sreply; while (!string.IsNullOrEmpty(sreply = CStream.Receive(streamIO, out int announcedSize, out bool error))) { var reply = CJson <CListenerReply> .Deserialize(sreply, out except); CLogger.Add($"{reply.ToString()} (received message)"); if (!reply.Notification) { break; } } } } } return(true); }