interprocess communication

This commit is contained in:
2016-03-13 00:56:33 +01:00
parent a1af555c5d
commit 939f7a5abe
13 changed files with 249 additions and 26 deletions
+42 -7
View File
@@ -4,7 +4,7 @@
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -28,29 +28,45 @@ using System.Collections.Generic;
using Ntp.Analyzer.Log;
using System.IO;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;
namespace Ntp.Process
{
public sealed class Cluster
{
public Cluster (Scheduler scheduler, IEnumerable<IRequest> peers, LogBase log)
public Cluster (
string name,
Scheduler scheduler,
IEnumerable<IRequest> peers,
LogBase log)
{
this.name = name;
this.scheduler = scheduler;
this.peers = peers;
this.log = log;
waitHandle = new EventWaitHandle (false, EventResetMode.AutoReset);
}
private readonly string name;
private readonly Scheduler scheduler;
private readonly IEnumerable<IRequest> peers;
private readonly LogBase log;
private readonly EventWaitHandle waitHandle;
private bool run;
public void Activate ()
{
log.WriteLine ("Starting cluster module.", Severity.Info);
Thread killSwitch = new Thread (KillSwitch);
killSwitch.Start ();
run = true;
// TODO: Move to public scope (ApplicationState)
bool activated = true;
while (true) {
while (run) {
bool otherActive = false;
@@ -76,7 +92,7 @@ namespace Ntp.Process
}
if (!otherActive) {
//log.WriteLine ("No other nodes active. Activting node.", Severity.Notice);
log.WriteLine ("No other nodes active. Activting node.", Severity.Debug);
try {
scheduler.RunOneCycle ();
@@ -86,13 +102,32 @@ namespace Ntp.Process
return;
}
} else {
//log.WriteLine ("Other node active. Going to fallback mode.", Severity.Notice);
Thread.Sleep (10000);
log.WriteLine ("Other node active. Going to fallback mode.", Severity.Debug);
waitHandle.WaitOne (10000);
}
activated = false;
}
}
public void KillSwitch ()
{
try {
string message = InterLock.Wait (name);
if (message != null) {
log.WriteLine ("Error in mutux: " + message, Severity.Error);
}
waitHandle.Set ();
scheduler.WaitHandle.Set ();
run = false;
} catch (Exception e) {
log.WriteLine ("Error in mutux. Arborting.", Severity.Error);
log.WriteLine (e.ToString (), Severity.Error);
}
log.WriteLine ("Closing down.", Severity.Notice);
}
}
}
+109
View File
@@ -0,0 +1,109 @@
//
// InterLock.cs
//
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Ntp.Process
{
#if __MonoCS__
using Mono.Unix;
public static class InterLock
{
public static string Release (string name)
{
try {
UnixEndPoint endPoint = new UnixEndPoint (name);
Socket socket = new Socket (AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
socket.Connect (endPoint);
byte[] bytes = Encoding.UTF8.GetBytes ("RELEASE");
socket.Send (bytes);
socket.Close ();
} catch (Exception e) {
return e.Message;
}
return null;
}
public static string Wait (string name)
{
UnixEndPoint endPoint = new UnixEndPoint (name);
Socket socket = new Socket (AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
byte[] buf = new byte[64];
try {
socket.Bind (endPoint);
socket.Listen (5);
Socket sock = socket.Accept ();
sock.Receive (buf);
sock.Close ();
} catch (Exception e) {
return e.Message;
} finally {
socket.Close ();
}
return null;
}
}
#else
public static class InterLock
{
public static string Release (string name)
{
try {
EventWaitHandle handle = EventWaitHandle.OpenExisting (name);
handle.Set ();
} catch (Exception e) {
return e.Message;
}
return null;
}
public static string Wait (string name)
{
try {
EventWaitHandle handle = new EventWaitHandle (
false, EventResetMode.ManualReset,
name);
handle.WaitOne ();
} catch (Exception e) {
return e.Message;
}
return null;
}
}
#endif
}
+2
View File
@@ -31,6 +31,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Mono.Posix" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -44,6 +45,7 @@
<Compile Include="Scheduler.cs" />
<Compile Include="IRequest.cs" />
<Compile Include="Cluster.cs" />
<Compile Include="InterLock.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
+15 -2
View File
@@ -4,7 +4,7 @@
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -49,6 +49,8 @@ namespace Ntp.Process
firstRun = true;
active = false;
waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
schedule = new List<ScheduledJob> ();
jobs = new List<Job> ();
activityLog = new ActivityLog ();
@@ -60,6 +62,7 @@ namespace Ntp.Process
this.log = logGroup;
}
private readonly EventWaitHandle waitHandle;
private readonly ActivityLog activityLog;
private readonly List<Job> jobs;
private readonly List<ScheduledJob> schedule;
@@ -87,6 +90,14 @@ namespace Ntp.Process
return jobs.GetEnumerator ();
}
/// <summary>
/// Gets the wait handle of this <see cref="Scheduler" />.
/// </summary>
/// <value>The wait handle.</value>
public EventWaitHandle WaitHandle {
get { return waitHandle; }
}
/// <summary>
/// Gets the startup time of this <see cref="Scheduler" />.
/// </summary>
@@ -189,7 +200,9 @@ namespace Ntp.Process
wait = 0;
}
Thread.Sleep (wait);
bool signal = waitHandle.WaitOne (wait);
if (signal)
return;
if (next.Job.Description.SingleThread && schedule.Count (j => j.Job.Description.SingleThread && j.Job.Running) != 0) {
PostponeJob (next.Job);