This example can be found in examples\csharp\restserver\restserver.cs
. When the PST SDK has been installed through the PST Software Suite installer the examples can be found in the Development folder.
This example shows how to enable the REST server using the PST SDK. The REST server enables network-based access to the PST Tracker using the HTTP protocol. GET and POST requests can be made to the server to send and receive data and change parameters. The REST interface offers a programming language independent interface to the PST Tracker. Besides accessing the REST server directly using a browser, a networking library like Curl can be used to interface with the server programatically.
When compiling and running this example, please make sure that the required dependencies can be found by the executable (e.g. by copying the Redist
directory into the build directory. When the PST SDK has been installed through the PST Software Suite installer the Redist folder can be found in the Development folder.).
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace PSTech.RestServer
{
public class RestServer
{
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleEventHandler handler, bool add);
private delegate bool ConsoleEventHandler(CtrlType sig);
static ConsoleEventHandler ConsoleCtrlEventHandler;
public enum CtrlType
{
CtrlCEvent = 0,
CtrlBreakEvent = 1,
CtrlCloseEvent = 2,
CtrlLogOffEvent = 5,
CtrlShutdownEvent = 6,
}
private static bool ConsoleHandler(CtrlType sig)
{
Exithandler((int)sig);
return true;
}
static void Exithandler(int sig)
{
Tracker.DisableRestServer();
Tracker.Shutdown();
Environment.Exit(0);
}
static int Main(string[] args)
{
ConsoleCtrlEventHandler += new ConsoleEventHandler(ConsoleHandler);
SetConsoleCtrlHandler(ConsoleCtrlEventHandler, true);
try
{
Tracker tracker = new Tracker();
tracker.StartTracker();
Tracker.EnableRestServer("localhost", "7278");
Console.Write("PST REST server enabled. See the PST SDK Manual for example commands.\n");
Thread.Sleep(60000);
Tracker.DisableRestServer();
}
{
Console.Write(e + "\n");
}
finally
{
Tracker.Shutdown();
}
return 0;
}
}
}