Skip to content

Commit

Permalink
Major changes to comms (#79)
Browse files Browse the repository at this point in the history
* Set AskForReloadAPIList to false as default

* Split API and Command JSON events

* Set AskForReloadAPIList to false as default

* Version bump

* Separate API and Command handling

* Add words to dictionary

* Add HSL color functionality

* Apply changed interface name

* Add functionality for separate API/command msg

JSON error parsing now is show in the bottom bar instead of showing a dialog. Error should be recoverable.
Fixed polling not working.
  • Loading branch information
jdahlblom authored Apr 11, 2024
1 parent 44cf7f7 commit e316da1
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/client/DCSInsight/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<value>False</value>
</setting>
<setting name="AskForReloadAPIList" serializeAs="String">
<value>True</value>
<value>False</value>
</setting>
</DCSInsight.Properties.Settings>
</userSettings>
Expand Down
99 changes: 66 additions & 33 deletions src/client/DCSInsight/Communication/TCPClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ internal class TCPClientHandler : IDisposable, ICommandListener
private bool _isRunning;
private readonly string _host;
private readonly string _port;
private bool _apiListReceived;
private int _metaDataPollCounter;
public bool LogJSON { get; set; }
public bool LogJSON { get; set; }
private string _currentMessage = "";
private volatile bool _responseReceived;
private bool _requestAPIList;

public TCPClientHandler(string host, string port, bool requestAPIList)
public TCPClientHandler(string host, string port)
{
_host = host;
_port = port;
_requestAPIList = requestAPIList;
ICEventHandler.AttachCommandListener(this);
}

Expand All @@ -47,6 +43,39 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public void RequestAPIList()
{
if (_tcpClient == null) return;

try
{
if (!_tcpClient.Connected) return;

Thread.Sleep(300);
_tcpClient.GetStream().Write(Encoding.ASCII.GetBytes("SENDAPI\n"));
Thread.Sleep(1000);

var bytes = new byte[_tcpClient.Available];
var bytesRead = _tcpClient.GetStream().Read(bytes);
var msg = Encoding.ASCII.GetString(bytes);
if (LogJSON) Logger.Info(msg);
HandleAPIMessage(msg);
Thread.Sleep(100);
}
catch (SocketException ex)
{
Logger.Error(ex);
return;
}
}

public void StartListening()
{
_isRunning = true;
_clientThread = new Thread(ClientThread);
_clientThread.Start();
}

private void ClientThread()
{
if (_tcpClient == null) return;
Expand Down Expand Up @@ -78,15 +107,6 @@ private void ClientThread()

if (!_tcpClient.Connected) break;

if (_requestAPIList && !_apiListReceived && _metaDataPollCounter < 1)
{
Thread.Sleep(300);
_metaDataPollCounter++;
_tcpClient.GetStream().Write(Encoding.ASCII.GetBytes("SENDAPI\n"));
Thread.Sleep(1000);
_requestAPIList = false;
}

if (_commandsQueue.Count > 0 && _responseReceived)
{
if (!_commandsQueue.TryDequeue(out var dcsApi)) continue;
Expand All @@ -103,7 +123,7 @@ private void ClientThread()
var bytesRead = _tcpClient.GetStream().Read(bytes);
var msg = Encoding.ASCII.GetString(bytes);
if (LogJSON) Logger.Info(msg);
HandleMessage(msg);
HandleCommandMessage(msg);
Thread.Sleep(100);
}
catch (SocketException ex)
Expand All @@ -117,24 +137,30 @@ private void ClientThread()
_tcpClient = null;
ICEventHandler.SendConnectionStatus(_isRunning);
}
private void HandleMessage(string str)

private void HandleCommandMessage(string str)
{
try
{
if (!_apiListReceived)
{
HandleAPIMessage(str);
return;
}

if (str.Contains("\"returns_data\":") && str.EndsWith("}")) // regex?
{
var dcsApi = JsonConvert.DeserializeObject<DCSAPI>(_currentMessage + str);
DCSAPI? dcsApi;
try
{
dcsApi = JsonConvert.DeserializeObject<DCSAPI>(_currentMessage + str);
}
catch (Exception e)
{
_currentMessage = "";
_responseReceived = true;
ICEventHandler.SendCommsErrorMessage("Error parsing JSON (API)", e);
return;
}

if (dcsApi == null) return;

_currentMessage = "";
ICEventHandler.SendData(dcsApi);
ICEventHandler.SendCommandData(dcsApi);
_responseReceived = true;
}
else
Expand All @@ -152,12 +178,22 @@ private void HandleAPIMessage(string str)
{
try
{
var dcsAPIList = JsonConvert.DeserializeObject<List<DCSAPI>>(str);
List<DCSAPI>? dcsAPIList;
try
{
dcsAPIList = JsonConvert.DeserializeObject<List<DCSAPI>>(str);
}
catch (Exception e)
{
_responseReceived = true;
ICEventHandler.SendCommsErrorMessage("Error parsing JSON (API List)", e);
return;
}

if (dcsAPIList == null) return;

ICEventHandler.SendData(dcsAPIList);
ICEventHandler.SendAPIData(dcsAPIList);
_responseReceived = true;
_apiListReceived = true;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -185,9 +221,6 @@ public void Connect()
_isRunning = false;
_tcpClient = new TcpClient();
_tcpClient.Connect(serverEndPoint);
_isRunning = true;
_clientThread = new Thread(ClientThread);
_clientThread.Start();
}
catch (Exception ex)
{
Expand All @@ -213,7 +246,7 @@ public void AddCommand(DCSAPI dcsApi)
{
_commandsQueue.Enqueue(dcsApi);
}

public void SendCommand(SendCommandEventArgs args)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/client/DCSInsight/DCSInsight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- for WinForms -->
<AssemblyName>dcs-insight</AssemblyName>
<Version>1.0.0</Version>
<AssemblyVersion>1.9.3</AssemblyVersion>
<AssemblyVersion>1.10.0</AssemblyVersion>
<ApplicationIcon>Images\Magnifier_icon.ico</ApplicationIcon>
<Company>DCS-Skunkworks</Company>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/client/DCSInsight/DCSInsight.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DCSAPI/@EntryIndexedValue">DCSAPI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DCSAPIS/@EntryIndexedValue">DCSAPIS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DCSBIOS/@EntryIndexedValue">DCSBIOS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=HSL/@EntryIndexedValue">HSL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IC/@EntryIndexedValue">IC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=JSON/@EntryIndexedValue">JSON</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LED/@EntryIndexedValue">LED</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RGB/@EntryIndexedValue">RGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TCP/@EntryIndexedValue">TCP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/XamlNaming/Abbreviations/=API/@EntryIndexedValue">API</s:String>
Expand Down
26 changes: 23 additions & 3 deletions src/client/DCSInsight/Events/EventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,40 @@ public ErrorEventArgs(string message, Exception ex)
public Exception Ex { get; }
}

public class CommsErrorEventArgs : EventArgs
{
public CommsErrorEventArgs(string shortMessage, Exception ex)
{
ShortMessage = shortMessage;
Ex = ex;
}

public string ShortMessage { get; }

public Exception Ex { get; }
}

public class ConnectionEventArgs : EventArgs
{
public bool IsConnected { get; init; }
}

public class DataEventArgs : EventArgs
public class CommandDataEventArgs : EventArgs
{
public DataEventArgs(DCSAPI? dcsApi, List<DCSAPI>? dcsapis)
public CommandDataEventArgs(DCSAPI dcsApi)
{
DCSApi = dcsApi;
DCSAPIS = dcsapis;
}

public DCSAPI? DCSApi { get; }
}

public class APIDataEventArgs : EventArgs
{
public APIDataEventArgs(List<DCSAPI> dcsapis)
{
DCSAPIS = dcsapis;
}

public List<DCSAPI>? DCSAPIS { get; }
}
Expand Down
58 changes: 47 additions & 11 deletions src/client/DCSInsight/Events/ICEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,64 @@ public static void SendConnectionStatus(bool isConnected)
OnConnection?.Invoke(new ConnectionEventArgs { IsConnected = isConnected});
}
/*
*
* For handling API list
*/
public delegate void APIDataEventHandler(APIDataEventArgs e);
public static event APIDataEventHandler? OnAPIData;

public static void AttachAPIDataListener(IAPIDataListener listener)
{
OnAPIData += listener.APIDataReceived;
}

public static void DetachAPIDataListener(IAPIDataListener listener)
{
OnAPIData -= listener.APIDataReceived;
}

public static void SendAPIData(List<DCSAPI> dcsAPIs)
{
OnAPIData?.Invoke(new APIDataEventArgs(dcsAPIs));
}
/*
* For handling back and forth command data
*/
public delegate void DataEventHandler(DataEventArgs e);
public static event DataEventHandler? OnData;
public delegate void CommandDataEventHandler(CommandDataEventArgs e);
public static event CommandDataEventHandler? OnCommandData;

public static void AttachDataListener(IDataListener listener)
public static void AttachCommandDataListener(ICommandDataListener listener)
{
OnData += listener.DataReceived;
OnCommandData += listener.CommandDataReceived;
}

public static void DetachDataListener(IDataListener listener)
public static void DetachCommandDataListener(ICommandDataListener listener)
{
OnData -= listener.DataReceived;
OnCommandData -= listener.CommandDataReceived;
}

public static void SendData(List<DCSAPI> dcsAPIs)
public static void SendCommandData(DCSAPI dcsAPI)
{
OnData?.Invoke(new DataEventArgs(null, dcsAPIs ));
OnCommandData?.Invoke(new CommandDataEventArgs(dcsAPI));
}
public static void SendData(DCSAPI dcsAPI)
/*
*
*/
public delegate void CommsErrorEventHandler(CommsErrorEventArgs e);
public static event CommsErrorEventHandler? OnCommsError;

public static void AttachCommsErrorListener(ICommsErrorListener listener)
{
OnCommsError += listener.CommsErrorMessage;
}

public static void DetachCommsErrorListener(ICommsErrorListener listener)
{
OnCommsError -= listener.CommsErrorMessage;
}

public static void SendCommsErrorMessage(string shortMessage, Exception ex)
{
OnData?.Invoke(new DataEventArgs (dcsAPI, null));
OnCommsError?.Invoke(new CommsErrorEventArgs(shortMessage, ex));
}
}
}
14 changes: 12 additions & 2 deletions src/client/DCSInsight/Interfaces/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ public interface IErrorListener
void ErrorMessage(ErrorEventArgs args);
}

public interface ICommsErrorListener
{
void CommsErrorMessage(CommsErrorEventArgs args);
}

public interface IConnectionListener
{
void ConnectionStatus(ConnectionEventArgs args);
}

public interface IDataListener
public interface IAPIDataListener
{
void APIDataReceived(APIDataEventArgs args);
}

public interface ICommandDataListener
{
void DataReceived(DataEventArgs args);
void CommandDataReceived(CommandDataEventArgs args);
}
}
Loading

0 comments on commit e316da1

Please sign in to comment.