Skip to content

Commit

Permalink
Added possibility to start PySSSMQ-server from within KSP_MOCR.
Browse files Browse the repository at this point in the history
Warning: The server will not stop by itself, it will keep running even if KSP_MOCR is terminated.
  • Loading branch information
Tunefix committed Feb 24, 2018
1 parent b87bc1c commit c7167a2
Show file tree
Hide file tree
Showing 60 changed files with 67,120 additions and 87 deletions.
Binary file modified KSP_MOCR/.vs/KSP_MOCR/v15/.suo
Binary file not shown.
Binary file modified KSP_MOCR/.vs/KSP_MOCR/v15/sqlite3/storage.ide
Binary file not shown.
34 changes: 30 additions & 4 deletions KSP_MOCR/Classes/DataStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ public DataStorage(PySSSMQ_client client)
{
this.client = client;
}

// Used when you wish to update data in the dataStorage, and transmit to PySSSMQ-clients

/// <summary>
/// Used when you wish to update data in the dataStorage, and transmit to PySSSMQ-clients
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setData(String key, String value)
{
storeData(key, value);
Expand All @@ -23,7 +27,12 @@ public void setData(String key, String value)
}
}

// Used when PySSSMQ-client receives data
/// <summary>
/// Store data in local storage. This is called when PySSSMQ-client receives data.
/// If in doubt, use setData();
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void storeData(String key, String value)
{
//Console.WriteLine("Storing Data: " + key + ", " + value);
Expand All @@ -37,6 +46,11 @@ public void storeData(String key, String value)
}
}

/// <summary>
/// Get data from local storage
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public String getData(String key)
{
//Console.WriteLine("Getting Data: " + key);
Expand All @@ -51,7 +65,19 @@ public String getData(String key)
}
}

// Fetch all data
/// <summary>
/// Get a copy of the entire local storage
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetDictionary()
{
return new Dictionary<string, string>(storage);
}

/// <summary>
/// Get all data from server and store it locally.
/// Most usefull to sync up at start.
/// </summary>
public void Pull()
{
client.Pull("&");
Expand Down
6 changes: 4 additions & 2 deletions KSP_MOCR/Classes/MocrScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ public static MocrScreen Create(int id, Screen form)
return new MapScreen(form);
case 9:
Console.WriteLine("MAKING TEST");
return new TestScreen(form);
return new TestScreen(form);
case 11:
return new StreamsScreen(form);
return new StreamsScreen(form);
case 12:
return new DataStorageScreen(form);
case 50:
return new Terrain(form);
case 51:
Expand Down
14 changes: 8 additions & 6 deletions KSP_MOCR/Classes/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public Screen(Form1 form, int id, Connection connection, StreamCollection stream
screenTimer.AutoReset = false;
screenTimer.Interval = 1000;
screenTimer.Elapsed += screenTick;
}
}

