Skip to content

Commit

Permalink
Refactor shutdown service
Browse files Browse the repository at this point in the history
  • Loading branch information
fahminlb33 committed Jun 10, 2020
1 parent c7d8ff7 commit 4e072d7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 150 deletions.
149 changes: 0 additions & 149 deletions src/KFlearning.Core/Services/RemoteServer.cs

This file was deleted.

101 changes: 101 additions & 0 deletions src/KFlearning.Core/Services/RemoteShutdownServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SOLUTION : KFlearning
// PROJECT : KFlearning.Core
// FILENAME : RemoteServer.cs
// AUTHOR : Fahmi Noor Fiqri, Kodesiana.com
// WEBSITE : https://kodesiana.com
// REPO : https://github.com/Kodesiana or https://github.com/fahminlb33
//
// This file is part of KFlearning, see LICENSE.
// See this code in repository URL above!

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace KFlearning.Core.Services
{
public interface IRemoteShutdownServer : IDisposable
{
void Listen();
void SendShutdown(string cluster);

event EventHandler<ShutdownRequestedEventArgs> ShutdownRequested;
}

public class RemoteShutdownServer : IRemoteShutdownServer
{
public const int ServicePort = 2021;
private const string MessageShutdown = "SDWN";

private static readonly IPEndPoint ServiceEndpoint = new IPEndPoint(IPAddress.Any, ServicePort);

private readonly UdpClient _socket = new UdpClient();
private MemoryStream _uploadStream;

public event EventHandler<ShutdownRequestedEventArgs> ShutdownRequested;
public event EventHandler<ShutdownRequestedEventArgs> FileBroadcast;

public RemoteShutdownServer()
{
_socket.Client.Bind(ServiceEndpoint);
}

#region Public Methods

public void Listen()
{
_socket.BeginReceive(ReceiveCallback, null);
}

public void SendShutdown(string cluster)
{
Send(MessageShutdown, cluster);
}

#endregion

#region Private Methods (Callbacks)

private void Send(string message, string body)
{
var data = Encoding.ASCII.GetBytes($"{message}|{body}");
_socket.BeginSend(data, data.Length, ServiceEndpoint, SendCallback, null);
}

private void SendCallback(IAsyncResult ar)
{
_socket.EndSend(ar);
}

private void ReceiveCallback(IAsyncResult ar)
{
var epFrom = new IPEndPoint(IPAddress.Any, ServicePort);
var bytes = _socket.EndReceive(ar, ref epFrom);
var message = Encoding.ASCII.GetString(bytes);
ProcessMessage(message, epFrom.Address);

_socket.BeginReceive(ReceiveCallback, null);
}

private void ProcessMessage(string message, IPAddress server)
{
if (!message.StartsWith(MessageShutdown)) return;
var cluster = message.Split('|')[1];
ShutdownRequested?.Invoke(this, new ShutdownRequestedEventArgs { Address = server, Clusster = cluster });
}

#endregion

#region IDisposable

public void Dispose()
{
((IDisposable)_socket)?.Dispose();
((IDisposable)_uploadStream)?.Dispose();
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

namespace KFlearning.Core.Services
{
public class RemoteEventArgs : EventArgs
public class ShutdownRequestedEventArgs : EventArgs
{
public IPAddress Address { get; set; }
public string Clusster { get; set; }
}
}

0 comments on commit 4e072d7

Please sign in to comment.