108 lines
3.7 KiB
C#
108 lines
3.7 KiB
C#
using System.Net.Sockets;
|
|
using static InnovEnergy.Logging.Settings;
|
|
|
|
namespace InnovEnergy.Logging;
|
|
|
|
public static class Server
|
|
{
|
|
|
|
// TODO: notify about n skipped identical messages in last period
|
|
|
|
|
|
private static UdpClient _incomingSocket = new UdpClient(IncomingEndPoint); // TODO: directly use socket
|
|
|
|
public static void Main()
|
|
{
|
|
// Init();
|
|
//
|
|
// ImmediateScheduler.Instance
|
|
// .Repeat(ReceiveErrorMessage)
|
|
// .Do(LogReceived)
|
|
// .ThrottleDistinct(m => m.title, EmailSubjectThrottle)
|
|
// .Try(CreateMimeMessage)
|
|
// .ThenTry(SendMail)
|
|
// .Match(LogSent, LogError)
|
|
// .IgnoreErrors()
|
|
// .Subscribe();
|
|
//
|
|
// Console.WriteLine("Logging Server exited abnormally!");
|
|
}
|
|
|
|
// private static void Init()
|
|
// {
|
|
// "Logging Server started".Underline().Write();
|
|
//
|
|
// "Listening on " .Write(); IncomingEndPoint.WriteLine(Cyan);
|
|
// "Sending errors to " .Write(); Recipient.WriteLine(Cyan);
|
|
// "Throttling period is ".Write(); (EmailSubjectThrottle.TotalHours + "h").WriteLine(Cyan);
|
|
//
|
|
// TestMailServerConnection();
|
|
// }
|
|
//
|
|
// private static void TestMailServerConnection()
|
|
// {
|
|
// "Testing connection to mail server ".Write();
|
|
// SmtpServer.Host.Write(Cyan); ":".Write(Cyan); SmtpServer.Port.Write(Cyan);
|
|
// " ... ".Write();
|
|
//
|
|
// var errorMsg = Try(() => SendMail(null))
|
|
// .Catch(e => e.Message);
|
|
//
|
|
// if (errorMsg != null)
|
|
// {
|
|
// "FAILURE".WriteLine(Red);
|
|
// errorMsg.WriteLine(Red);
|
|
// "Logging Server stopped".WriteLine();
|
|
// Environment.Exit(2);
|
|
// }
|
|
//
|
|
// "success".WriteLine(Green);
|
|
// }
|
|
//
|
|
// private static void LogReceived((String title, String message) m) => Console.WriteLine("RECEIVED: " + m.title);
|
|
// private static void LogError(Exception ex) => Console.WriteLine("ERROR : " + ex);
|
|
// private static void LogSent(String msg) => Console.WriteLine("SENT : " + msg);
|
|
//
|
|
// private static MimeMessage CreateMimeMessage((String title, String message) m)
|
|
// {
|
|
// return new MimeMessage
|
|
// {
|
|
// Subject = m.title,
|
|
// Sender = SenderAddress,
|
|
// To = {RecipientAddress},
|
|
// Body = new TextPart(TextFormat.Plain) { Text = m.message }
|
|
// };
|
|
// }
|
|
//
|
|
// private static (String title, String message) ReceiveErrorMessage()
|
|
// {
|
|
// return Try(ReadDatagram) .OnError("META: Failed to read from socket")
|
|
// .ThenTry(Extensions.Deserialize) .OnError("META: Failed parse error datagram")
|
|
// .Catch(ResetSocket);
|
|
// }
|
|
//
|
|
// private static IReadOnlyList<Byte> ReadDatagram() => _incomingSocket.ReadDatagram().Payload;
|
|
//
|
|
//
|
|
// private static String SendMail(MimeMessage message)
|
|
// {
|
|
// using var smtp = new SmtpClient();
|
|
//
|
|
// smtp.Connect(SmtpServer.Host, SmtpServer.Port, false);
|
|
// smtp.Authenticate(SmtpUser, SmtpPassword);
|
|
//
|
|
// if (message != null) smtp.Send(message);
|
|
//
|
|
// smtp.Disconnect(true);
|
|
//
|
|
// return message?.Subject;
|
|
// }
|
|
//
|
|
// private static (String, String) ResetSocket(Exception e)
|
|
// {
|
|
// _incomingSocket.Dispose();
|
|
// _incomingSocket = new UdpClient(IncomingEndPoint);
|
|
//
|
|
// return (e.Message, e.ToString());
|
|
// }
|
|
} |