Skip to content

Commit

Permalink
C# libraries separated by MDM
Browse files Browse the repository at this point in the history
  • Loading branch information
derekkingston committed Oct 27, 2017
1 parent fc46ce3 commit b703c7a
Show file tree
Hide file tree
Showing 20 changed files with 721 additions and 171 deletions.
230 changes: 213 additions & 17 deletions src/avtas/lmcp/lmcpgen/CsMethods.java

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/avtas/lmcp/lmcpgen/MDMInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MDMInfo {
public String seriesName = "";
public String namespace = "";
public String comment = "";
public String guid = "";
public StructInfo[] structs = new StructInfo[0];
public EnumInfo[] enums = new EnumInfo[0];
/** a string containing the contents of the MDM file */
Expand Down
2 changes: 2 additions & 0 deletions src/avtas/lmcp/lmcpgen/MDMReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.UUID;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -95,6 +96,7 @@ public static MDMInfo getFromXML(Node node) throws Exception {
MDMInfo info = new MDMInfo();

info.seriesName = XMLUtil.get(node, "SeriesName", "");
info.guid = UUID.nameUUIDFromBytes(info.seriesName.getBytes()).toString().toUpperCase(); // UUID.randomUUID().toString().toUpperCase();
info.seriesNameAsLong = seriesNameToLong(info.seriesName);
int startId = 1;
info.namespace = XMLUtil.get(node, "Namespace", "");
Expand Down
5 changes: 5 additions & 0 deletions src/templates/cs.tl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
ONCE cs/LMCP.sln ./LMCP.sln

ONCE cs/LMCP.csproj Library/LMCP.csproj
ONCE cs/LmcpCore.csproj Library/LmcpCore.csproj
ONCE cs/LibraryAssemblyInfo.cs Library/Properties/AssemblyInfo.cs
ONCE cs/ILmcpObject.cs Library/Avtas/Lmcp/ILmcpObject.cs
ONCE cs/LmcpFactory.cs Library/Avtas/Lmcp/LmcpFactory.cs
ONCE cs/LmcpCoreFactory.cs Library/Avtas/Lmcp/LmcpCoreFactory.cs
ONCE cs/LmcpBinaryReader.cs Library/Avtas/Lmcp/LmcpBinaryReader.cs
ONCE cs/LmcpBinaryWriter.cs Library/Avtas/Lmcp/LmcpBinaryWriter.cs
ONCE cs/LmcpBinaryProcessor.cs Library/Avtas/Lmcp/LmcpBinaryProcessor.cs
ONCE cs/LmcpObjectReceivedEventArgs.cs Library/Avtas/Lmcp/LmcpObjectReceivedEventArgs.cs
ONCE cs/LmcpXmlReader.cs Library/Avtas/Lmcp/LmcpXmlReader.cs
ONCE cs/LmcpCoreXmlReader.cs Library/Avtas/Lmcp/LmcpCoreXmlReader.cs
ONCE cs/LmcpXmlWriter.cs Library/Avtas/Lmcp/LmcpXmlWriter.cs
ONCE cs/ISeriesList.cs Library/Avtas/Lmcp/ISeriesList.cs

ONCE cs/TestClient.csproj TestClient/TestClient.csproj
ONCE cs/TestClient.cs TestClient/Program.cs
Expand All @@ -23,6 +27,7 @@ ONCE cs/TestServerAssemblyInfo.cs TestServer/Properties/Assemb
PER_MDM cs/SeriesList.cs Library/-<namespace>-/SeriesList.cs
PER_MDM cs/SeriesEnum.cs Library/-<namespace>-/SeriesEnum.cs
PER_MDM cs/SeriesXmlReader.cs Library/-<namespace>-/SeriesXmlReader.cs
PER_MDM cs/SeriesProj.csproj Library/Lmcp-<cs_series_name>-.csproj

PER_STRUCT cs/SeriesObject.cs Library/-<namespace>-/-<datatype_name>-.cs

Expand Down
10 changes: 10 additions & 0 deletions src/templates/cs/ISeriesList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Avtas.Lmcp;

namespace Avtas.Lmcp
{
public interface ISeriesList
{
long SeriesId { get; }
ILmcpObject GetInstance( uint object_type, ushort version );
}
}
9 changes: 5 additions & 4 deletions src/templates/cs/LMCP.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Afrl</RootNamespace>
<AssemblyName>LMCP</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down Expand Up @@ -59,8 +59,9 @@
<Compile Include="Avtas\Lmcp\LmcpObjectReceivedEventArgs.cs" />
<Compile Include="Avtas\Lmcp\LmcpXmlReader.cs" />
<Compile Include="Avtas\Lmcp\LmcpXmlWriter.cs" />
<Compile Include="Avtas\Lmcp\ISeriesList.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
-<project_sources>-
-<project_sources>-
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
18 changes: 11 additions & 7 deletions src/templates/cs/LMCP.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LMCP", "Library\LMCP.csproj", "{-<library_make_guid>-}"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LmcpCore", "Library\LmcpCore.csproj", "{-<core_library_make_guid>-}"
EndProject
-<all_series_projects>-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestClient", "TestClient\TestClient.csproj", "{-<test_client_make_guid>-}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestServer", "TestServer\TestServer.csproj", "{-<test_server_make_guid>-}"
Expand All @@ -13,10 +16,11 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{-<library_make_guid>-}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{-<library_make_guid>-}.Debug|Any CPU.Build.0 = Debug|Any CPU
{-<library_make_guid>-}.Release|Any CPU.ActiveCfg = Release|Any CPU
{-<library_make_guid>-}.Release|Any CPU.Build.0 = Release|Any CPU
{-<core_library_make_guid>-}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{-<core_library_make_guid>-}.Debug|Any CPU.Build.0 = Debug|Any CPU
{-<core_library_make_guid>-}.Release|Any CPU.ActiveCfg = Release|Any CPU
{-<core_library_make_guid>-}.Release|Any CPU.Build.0 = Release|Any CPU
-<all_series_configurations>-
{-<test_client_make_guid>-}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{-<test_client_make_guid>-}.Debug|Any CPU.Build.0 = Debug|Any CPU
{-<test_client_make_guid>-}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
73 changes: 73 additions & 0 deletions src/templates/cs/LmcpCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{-<core_library_make_guid>-}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Afrl</RootNamespace>
<AssemblyName>LcmpCore</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>bin\Debug\LmcpCore.XML</DocumentationFile>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\LmcpCore.XML</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Avtas\Lmcp\ILmcpObject.cs" />
<Compile Include="Avtas\Lmcp\LmcpBinaryProcessor.cs" />
<Compile Include="Avtas\Lmcp\LmcpBinaryReader.cs" />
<Compile Include="Avtas\Lmcp\LmcpBinaryWriter.cs" />
<Compile Include="Avtas\Lmcp\LmcpCoreFactory.cs" />
<Compile Include="Avtas\Lmcp\LmcpObjectReceivedEventArgs.cs" />
<Compile Include="Avtas\Lmcp\LmcpCoreXmlReader.cs" />
<Compile Include="Avtas\Lmcp\LmcpXmlWriter.cs" />
<Compile Include="Avtas\Lmcp\ISeriesList.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
182 changes: 182 additions & 0 deletions src/templates/cs/LmcpCoreFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// ===============================================================================
// Authors: AFRL/RQQA
// Organization: Air Force Research Laboratory, Aerospace Systems Directorate, Power and Control Division
//
// Copyright (c) 2017 Government of the United State of America, as represented by
// the Secretary of the Air Force. No copyright is claimed in the United States under
// Title 17, U.S. Code. All Other Rights Reserved.
// ===============================================================================

// This file was auto-created by LmcpGen. Modifications will be overwritten.

using System;
using System.IO;
using System.Collections.Generic;

namespace Avtas.Lmcp
{
/// <summary>
/// Factory for creating LMCP objects.
/// </summary>
public abstract class LmcpFactory
{
internal const int HEADER_SIZE = 8;
internal const int CHECKSUM_SIZE = 4;
internal const int LMCP_CONTROL_STR = 0x4c4d4350;

private static List<ISeriesList> _seriesLists = new List<ISeriesList>();

public static void RegisterSeries( ISeriesList seriesList )
{
_seriesLists.Add( seriesList );
}
/// <summary>
/// Returns an array of bytes corresponding to the first message encountered in
/// the input stream. The method blocks until all of the bytes are read.
/// </summary>
/// <returns>An array of bytes corresponding to the first message encountered in
/// the input stream.</returns>
public static byte[] GetMessageBytes(BinaryReader sr)
{
byte[] bytes = new byte[HEADER_SIZE];
int i = sr.Read(bytes, 0, HEADER_SIZE);
while(i <bytes.Length)
i += sr.Read(bytes, i, bytes.Length-i);

// retrieves the "size" value in BIG_ENDIAN order
uint size = GetSize(bytes);

byte[] buf = new byte[size + HEADER_SIZE + CHECKSUM_SIZE];
bytes.CopyTo(buf, 0);

i = bytes.Length;
while(i <buf.Length)
i += sr.Read(buf, i, buf.Length-i);

return buf;
}

/// <summary>
/// Returns an Lmcp message that is created by reading an array of bytes from some input
/// source. The message header items are read, the root object is created, and the
/// checksum is validated.
/// </summary>
/// <returns>An ILmcpObject or null if the root object type is not defined.</returns>
public static ILmcpObject GetObject(byte[] bytes)
{
if (bytes == null || bytes.Length < HEADER_SIZE)
throw new Exception("Lmcp Factory Exception: Null buffer or not enough bytes in buffer");

if (!Validate(bytes))
throw new Exception("Lmcp Factory Exception: Checksum does not match");

LmcpBinaryReader buf = new LmcpBinaryReader(bytes);

if (buf.ReadInt32() != LMCP_CONTROL_STR) {
throw new Exception("Lmcp Factory Exception: Not a valid LMCP message.");
}
if ( buf.ReadInt32() > bytes.Length - HEADER_SIZE - CHECKSUM_SIZE) {
throw new Exception("LMCP Factory Exception: not enough bytes in buffer to create object.");
}

return buf.ReadObject();
}

/// <summary>
/// Returns an byte array containing an ILmcpObject packed as a message.
/// </summary>
/// <returns>The packed message.</returns>
public static byte[] PackMessage(ILmcpObject rootObject, bool calculatechecksum)
{
if (rootObject == null) return null;

int size = rootObject.CalculateSize();
byte[] bytes = new byte[size + HEADER_SIZE + CHECKSUM_SIZE];

MemoryStream buf = new MemoryStream(bytes);
LmcpBinaryWriter bw = new LmcpBinaryWriter(buf);

bw.Write(LMCP_CONTROL_STR);
bw.Write(size);
bw.Write(rootObject);

uint cs = calculatechecksum ? CalculateChecksum(bytes) : 0;
bw.Write(cs);

return buf.ToArray();
}

internal static ILmcpObject CreateObject(long series_id, uint object_type, ushort version) {
foreach ( var series in _seriesLists )
{
if ( series_id != series.SeriesId )
continue;
return series.GetInstance( object_type, version );
}
return null;
}

/// <summary>
/// Returns the size of a message that is represented by the given byte array.
/// </summary>
public static uint GetSize(byte[] bytes)
{
uint size = 0;
size |= (bytes[4] & 0xFFu);
size <<= 8;
size |= (bytes[5] & 0xFFu);
size <<= 8;
size |= (bytes[6] & 0xFFu);
size <<= 8;
size |= (bytes[7] & 0xFFu);
return size;
}

/// <summary>
/// Returns a message read from an object.
/// </summary>
public static ILmcpObject GetObject(BinaryReader sr)
{
byte[] bytes = GetMessageBytes(sr);
return GetObject(bytes);
}


/// <summary>
/// Calculates the checksum. This should be called after pack().
/// The checksum sums all bytes in the packet between 0 and
/// buf.limit() - CHECKSUM_SIZE.
/// </summary>
public static uint CalculateChecksum(byte[] bytes)
{
uint val = 0;
for (int i = 0; i < bytes.Length - CHECKSUM_SIZE; i++)
{
val += ( (uint) bytes[i] );
}
return val;
}

/// <summary>
/// Checks the bytebuffer's checksum value against the calculated checksum
/// returns true if the calculated and stored values match, or if the buffer value is
/// zero (indicating that checksum was not calculated. This method rewinds the buffer and
/// returns it to LIMIT - 4 bytes (start position of checksum).
/// </summary>
public static bool Validate(byte[] bytes)
{
// retrieves the checksum value in BIG_ENDIAN order
uint cs = 0;
int len = bytes.Length;
cs |= (bytes[len - 4] & 0xFFu);
cs <<= 8;
cs |= (bytes[len - 3] & 0xFFu);
cs <<= 8;
cs |= (bytes[len - 2] & 0xFFu);
cs <<= 8;
cs |= (bytes[len - 1] & 0xFFu);

return (cs == 0) || (CalculateChecksum(bytes) == cs);
}
}
}
Loading

0 comments on commit b703c7a

Please sign in to comment.