Skip to content

Commit

Permalink
Daemon will now accept options in it's requests
Browse files Browse the repository at this point in the history
  • Loading branch information
hcorg committed Jan 27, 2020
1 parent df24150 commit e6fc232
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 19 deletions.
24 changes: 23 additions & 1 deletion Daemon.ExampleClient/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,28 @@ def get_compilation_error(uri):
'AcnFiles': []
}
get_ast(uri, data)


def get_ast_with_options(uri):
data = {
'AsnFiles': [
{
'Name': 'Test.asn',
'Contents': 'Example DEFINITIONS ::= BEGIN MyInt ::= INTEGER(0 .. 10) MySequence ::= SEQUENCE { a MyInt } END'
}
],
'AcnFiles': [
{
'Name': 'Test.acn',
'Contents': 'Example DEFINITIONS ::= BEGIN MyInt [size 32, encoding pos-int] END'
}
],
'Options' : {
'TypePrefix' : 'T',
'Encoding' : { 'Case' : 'BER' },
'FieldPrefix' : { 'Case' : 'FieldPrefixUserValue', 'Fields' : ['PREF_']}
}
}
get_ast(uri, data)

def run(uri):
print("asn1scc Daemon Test Client")
Expand All @@ -57,6 +78,7 @@ def run(uri):
get_correct_ast(uri)
get_compilation_error(uri)
get_correct_ast_with_acn(uri)
get_ast_with_options(uri)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion Daemon/Asn1ServiceBindings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ type Asn1ServiceBindings(service:IAsn1Service) =
member this.GetVersion (context:HttpListenerContext) = Http.SendPlainText context.Response service.Version

member this.BuildAst (context:HttpListenerContext) =
let data = Http.ReadJson<Dto.InputFiles> context.Request
let data = Http.ReadJson<Dto.Input> context.Request
Http.SendJson context.Response (service.BuildAst data)

8 changes: 6 additions & 2 deletions Daemon/Http.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ open System.Text
open Newtonsoft.Json
open System.IO

let private serializer =
let settings = new JsonSerializerSettings()
settings.Converters.Add(new Converters.StringEnumConverter())
settings.Converters.Add(new Converters.DiscriminatedUnionConverter())
JsonSerializer.Create(settings)

let ReadJson<'T> (request:HttpListenerRequest) =
let serializer = JsonSerializer()
use reader = new StreamReader(request.InputStream)
serializer.Deserialize(reader, typeof<'T>) :?> 'T

Expand All @@ -16,7 +21,6 @@ let SendJson<'T> (response:HttpListenerResponse) (obj:'T) =
response.ContentType <- "application/json"
response.ContentEncoding <- Encoding.UTF8

let serializer = JsonSerializer()
use writer = new StreamWriter(response.OutputStream)
serializer.Serialize(writer, obj)

Expand Down
38 changes: 25 additions & 13 deletions Service.Implementation/Asn1Service.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ open System.IO
type Asn1Service() =

let asn1f2AccessLock = new Object();

let defaultOptions = {
Dto.GenerationOptions.Encoding = CommonTypes.Asn1Encoding.ACN
Dto.GenerationOptions.TargetLanguage = CommonTypes.ProgrammingLanguage.C
Dto.GenerationOptions.IntegerSizeInBytes = 8
Dto.GenerationOptions.FloatingPointSizeInBytes = 8
Dto.GenerationOptions.TypePrefix = ""
Dto.GenerationOptions.FieldPrefix = CommonTypes.FieldPrefixAuto
Dto.GenerationOptions.RenamePolicy = CommonTypes.EnumRenamePolicy.SelectiveEnumerants
}

interface IAsn1Service with

member this.BuildAst(files: Dto.InputFiles): Dto.Output =
member this.BuildAst(input: Dto.Input): Dto.Output =


