Skip to content

Commit

Permalink
Merge pull request #5 from jacqueskang/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jacqueskang authored Feb 14, 2018
2 parents 8543c81 + e297f0c commit 57d8731
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/IpcServiceSample.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ private static async Task MainAsync(string[] args)
float result1 = await client.InvokeAsync(x => x.AddFloat(1.23f, 4.56f));
Console.WriteLine($"sum of 2 floating number is: {result1}");

// test 1: call IPC service method with complex types
// test 2: call IPC service method with complex types
ComplexNumber result2 = await client.InvokeAsync(x => x.AddComplexNumber(
new ComplexNumber(0.1f, 0.3f),
new ComplexNumber(0.2f, 0.6f)));
Console.WriteLine($"sum of 2 complexe number is: {result2.A}+{result2.B}i");

// test 3: call IPC service method without parameter or return
// test 3: call IPC service method with an array of complex types
ComplexNumber result3 = await client.InvokeAsync(x => x.AddComplexNumbers(new[]
{
new ComplexNumber(0.5f, 0.4f),
new ComplexNumber(0.2f, 0.1f),
new ComplexNumber(0.3f, 0.5f),
}));
Console.WriteLine($"sum of 3 complexe number is: {result3.A}+{result3.B}i");

// test 4: call IPC service method without parameter or return
await client.InvokeAsync(x => x.DoNothing());
Console.WriteLine($"invoked DoNothing()");
}
Expand Down
14 changes: 13 additions & 1 deletion src/IpcServiceSample.ConsoleServer/ComputingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using IpcServiceSample.ServiceContracts;
using System.Collections.Generic;
using IpcServiceSample.ServiceContracts;
using Microsoft.Extensions.Logging;

namespace IpcServiceSample.ConsoleServer
Expand All @@ -18,6 +19,17 @@ public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
return new ComplexNumber(x.A + y.A, x.B + y.B);
}

public ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers)
{
_logger.LogInformation($"{nameof(AddComplexNumbers)} called.");
var result = new ComplexNumber(0, 0);
foreach (var number in numbers)
{
result = new ComplexNumber(result.A + number.A, result.B + number.B);
}
return result;
}

public float AddFloat(float x, float y)
{
_logger.LogInformation($"{nameof(AddFloat)} called.");
Expand Down
5 changes: 4 additions & 1 deletion src/IpcServiceSample.ServiceContracts/IComputingService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace IpcServiceSample.ServiceContracts
using System.Collections.Generic;

namespace IpcServiceSample.ServiceContracts
{
public interface IComputingService
{
float AddFloat(float x, float y);
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);
void DoNothing();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
<PackageReleaseNotes>1.0.1
- support passing array parameters</PackageReleaseNotes>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace JKang.IpcServiceFramework.Core.Tests
{
Expand Down Expand Up @@ -70,6 +71,66 @@ public void TryConvert_JObjectToComplexType()
Assert.AreEqual(expected.StringValue, ((ComplexType)actual).StringValue);
}

[TestMethod]
public void TryConvert_Int32Array()
{
int[] expected = new[] { 1, 2 };
object jObj = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(expected));

bool succeed = _sut.TryConvert(jObj, typeof(int[]), out object actual);

Assert.IsTrue(succeed);
Assert.IsInstanceOfType(actual, typeof(int[]));
var actualArray = actual as int[];
Assert.AreEqual(expected.Length, actualArray.Length);
for (int i = 0; i < expected.Length; i++)
{
Assert.AreEqual(expected[i], actualArray[i]);
}
}

[TestMethod]
public void TryConvert_Int32List()
{
var expected = new List<int> { 1, 2 };
object jObj = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(expected));

bool succeed = _sut.TryConvert(jObj, typeof(List<int>), out object actual);

Assert.IsTrue(succeed);
Assert.IsInstanceOfType(actual, typeof(List<int>));
var actualList = actual as List<int>;
Assert.AreEqual(expected.Count, actualList.Count);
for (int i = 0; i < expected.Count; i++)
{
Assert.AreEqual(expected[i], actualList[i]);
}
}

[TestMethod]
public void TryConvert_ComplexTypeArray()
{
ComplexType[] expected = new[]
{
new ComplexType { Int32Value = 123, StringValue = "abc" },
new ComplexType { Int32Value = 456, StringValue = "edf" },
};
object jObj = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(expected));

bool succeed = _sut.TryConvert(jObj, typeof(ComplexType[]), out object actual);

Assert.IsTrue(succeed);
Assert.IsInstanceOfType(actual, typeof(ComplexType[]));
var actualArray = actual as ComplexType[];
Assert.AreEqual(expected.Length, actualArray.Length);
for (int i = 0; i < expected.Length; i++)
{
Assert.IsNotNull(actualArray[i]);
Assert.AreEqual(expected[i].Int32Value, actualArray[i].Int32Value);
Assert.AreEqual(expected[i].StringValue, actualArray[i].StringValue);
}
}

class ComplexType
{
public int Int32Value { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
<PackageReleaseNotes>1.0.1
- support passing array parameters</PackageReleaseNotes>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public bool TryConvert(object origValue, Type destType, out object destValue)
return true;
}

if (origValue is JArray jArray)
{
destValue = jArray.ToObject(destType);
return true;
}

try
{
destValue = Convert.ChangeType(origValue, destType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ This package is for server hosting.</Description>
<PackageProjectUrl>https://github.com/jacqueskang/IpcServiceFramework</PackageProjectUrl>
<RepositoryUrl>https://github.com/jacqueskang/IpcServiceFramework</RepositoryUrl>
<PackageTags>dotnetcore,named-pipes,interprocess-communication</PackageTags>
<Version>1.0.1</Version>
<PackageReleaseNotes>1.0.1
- support passing array parameters</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 57d8731

Please sign in to comment.