コード例 #1
0
 public ComPortWithThreshold(ComPortWithThresholdSettings settings,
                             Func <List <byte>, bool> packetRcvAction,
                             Action <ComPortErrorNames, Exception> errorCallback)
     : base(settings, errorCallback)
 {
     try
     {
         this.settings     = settings;
         packetRcv         = packetRcvAction ?? throw new ArgumentNullException(nameof(packetRcvAction));
         Buffer            = new Queue <byte>();
         checkBufferThread = new RepeatedTask(checkBuffer, 1);
         checkBufferThread.Start();
     }
     catch (Exception ex)
     {
         raiseError(ComPortErrorNames.OnConstruction, ex);
     }
 }
コード例 #2
0
        private void ThreadInitialize()
        {
            var configKey = RegistryPath.Open(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId");

            var log_path = (string)configKey.GetValue("log_path");
            var log      = new LogTagged(new LogFileWeekly(log_path, true), 1);

            log.TagWithDateTime = true;
            Log.Replace(log);

            Log.Inform(">>>>> STARTING <<<<<");

            var firewall_address  = (string)configKey.GetValue("firewall_address");
            var firewall_username = (string)configKey.GetValue("firewall_username");
            var firewall_api_key  = (string)configKey.GetValue("firewall_api_key");

            MapUserIp.New(new PanXmlApi(firewall_address, firewall_username, firewall_api_key));

            var dhcpFileFilter        = new FileFilterRecent(new FileFilterChanged());
            var dhcpWatcher           = new FolderWatchers(dhcpFileFilter);
            var dhcp_logfile_wildcard = (string)configKey.GetValue("dhcp_logfile_wildcard");

            foreach (var dhcp_log_paths in ((string)configKey.GetValue("dhcp_log_paths")).Split(';'))
            {
                dhcpWatcher.AddWatcher(dhcp_log_paths, dhcp_logfile_wildcard);
            }
            var dhcpHandler     = AssembleDhcpRecordHandler(new DsvLineConfig());
            var dhcpFileManager = new FileFollowerManager(dhcpHandler, token);
            var dhcpLogs        = new FolderWatcherManager(dhcpWatcher, dhcpFileManager, token);

            var iasFileFilter        = new FileFilterRecent(new FileFilterChanged());
            var iasWatcher           = new FolderWatchers(iasFileFilter);
            var ias_logfile_wildcard = (string)configKey.GetValue("ias_logfile_wildcard");

            foreach (var ias_log_paths in ((string)configKey.GetValue("ias_log_paths")).Split(';'))
            {
                iasWatcher.AddWatcher(ias_log_paths, ias_logfile_wildcard);
            }
            var iasHandler     = AssembleIasRecordHandler(new DsvLineConfig());
            var iasFileManager = new FileFollowerManager(iasHandler, token);
            var iasLogs        = new FolderWatcherManager(iasWatcher, iasFileManager, token);

            var dhcpTask = dhcpLogs.ProcessAsync();
            var IasTask  = iasLogs.ProcessAsync();

            Func <bool> checkIfAllOldLogsRead = () => {
                return(!(dhcpFileManager.AreAllFollowing && iasFileManager.AreAllFollowing));
            };
            Func <bool> wait15msecs = () => {
                const int _15_msecs = 15;
                Task      taskDelay = null;
                try     { taskDelay = Task.Delay(_15_msecs, token); taskDelay.Wait(); return(true); }
                catch   { return(false); }
                finally { if (taskDelay != null)
                          {
                              taskDelay.Dispose();
                          }
                }
            };
            var blockUntilAllOldLogsRead = new RepeatedTask(checkIfAllOldLogsRead, wait15msecs);

            blockUntilAllOldLogsRead.Process();

            if (token.IsCancellationRequested)
            {
                return;
            }

            Log.Inform(">>>>> FOLLOWING <<<<<");
            var updater = PaloAltoUserIdUpdater.Instance;

            updater.LoginCached();

            if (token.IsCancellationRequested)
            {
                return;
            }

            Func <bool> maintainDatabases = () => {
                Log.Inform(">>>>> MAINTAIN DATABASES <<<<<");
                updater.RemoveStaleEntries();
                return(true);
            };
            Func <bool> wait1day = () => {
                TimeSpan _1_day    = TimeSpan.FromDays(1);
                Task     taskDelay = null;
                try     { taskDelay = Task.Delay(_1_day, token); taskDelay.Wait(); return(true); }
                catch   { return(false); }
                finally { if (taskDelay != null)
                          {
                              taskDelay.Dispose();
                          }
                }
            };
            var maintainDatabasesEveryDay = new RepeatedTask(maintainDatabases, wait1day);

            maintainDatabasesEveryDay.Process();
        }