try
lock asn1f2AccessLock (fun() -> this.CallProgram files)
lock asn1f2AccessLock (fun() -> this.CallProgram input)
with
| :? Antlr.Asn1.asn1Parser.SyntaxErrorException as ex ->
{
Expand Down Expand Up @@ -50,24 +60,25 @@ type Asn1Service() =

member this.Version = "TODO" /// TODO, where version should be stored? FrontEnd? it can't be in Asnf4 Asn1f2.Program.GetVersionString()
member private this.BuildCommandLineSettings (input:Dto.InputFiles) outfile =
member private this.BuildCommandLineSettings asnFiles acnFiles (options:Dto.GenerationOptions) outfile =
{
CommandLineSettings.asn1Files = input.AsnFiles |> Seq.map this.ConvertInput |> Seq.toList
acnFiles = input.AcnFiles |> Seq.map this.ConvertInput |> Seq.toList
encodings = [ CommonTypes.Asn1Encoding.ACN ]// TODO does this influence generated XML?
CommandLineSettings.asn1Files = asnFiles |> Seq.map this.ConvertInput |> Seq.toList
acnFiles = acnFiles |> Seq.map this.ConvertInput |> Seq.toList
encodings = match box options.Encoding with | null -> [defaultOptions.Encoding] | _ -> [options.Encoding]
GenerateEqualFunctions = false
generateAutomaticTestCases = false
TypePrefix = ""
TypePrefix = match options.TypePrefix with | null -> defaultOptions.TypePrefix | _ -> options.TypePrefix
CheckWithOss = false
AstXmlAbsFileName = outfile
IcdUperHtmlFileName = ""
IcdAcnHtmlFileName = ""
mappingFunctionsModule = None
integerSizeInBytes = 8I
renamePolicy = CommonTypes.EnumRenamePolicy.NoRenamePolicy
integerSizeInBytes = bigint(match options.IntegerSizeInBytes with | 0 -> defaultOptions.IntegerSizeInBytes | _ -> options.IntegerSizeInBytes)
floatingPointSizeInBytes = bigint(match options.FloatingPointSizeInBytes with | 0 -> defaultOptions.FloatingPointSizeInBytes | _ -> options.FloatingPointSizeInBytes)
renamePolicy = match box options.RenamePolicy with | null -> defaultOptions.RenamePolicy | _ -> options.RenamePolicy
custom_Stg_Ast_Version = 1
fieldPrefix = None
targetLanguages = [CommonTypes.ProgrammingLanguage.C]
fieldPrefix = match box options.FieldPrefix with | null -> None | _ -> Some(options.FieldPrefix)
targetLanguages = match box options.TargetLanguage with | null -> [defaultOptions.TargetLanguage] | _ -> [options.TargetLanguage]
floatingPointSizeInBytes = 8I
objectIdentifierMaxLength = 8I
}
Expand All @@ -83,11 +94,12 @@ type Asn1Service() =
Name = file.Replace(dir.Path, "")
Contents = System.IO.File.ReadAllLines file |> String.concat "\n"
}
member private this.CallProgram (files: Dto.InputFiles) : Dto.Output =
member private this.CallProgram (input: Dto.Input) : Dto.Output =
use dir = new TemporaryDirectory()
let xmlFileName = dir.FullPath "AST.xml"

let args = this.BuildCommandLineSettings files xmlFileName
let options = match box input.Options with | null -> defaultOptions | _ -> input.Options
let args = this.BuildCommandLineSettings input.AsnFiles input.AcnFiles options xmlFileName
let frontEntAst, acnDeps = FrontEntMain.constructAst args (fun a b -> ())
ExportToXml.exportFile frontEntAst acnDeps args.AstXmlAbsFileName

Expand Down
13 changes: 12 additions & 1 deletion Service/Dto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

open System.Collections.Generic

type GenerationOptions = {
Encoding: CommonTypes.Asn1Encoding
TargetLanguage: CommonTypes.ProgrammingLanguage
IntegerSizeInBytes: int
FloatingPointSizeInBytes: int
TypePrefix: string
FieldPrefix: CommonTypes.FieldPrefix
RenamePolicy: CommonTypes.EnumRenamePolicy
}

type FileData = {
Name: string
Contents: string
}

type InputFiles = {
type Input = {
AsnFiles: IEnumerable<FileData>
AcnFiles: IEnumerable<FileData>
Options: GenerationOptions
}

type Output = {
Expand Down
2 changes: 1 addition & 1 deletion Service/IAsn1Service.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

type IAsn1Service =
abstract member Version : string
abstract member BuildAst : Dto.InputFiles -> Dto.Output
abstract member BuildAst : Dto.Input -> Dto.Output
7 changes: 7 additions & 0 deletions Service/Service.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CommonTypes\CommonTypes.fsproj">
<Name>CommonTypes</Name>
<Project>{83f15fa6-7da0-4e47-9512-39ae3fdd28cf}</Project>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<!-- 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">
Expand Down

0 comments on commit e6fc232

Please sign in to comment.