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
+3 -2
View File
@@ -47,7 +47,7 @@ Configure database
------------------
Now adjust create_data.sql with real values. For host rows:
name is name used to query with ntpdc. Must match name/ip in ntp.conf
name is name used to query with ntpdc. Must match name/ip in ntpa.conf
ip is IP address as shown in generated web pages
orgId is ID from support.ntp.org. Use 10000 or above for non registered servers
@@ -76,7 +76,8 @@ To stop ntpa use kill. Find ntpa pid in log file.
# kill 77342
GTK GUI
Run Ntp.Analyzer.Gui.exe and connect to ntpa with the a port specified in the listener section.
Run Ntp.Analyzer.Gui.exe and connect to ntpa with the a port specified
in the listener section.
Connect to ntpa and get a list of available commands:
# ntpac 127.0.0.1 9070 help
+22 -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
@@ -23,10 +23,11 @@
// 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.Runtime.InteropServices;
using Ntp.Analyzer.Process;
using System.Text;
using System;
using System.Runtime.InteropServices;
using System.Text;
using Ntp.Analyzer.Process;
using Ntp.Process;
namespace Ntp.Analyzer.Cli
{
@@ -35,7 +36,8 @@ namespace Ntp.Analyzer.Cli
/// </summary>
public static class MainClass
{
[DllImport("libc")]
#if __MonoCS__
[DllImport ("libc")]
private static extern int getpid ();
[DllImport ("libc")] // Linux
@@ -62,6 +64,9 @@ namespace Ntp.Analyzer.Cli
}
}
}
#else
private static int getpid () { return 0; }
#endif
/// <summary>
/// The entry point of the program, where the program control starts and ends.
@@ -69,10 +74,20 @@ namespace Ntp.Analyzer.Cli
/// <param name="args">The command-line arguments.</param>
public static void Main (string[] args)
{
string name = (args.Length > 1) ? args [1] : "NTPA_DEFAULT";
if (args.Length >= 1 && args [0] == "stop") {
string message = InterLock.Release (name);
if (message != null) {
Console.WriteLine ("Could not stop daemon.");
}
return;
}
string configFile = (args.Length > 0) ? args [0] : "/etc/ntpa.conf";
int pid = getpid ();
Main main = new Main (configFile, pid);
Main main = new Main (configFile, pid, name);
main.Start ();
}
}
+6 -1
View File
@@ -23,9 +23,10 @@
<OutputPath>..\bin\ntpa\Debug</OutputPath>
<WarningLevel>4</WarningLevel>
<LangVersion>4</LangVersion>
<Commandlineparameters>/home/hawk/code/ntp/config/ntpa-test.conf</Commandlineparameters>
<Externalconsole>true</Externalconsole>
<DocumentationFile>..\bin\ntpa\Debug\ntpa.xml</DocumentationFile>
<DebugSymbols>true</DebugSymbols>
<Commandlineparameters>~/code/ntpa/example/ntpa.stat.conf</Commandlineparameters>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@@ -45,5 +46,9 @@
<Project>{266A828E-05CA-4FEB-8B78-0BC066760C26}</Project>
<Name>Ntp.Analyzer.Process</Name>
</ProjectReference>
<ProjectReference Include="..\Ntp.Process\Ntp.Process.csproj">
<Project>{CCF0EE0C-E3E4-4A75-AFEB-C709ED51E90F}</Project>
<Name>Ntp.Process</Name>
</ProjectReference>
</ItemGroup>
</Project>
+5 -1
View File
@@ -50,11 +50,13 @@ namespace Ntp.Analyzer.Process
/// </summary>
/// <param name="configFile">Config file to use.</param>
/// <param name="pid">Process ID. Only used for logging.</param>
public Initializer (string version, string configFile, int pid)
/// <param name="name">Name of process ID. Used for shutting down.</param>
public Initializer (string version, string configFile, int pid, string name)
{
this.version = version;
this.configFile = configFile;
this.pid = pid;
this.name = name;
nodes = new List<IRequest> ();
listeners = new List<Listener> ();
@@ -63,6 +65,7 @@ namespace Ntp.Analyzer.Process
private readonly string version;
private readonly string configFile;
private readonly int pid;
private readonly string name;
private readonly List<IRequest> nodes;
private readonly List<Listener> listeners;
private Configuration config;
@@ -172,6 +175,7 @@ namespace Ntp.Analyzer.Process
// Initialize application state
ApplicationState.Version = version;
ApplicationState.Pid = pid;
ApplicationState.Name = name;
ApplicationState.Config = config;
ApplicationState.ConfigFile = configFile;
ApplicationState.Log = log;
+35
View File
@@ -0,0 +1,35 @@
//
// Killer.cs
//
// Author:
// carsten <${AuthorEmail}>
//
// Copyright (c) 2016 carsten
//
// 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.Threading;
namespace Ntp.Analyzer.Process
{
public sealed class Killer
{
public Killer
}
}
+7 -5
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
@@ -30,29 +30,31 @@ namespace Ntp.Analyzer.Process
{
public sealed class Main
{
public Main (string configFile, int pid)
public Main (string configFile, int pid, string name)
{
// Also update solution version.
version = "8.8.5";
version = "8.8.6";
this.configFile = configFile;
this.pid = pid;
this.name = name;
}
private readonly string version;
private readonly string configFile;
private readonly int pid;
private readonly string name;
public void Start ()
{
Initializer i = new Initializer (version, configFile, pid);
Initializer i = new Initializer (version, configFile, pid, name);
i.Run ();
if (!i.Ready) {
return;
}
Cluster c = new Cluster (i.Scheduler, i.Nodes, i.Log);
Cluster c = new Cluster (name, i.Scheduler, i.Nodes, i.Log);
c.Activate ();
}
}
@@ -35,6 +35,7 @@
<Reference Include="MonoDevelop.Ide, Version=2.6.0.0, Culture=neutral">
<Package>monodevelop</Package>
</Reference>
<Reference Include="Mono.Posix" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
+1 -1
View File
@@ -122,7 +122,7 @@ Global
GlobalSection(NestedProjects) = preSolution
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Ntp.Analyzer.Gui\Ntp.Analyzer.Gui.csproj
StartupItem = Ntp.Analyzer.Cli\Ntp.Analyzer.Cli.csproj
Policies = $0
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = PrefixedHierarchical
+1
View File
@@ -37,6 +37,7 @@ namespace Ntp.Monitor.Server
public static string ConfigFile;
public static string Version;
public static int Pid;
public static string Name;
public static Configuration Config;
public static LogBase Log;
public static IScheduler Scheduler;
+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);