Skip to content

Commit 374a3e9

Browse files
committed
dotnet fixes for per-proxy hmac in examples
1 parent 39b6505 commit 374a3e9

File tree

11 files changed

+783
-747
lines changed

11 files changed

+783
-747
lines changed
Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
1-
/* part of Pyrolite, by Irmen de Jong ([email protected]) */
2-
3-
using System;
4-
using System.Collections;
5-
using System.Collections.Generic;
6-
using System.IO;
7-
using System.Text;
8-
9-
using Razorvine.Pickle;
10-
11-
namespace Pyrolite.PickleExample
12-
{
13-
class PickleTest
14-
{
15-
public static void Main(string[] args)
16-
{
17-
// going to pickle a c# datastructure
18-
19-
var map = new Dictionary<string, object>();
20-
map["apple"] = 42;
21-
map["microsoft"] = "hello";
22-
var values = new List<double>();
23-
values.AddRange(new double[] { 1.11, 2.22, 3.33, 4.44, 5.55} );
24-
map["values"] = values;
25-
// You can add many other types if you like. See the readme about the type mappings.
26-
27-
const string PickleFilename = "testpickle.dat";
28-
29-
Console.WriteLine("Writing pickle to '{0}'", PickleFilename);
30-
31-
var pickler = new Pickler(true);
32-
using(FileStream fos = new FileStream(PickleFilename, FileMode.Create))
33-
{
34-
pickler.dump(map, fos);
35-
}
36-
37-
Console.WriteLine("Done. Try unpickling it in python.\n");
38-
39-
Console.WriteLine("Reading a pickle created in python...");
40-
41-
// the following pickle was created in Python 3.4.
42-
// it is this data: [1, 2, 3, (11, 12, 13), {'banana', 'grape', 'apple'}]
43-
byte[] pythonpickle = new byte[] {128, 4, 149, 48, 0, 0, 0, 0, 0, 0, 0, 93, 148, 40, 75, 1, 75, 2, 75, 3, 75, 11, 75, 12, 75, 13, 135, 148, 143, 148, 40, 140, 6, 98, 97, 110, 97, 110, 97, 148, 140, 5, 103, 114, 97, 112, 101, 148, 140, 5, 97, 112, 112, 108, 101, 148, 144, 101, 46};
44-
var unpickler = new Unpickler();
45-
object result = unpickler.loads(pythonpickle);
46-
47-
Console.WriteLine("type: {0}", result.GetType());
48-
var list = (ArrayList) result;
49-
int integer1 = (int)list[0];
50-
int integer2 = (int)list[1];
51-
int integer3 = (int)list[2];
52-
object[] tuple = (object[]) list[3];
53-
HashSet<object> set = (HashSet<object>) list[4];
54-
Console.WriteLine("1-3: integers: {0}, {1}, {2}", integer1, integer2, integer3);
55-
Console.WriteLine("4: tuple: ({0}, {1}, {2})", tuple[0], tuple[1], tuple[2]);
56-
Console.WriteLine("5: set: {0}", string.Join(",", set));
57-
}
58-
}
1+
/* part of Pyrolite, by Irmen de Jong ([email protected]) */
2+
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Text;
8+
9+
using Razorvine.Pickle;
10+
11+
namespace Pyrolite.PickleExample
12+
{
13+
class PickleTest
14+
{
15+
public static void Main(string[] args)
16+
{
17+
// going to pickle a c# datastructure
18+
19+
var map = new Dictionary<string, object>();
20+
map["apple"] = 42;
21+
map["microsoft"] = "hello";
22+
var values = new List<double>();
23+
values.AddRange(new double[] { 1.11, 2.22, 3.33, 4.44, 5.55} );
24+
map["values"] = values;
25+
// You can add many other types if you like. See the readme about the type mappings.
26+
27+
const string PickleFilename = "testpickle.dat";
28+
29+
Console.WriteLine("Writing pickle to '{0}'", PickleFilename);
30+
31+
var pickler = new Pickler(true);
32+
using(FileStream fos = new FileStream(PickleFilename, FileMode.Create))
33+
{
34+
pickler.dump(map, fos);
35+
}
36+
37+
Console.WriteLine("Done. Try unpickling it in python.\n");
38+
39+
Console.WriteLine("Reading a pickle created in python...");
40+
41+
// the following pickle was created in Python 3.4.
42+
// it is this data: [1, 2, 3, (11, 12, 13), {'banana', 'grape', 'apple'}]
43+
byte[] pythonpickle = new byte[] {128, 4, 149, 48, 0, 0, 0, 0, 0, 0, 0, 93, 148, 40, 75, 1, 75, 2, 75, 3, 75, 11, 75, 12, 75, 13, 135, 148, 143, 148, 40, 140, 6, 98, 97, 110, 97, 110, 97, 148, 140, 5, 103, 114, 97, 112, 101, 148, 140, 5, 97, 112, 112, 108, 101, 148, 144, 101, 46};
44+
var unpickler = new Unpickler();
45+
object result = unpickler.loads(pythonpickle);
46+
47+
Console.WriteLine("type: {0}", result.GetType());
48+
var list = (ArrayList) result;
49+
int integer1 = (int)list[0];
50+
int integer2 = (int)list[1];
51+
int integer3 = (int)list[2];
52+
object[] tuple = (object[]) list[3];
53+
HashSet<object> set = (HashSet<object>) list[4];
54+
Console.WriteLine("1-3: integers: {0}, {1}, {2}", integer1, integer2, integer3);
55+
Console.WriteLine("4: tuple: ({0}, {1}, {2})", tuple[0], tuple[1], tuple[2]);
56+
Console.WriteLine("5: set: {0}", string.Join(",", set));
57+
58+
Console.WriteLine("\r\nEnter to exit:"); Console.ReadLine();
59+
}
60+
}
5961
}
Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
3-
<PropertyGroup>
4-
<ProjectGuid>{840FD314-D643-4CD1-9A18-E1DDDD3B2338}</ProjectGuid>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<OutputType>Exe</OutputType>
8-
<RootNamespace>Pyrolite.PickleExample</RootNamespace>
9-
<AssemblyName>Pyrolite.PickleExample</AssemblyName>
10-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
11-
<AppDesignerFolder>Properties</AppDesignerFolder>
12-
</PropertyGroup>
13-
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
14-
<PlatformTarget>x86</PlatformTarget>
15-
</PropertyGroup>
16-
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
17-
<OutputPath>bin\Debug\</OutputPath>
18-
<DebugSymbols>True</DebugSymbols>
19-
<DebugType>Full</DebugType>
20-
<Optimize>False</Optimize>
21-
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
22-
<DefineConstants>DEBUG;TRACE</DefineConstants>
23-
</PropertyGroup>
24-
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
25-
<OutputPath>bin\Release\</OutputPath>
26-
<DebugSymbols>False</DebugSymbols>
27-
<DebugType>None</DebugType>
28-
<Optimize>True</Optimize>
29-
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
30-
<DefineConstants>TRACE</DefineConstants>
31-
</PropertyGroup>
32-
<ItemGroup>
33-
<Reference Include="System" />
34-
<Reference Include="System.Core">
35-
<RequiredTargetFramework>3.5</RequiredTargetFramework>
36-
</Reference>
37-
</ItemGroup>
38-
<ItemGroup>
39-
<Compile Include="PickleTest.cs" />
40-
<Compile Include="Properties\AssemblyInfo.cs" />
41-
</ItemGroup>
42-
<ItemGroup>
43-
<None Include="app.config" />
44-
</ItemGroup>
45-
<ItemGroup>
46-
<ProjectReference Include="..\Pyrolite\Pyrolite.csproj">
47-
<Project>{E6EAC69D-D42A-4A86-AFBE-18A1013BFDAB}</Project>
48-
<Name>Pyrolite</Name>
49-
</ProjectReference>
50-
</ItemGroup>
51-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
3+
<PropertyGroup>
4+
<ProjectGuid>{840FD314-D643-4CD1-9A18-E1DDDD3B2338}</ProjectGuid>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<OutputType>Exe</OutputType>
8+
<RootNamespace>Pyrolite.PickleExample</RootNamespace>
9+
<AssemblyName>Pyrolite.PickleExample</AssemblyName>
10+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
11+
<AppDesignerFolder>Properties</AppDesignerFolder>
12+
</PropertyGroup>
13+
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
14+
<PlatformTarget>x86</PlatformTarget>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
17+
<OutputPath>bin\Debug\</OutputPath>
18+
<DebugSymbols>True</DebugSymbols>
19+
<DebugType>Full</DebugType>
20+
<Optimize>False</Optimize>
21+
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
25+
<OutputPath>bin\Release\</OutputPath>
26+
<DebugSymbols>False</DebugSymbols>
27+
<DebugType>None</DebugType>
28+
<Optimize>True</Optimize>
29+
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
30+
<DefineConstants>TRACE</DefineConstants>
31+
</PropertyGroup>
32+
<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
33+
<PlatformTarget>x86</PlatformTarget>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core">
38+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
39+
</Reference>
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="PickleTest.cs" />
43+
<Compile Include="Properties\AssemblyInfo.cs" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<None Include="app.config" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<ProjectReference Include="..\Pyrolite\Pyrolite.csproj">
50+
<Project>{E6EAC69D-D42A-4A86-AFBE-18A1013BFDAB}</Project>
51+
<Name>Pyrolite</Name>
52+
</ProjectReference>
53+
</ItemGroup>
54+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5255
</Project>
Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,80 @@
1-
/* part of Pyrolite, by Irmen de Jong ([email protected]) */
2-
3-
using System;
4-
using System.Text;
5-
using Razorvine.Pyro;
6-
7-
namespace Pyrolite.TestPyroFlame
8-
{
9-
10-
/// <summary>
11-
/// Test Pyro with a Flame server
12-
/// </summary>
13-
public class TestFlame {
14-
15-
public static void Main(String[] args) {
16-
try {
17-
Test();
18-
} catch (Exception x) {
19-
Console.WriteLine("unhandled exception: {0}",x);
20-
}
21-
}
22-
23-
public static void Test() {
24-
25-
Console.WriteLine("Testing Pyro flame server (make sure it's running on localhost 9999)...");
26-
Console.WriteLine("Pyrolite version: "+Config.PYROLITE_VERSION);
27-
28-
setConfig();
29-
using(dynamic flame=new PyroProxy("localhost",9999,"Pyro.Flame"))
30-
{
31-
Console.WriteLine("builtin:");
32-
using(dynamic r_max=(FlameBuiltin)flame.builtin("max"))
33-
{
34-
int maximum=(int)r_max(new int[]{22,99,1}); // invoke remote max() builtin function
35-
Console.WriteLine("maximum="+maximum);
36-
}
37-
38-
using(dynamic r_module=(FlameModule)flame.module("socket"))
39-
{
40-
String hostname=(String)r_module.gethostname(); // get remote hostname
41-
Console.WriteLine("hostname="+hostname);
42-
}
43-
44-
int sum=(int)flame.evaluate("9+9");
45-
Console.WriteLine("sum="+sum);
46-
47-
flame.execute("import sys; sys.stdout.write('HELLO FROM C#\\n')");
48-
49-
using(FlameRemoteConsole console=(FlameRemoteConsole)flame.console())
50-
{
51-
console.interact();
52-
}
53-
}
54-
}
55-
56-
static void setConfig()
57-
{
58-
Config.SERIALIZER = Config.SerializerType.pickle; // flame requires pickle
59-
60-
string tracedir=Environment.GetEnvironmentVariable("PYRO_TRACE_DIR");
61-
if(tracedir!=null) {
62-
Config.MSG_TRACE_DIR=tracedir;
63-
}
64-
}
65-
}
66-
1+
/* part of Pyrolite, by Irmen de Jong ([email protected]) */
2+
3+
using System;
4+
using System.Text;
5+
using Razorvine.Pyro;
6+
7+
namespace Pyrolite.TestPyroFlame
8+
{
9+
10+
/// <summary>
11+
/// Test Pyro with a Flame server
12+
/// </summary>
13+
public class TestFlame {
14+
15+
static protected byte[] hmacKey; // just ignore this if you don't specify a PYRO_HMAC_KEY environment var
16+
17+
public static void Main(String[] args) {
18+
try {
19+
Test();
20+
} catch (Exception x) {
21+
Console.WriteLine("unhandled exception: {0}",x);
22+
}
23+
}
24+
25+
public static void Test() {
26+
27+
Console.WriteLine("Testing Pyro flame server (make sure it's running on localhost 9999)...");
28+
Console.WriteLine("Pyrolite version: "+Config.PYROLITE_VERSION);
29+
30+
setConfig();
31+
using(dynamic flame=new PyroProxy("localhost",9999,"Pyro.Flame"))
32+
{
33+
if(hmacKey!=null) flame.pyroHmacKey = hmacKey;
34+
35+
Console.WriteLine("builtin:");
36+
using(dynamic r_max=(FlameBuiltin)flame.builtin("max"))
37+
{
38+
if(hmacKey!=null) r_max.pyroHmacKey = hmacKey;
39+
40+
int maximum=(int)r_max(new int[]{22,99,1}); // invoke remote max() builtin function
41+
Console.WriteLine("maximum="+maximum);
42+
}
43+
44+
using(dynamic r_module=(FlameModule)flame.module("socket"))
45+
{
46+
if(hmacKey!=null) r_module.pyroHmacKey = hmacKey;
47+
48+
String hostname=(String)r_module.gethostname(); // get remote hostname
49+
Console.WriteLine("hostname="+hostname);
50+
}
51+
52+
int sum=(int)flame.evaluate("9+9");
53+
Console.WriteLine("sum="+sum);
54+
55+
flame.execute("import sys; sys.stdout.write('HELLO FROM C#\\n')");
56+
57+
using(FlameRemoteConsole console=(FlameRemoteConsole)flame.console())
58+
{
59+
console.interact();
60+
}
61+
62+
Console.WriteLine("\r\nEnter to exit:"); Console.ReadLine();
63+
}
64+
}
65+
66+
static void setConfig()
67+
{
68+
string hmackeyEnv=Environment.GetEnvironmentVariable("PYRO_HMAC_KEY");
69+
if(hmackeyEnv!=null) {
70+
hmacKey=Encoding.UTF8.GetBytes(hmackeyEnv);
71+
}
72+
string tracedir=Environment.GetEnvironmentVariable("PYRO_TRACE_DIR");
73+
if(tracedir!=null) {
74+
Config.MSG_TRACE_DIR=tracedir;
75+
}
76+
Config.SERIALIZER = Config.SerializerType.pickle; // flame requires the pickle serializer
77+
}
78+
}
79+
6780
}

0 commit comments

Comments
 (0)