示例#1
0
        public void sendInitialLogs(object sender, ClientEventArgs e)
        {
            List <String> logs = AppConfigAdapter.getInitialLogs(this.logger);
            Task          task = new Task(() =>
            {
                foreach (ClientHandler ch in this.clientHandlers)
                {
                    //if ((ClientHandler)sender == ch)
                    if (this.clientIDs[ch] == e.id)
                    {
                        try
                        {
                            ch.writeListOfStringsToClient(logs, this.logger);
                        }
                        catch (Exception ex)
                        {
                            logger.Log("couldn't write logs to client", MessageTypeEnum.FAIL);
                            mut.WaitOne();
                            this.clientHandlers.Remove(ch);
                            this.clientIDs.Remove(ch);
                            mut.ReleaseMutex();
                        }
                    }
                }
            });

            task.Start();
        }
        public static List <string> getInitialLogs(ILogging logger)
        {
            List <string> logs  = new List <string>();
            EventLog      myLog = new EventLog();

            myLog.Log = ConfigurationSettings.AppSettings.Get("LogName");
            int i = 0;
            int lengthOfLogInLogs = 0, lengthOfCurrentLog;
            int size = myLog.Entries.Count;

            //foreach (EventLogEntry entry in myLog.Entries)
            for (int j = myLog.Entries.Count - 1; j >= 0; j--)
            {
                String entry = AppConfigAdapter.changeToString(myLog.Entries[j]);
                //logger.Log("in first entry: {$entry.Message}", MessageTypeEnum.INFO);
                if (logs.Any()) // if logs isn't empty
                {
                    //lengthOfCurrentLog = entry.Message.Length;
                    lengthOfCurrentLog = myLog.Entries[j].Message.Length;
                    if (lengthOfLogInLogs + lengthOfCurrentLog > 100)
                    { // if sending two logs together is too big
                        i++;
                        logs.Add(entry);
                        lengthOfLogInLogs = lengthOfCurrentLog;
                    }
                    else // sum of lengths <= 100
                    {    // concatenate last string with current string
                        //String temp = String.Concat("*", entry);
                        //logs[i] = String.Concat(logs[i], temp);
                        logs[i]           += "*" + entry;
                        lengthOfLogInLogs += lengthOfCurrentLog;
                    }
                }
                else   // list is still empty
                {
                    logs.Add(entry);
                    lengthOfLogInLogs = myLog.Entries[j].Message.Length;
                }
            }
            return(logs);
        }
示例#3
0
        public void Start()
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);

            listener = new TcpListener(ep);
            listener.Start();
            //Console.WriteLine("Waiting for connections...");
            Task task = new Task(() => {
                while (true)
                {
                    try
                    {
                        TcpClient client    = listener.AcceptTcpClient();
                        ClientHandler ch    = new ClientHandler(mut, count);
                        ch.settingsWritten += sendInitialLogs;
                        MutexOfCount.WaitOne();
                        count++;
                        MutexOfCount.ReleaseMutex();
                        clientHandlers.Add(ch);
                        clientIDs.Add(ch, count - 1);
                        ch.setStream(client);
                        ch.InitialWritingHappened += StartListening;
                        ch.InitialWritingHappened += StartWritingLogs;
                        //Console.WriteLine("Got new connection");
                        //this.logger.Log("about to get Initial logs", MessageTypeEnum.INFO);

                        String settings = AppConfigAdapter.getStringOfConfiguration();
                        ch.writeStringToClient(settings);
                    }
                    catch (Exception)
                    {
                        logger.Log("couldn't send settings or set client", MessageTypeEnum.FAIL);
                    }
                }
            });

            task.Start();
        }