public void screenTick(object sender, EventArgs e)
{
//Console.WriteLine("Starting ScreenTick");
{
//Console.WriteLine("Starting ScreenTick");
updateStart = DateTime.Now;

if (activeScreen != null)
Expand All @@ -93,8 +93,10 @@ public void screenTick(object sender, EventArgs e)
TimeSpan updateDuration = updateEnd - updateStart;
int remainTime = activeScreen.updateRate - (int)updateDuration.TotalMilliseconds;

if (remainTime < 100) { remainTime = 100; }
Console.WriteLine("Remain Time: " + remainTime.ToString() + ", Time Spent: " + ((int)updateDuration.TotalMilliseconds).ToString() + ", ScrID: " + this.ID);
if (remainTime < 50) {
remainTime = 50;
Console.WriteLine("LOW REMAIN TIME: " + remainTime.ToString() + ", Time Spent: " + ((int)updateDuration.TotalMilliseconds).ToString() + ", ScrID: " + this.ID);
}

screenTimer.Interval = remainTime;
screenTimer.Start();
Expand Down
72 changes: 36 additions & 36 deletions KSP_MOCR/Classes/StreamCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public sealed class StreamCollection
private bool hasConnection = false;

private System.Object streamlock = new System.Object();

// Some much used variables

// Some much used variables
KRPC.Client.Services.SpaceCenter.Service spaceCenter;
Flight flight;
Vessel vessel;
Expand All @@ -33,22 +33,22 @@ public sealed class StreamCollection
ReferenceFrame surfaceRefFrame;
ReferenceFrame mapRefFrame;
Flight inertFlight;
Flight mapFlight;

static StreamCollection()
{
Flight mapFlight;

static StreamCollection()
{
}

private StreamCollection()
{
}

private StreamCollection()
{
}

public static StreamCollection Instance
{
get
{
return instance;
}
public static StreamCollection Instance
{
get
{
return instance;
}
}

public StreamCollection(Connection con)
Expand All @@ -62,26 +62,26 @@ public dynamic GetData(DataType type, bool force_reStream)
{
if (hasConnection)
{
lock (streamlock)
{
if (!streams.ContainsKey(type) || force_reStream)
{
// If forced, clear out old stream
if (force_reStream) { streams[type].Remove(); streams.Remove(type); }

try
{
addStream(type);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
Kstream stream = streams[type];
dynamic output = stream.Get();
return output;
lock (streamlock)
{
if (!streams.ContainsKey(type) || force_reStream)
{
// If forced, clear out old stream
if (force_reStream) { streams[type].Remove(); streams.Remove(type); }

try
{
addStream(type);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
Kstream stream = streams[type];
dynamic output = stream.Get();
return output;
}
}
else
Expand Down
96 changes: 93 additions & 3 deletions KSP_MOCR/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
using System.IO;
using System.Net;
using System.Threading;

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Diagnostics;
using Microsoft.Win32;

namespace KSP_MOCR
{

Expand All @@ -28,6 +32,7 @@ public partial class Form1 : Form

public PySSSMQ_client pySSSMQ = new PySSSMQ_client();
public PySSSMQ_Handler pySSSMQ_handler;
public ScriptSource pySSSMQ_server;

public DataStorage dataStorage;

Expand Down Expand Up @@ -77,12 +82,14 @@ enum Resource { ElectricCharge, MonoPropellant, LiquidFuel, Oxidizer, Ore };

public System.Timers.Timer screenTimer;
public System.Timers.Timer graphTimer;
public System.Timers.Timer statusCheck;

public StreamCollection streamCollection = StreamCollection.Instance;

TextBox ipAddr;
TextBox name;
CustomLabel pySSSMQStatus;
CustomLabel pySSSMQStatus2;
CustomLabel kRPCStatus;

public Form1()
Expand Down Expand Up @@ -194,6 +201,14 @@ private void Form1_Load(object sender, EventArgs e)
graphTimer.Elapsed += graphTick;
graphTimer.Start();

// Initiate Status Check
statusCheck = new System.Timers.Timer();
statusCheck.SynchronizingObject = this;
statusCheck.AutoReset = true;
statusCheck.Interval = 1000;
statusCheck.Elapsed += statusCheckTick;
statusCheck.Start();


// Load the connection screen
//SetScreen(0);
Expand Down Expand Up @@ -321,8 +336,48 @@ private void fillForm()
pySSSMQStatus.FlatStyle = FlatStyle.Flat;
pySSSMQStatus.BorderStyle = BorderStyle.None;
pySSSMQStatus.ForeColor = foreColor;
this.Controls.Add(pySSSMQStatus);

this.Controls.Add(pySSSMQStatus);

// PySSSMQ BUTTON
MocrButton pybutton = new MocrButton();
pybutton.Location = getPoint(1, 17);
pybutton.Size = getSize(24, 1);
pybutton.Cursor = Cursors.Hand;
pybutton.Font = font;
pybutton.Text = "Start PySSSMQ-Server";
pybutton.Padding = new Padding(0);
pybutton.Click += StartPyServer;
this.Controls.Add(pybutton);

MocrButton pybuttonS = new MocrButton();
pybuttonS.Location = getPoint(1, 18);
pybuttonS.Size = getSize(24, 1);
pybuttonS.Cursor = Cursors.Hand;
pybuttonS.Font = font;
pybuttonS.Text = "Stop PySSSMQ-Server";
pybuttonS.Padding = new Padding(0);
pybuttonS.Click += StopPyServer;
this.Controls.Add(pybuttonS);

// PySSSMQ-server status
pySSSMQStatus2 = new CustomLabel();
pySSSMQStatus2.setCharWidth(pxPrChar);
pySSSMQStatus2.setlineHeight(pxPrLine);
pySSSMQStatus2.setcharOffset(charOffset);
pySSSMQStatus2.setlineOffset(lineOffset);
pySSSMQStatus2.Font = font;
pySSSMQStatus2.AutoSize = false;
pySSSMQStatus2.TextAlign = ContentAlignment.TopCenter;
pySSSMQStatus2.Location = getPoint(1, 16);
pySSSMQStatus2.Size = getSize(42, 5);
pySSSMQStatus2.Text = "PySSSMQ-Server: NOT RUNNUNG";
pySSSMQStatus2.Padding = new Padding(0);
pySSSMQStatus2.Margin = new Padding(0);
pySSSMQStatus2.FlatStyle = FlatStyle.Flat;
pySSSMQStatus2.BorderStyle = BorderStyle.None;
pySSSMQStatus2.ForeColor = foreColor;
this.Controls.Add(pySSSMQStatus2);

}

private void checkForEnter(object sender, KeyEventArgs e)
Expand All @@ -349,6 +404,21 @@ private Size getSize(int x, int y)
return new Size(width, height);
}

public void statusCheckTick(object sender, EventArgs e)
{
//Process[] pname = Process.GetProcesses();

Process[] pname = Process.GetProcessesByName("pythonw");
if (pname.Length > 0)
{
pySSSMQStatus2.Text = "PySSSMQ-Server: RUNNING";
}
else
{
pySSSMQStatus2.Text = "PySSSMQ-Server: NOT RUNNING";
}
}

public void graphTick(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
Expand Down Expand Up @@ -439,6 +509,26 @@ public void ConnectToServer(object sender, EventArgs e)
}
}

public void StartPyServer(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.WorkingDirectory = System.Environment.CurrentDirectory;
p.StartInfo.FileName = "python\\pythonw.exe";
p.StartInfo.Arguments = " PySSSMQ_server.py";
p.StartInfo.UseShellExecute = true;
p.Start();
}

public void StopPyServer(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.WorkingDirectory = System.Environment.CurrentDirectory;
p.StartInfo.FileName = "python\\pythonw.exe";
p.StartInfo.Arguments = " PySSSMQ_server.py stop";
p.StartInfo.UseShellExecute = true;
p.Start();
}

public void DisconnectFromServer(object sender, EventArgs e)
{
connection.Dispose();
Expand Down
25 changes: 25 additions & 0 deletions KSP_MOCR/KSP_MOCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,33 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="IronPython, Version=2.7.8.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\IronPython.2.7.8\lib\net45\IronPython.dll</HintPath>
</Reference>
<Reference Include="IronPython.Modules, Version=2.7.8.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\IronPython.2.7.8\lib\net45\IronPython.Modules.dll</HintPath>
</Reference>
<Reference Include="IronPython.SQLite, Version=2.7.8.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\IronPython.2.7.8\lib\net45\IronPython.SQLite.dll</HintPath>
</Reference>
<Reference Include="IronPython.Wpf, Version=2.7.8.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\IronPython.2.7.8\lib\net45\IronPython.Wpf.dll</HintPath>
</Reference>
<Reference Include="KRPC.Client, Version=0.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\KRPC.Client.0.4.4\lib\net45\KRPC.Client.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Dynamic, Version=1.2.0.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\DynamicLanguageRuntime.1.2.1\lib\net45\Microsoft.Dynamic.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting, Version=1.2.0.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\DynamicLanguageRuntime.1.2.1\lib\net45\Microsoft.Scripting.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting.Metadata, Version=1.2.0.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
<HintPath>packages\DynamicLanguageRuntime.1.2.1\lib\net45\Microsoft.Scripting.Metadata.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.CodeCoverage.Shim, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\DynamicLanguageRuntime.1.2.1\lib\net45\Microsoft.VisualStudio.CodeCoverage.Shim.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms.DataVisualization" />
Expand All @@ -104,6 +128,7 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Screens\DataStorageScreen.cs" />
<Compile Include="Screens\Pilot1.cs" />
<Compile Include="Screens\StreamsScreen.cs" />
<Compile Include="Screens\Terrain.cs" />
Expand Down
Loading

0 comments on commit c7167a2

Please sign in to comment.