0003-Add-factory-singleton-for-participitants_531bea6.patch
rsb-cil-test/Program.cs | ||
---|---|---|
12 | 12 |
{ |
13 | 13 |
class MainClass |
14 | 14 |
{ |
15 |
static readonly byte[] host = new byte[] { 127, 0, 0, 1};
|
|
15 |
static readonly byte[] ip = new byte[] { 127, 0, 0, 1};
|
|
16 | 16 |
static readonly int port = 30010; |
17 | 17 | |
18 | 18 |
public static void PrintEvent(Event theEvent) |
... | ... | |
48 | 48 |
new Tuple<Type, IConverter>(typeof(JointAngles), new ProtocolBufferConverter(JointAngles.Descriptor)) |
49 | 49 |
}); |
50 | 50 | |
51 |
var listener = new Listener(new InPushConnector(new BusClientConnection(host, port), new Scope("/"), inConverters));
|
|
51 |
var listener = new Listener(new InPushConnector(new BusClientConnection(ip, port), new Scope("/"), inConverters));
|
|
52 | 52 |
listener.EventReceived += new NewEventHandler(PrintEvent); |
53 | 53 |
listener.Activate(); |
54 | 54 | |
55 |
var informer = new Informer("/test/narf", new OutConnector(new BusClientConnection(host, port),
|
|
55 |
var informer = new Informer("/test/narf", new OutConnector(new BusClientConnection(ip, port),
|
|
56 | 56 |
outConverters)); |
57 | 57 |
Console.WriteLine(informer.Scope); |
58 | 58 |
informer.Activate(); |
... | ... | |
74 | 74 | |
75 | 75 |
listener.Deactivate(); |
76 | 76 | |
77 |
Console.WriteLine("\nConverter test:");
|
|
77 |
Console.WriteLine("\nRepository test with factory:");
|
|
78 | 78 | |
79 |
DefaultConverterRepository.Instance.addConverter(new ProtocolBufferConverter(JointAngles.Descriptor)); |
|
79 |
Factory.Instance.Hostname = "localhost"; |
|
80 |
Factory.Instance.Port = port; |
|
80 | 81 | |
81 |
Informer repoInformer = new Informer("/test/repo", new OutConnector(new BusClientConnection(host, port), DefaultConverterRepository.Instance.SerializationConverterSelector)); |
|
82 |
Listener repoListener = new Listener(new InPushConnector(new BusClientConnection(host, port), new Scope("/"), DefaultConverterRepository.Instance.DeserializationConverterSelector)); |
|
82 |
DefaultConverterRepository.Instance.addConverter(new ProtocolBufferConverter(JointAngles.Descriptor)); |
|
83 | 83 | |
84 |
repoListener.EventReceived += (e) => |
|
85 |
{ |
|
84 |
Informer repoInformer = Factory.Instance.createInformer("/test/repo"); |
|
85 |
Listener repoListener = Factory.Instance.createListener("/"); |
|
86 |
|
|
87 |
repoListener.EventReceived += (e) => { |
|
86 | 88 |
Console.WriteLine("Event: {0}\nData: {1}", e, e.Data); |
87 | 89 |
}; |
88 | 90 |
repoListener.Activate(); |
rsb-cil/Rsb/Factory.cs | ||
---|---|---|
1 |
|
|
2 | ||
3 |
using Rsb.Transport.Socket; |
|
4 |
using Rsb.Converter; |
|
5 | ||
6 |
using System; |
|
7 |
using System.Net; |
|
8 |
using System.Linq; |
|
9 |
using System.Net.Sockets; |
|
10 |
using Rsb.Patterns; |
|
11 | ||
12 |
namespace Rsb |
|
13 |
{ |
|
14 |
public class Factory |
|
15 |
{ |
|
16 |
private static readonly Factory instance = new Factory(); |
|
17 | ||
18 |
private byte[] ip = { 127, 0, 0, 1 }; |
|
19 |
private string hostname = "localhost"; |
|
20 |
private int port = 30010; |
|
21 | ||
22 |
private IPEndPoint endPoint; |
|
23 | ||
24 |
public string Hostname { |
|
25 |
get { return hostname; } |
|
26 |
set { |
|
27 |
endPoint = null; |
|
28 |
hostname = value; |
|
29 |
// query all ipv4 addresses of the ip |
|
30 |
var addresses = Dns.GetHostAddresses(value); |
|
31 |
var ipv4addresses = (from ipv4 in addresses where ipv4.AddressFamily == AddressFamily.InterNetwork select ipv4); |
|
32 |
// take first ipv4 address |
|
33 |
if (ipv4addresses.Count() != 0) |
|
34 |
{ |
|
35 |
ip = ipv4addresses.First().GetAddressBytes(); |
|
36 |
} |
|
37 |
// otherwise take first address at all... |
|
38 |
else { |
|
39 |
ip = addresses[0].GetAddressBytes(); |
|
40 |
} |
|
41 |
} |
|
42 |
} |
|
43 | ||
44 |
public byte[] Ip { |
|
45 |
get { return ip; } |
|
46 |
set |
|
47 |
{ |
|
48 |
endPoint = null; |
|
49 |
ip = value; |
|
50 |
hostname = "unknown"; |
|
51 |
} |
|
52 |
} |
|
53 | ||
54 |
public int Port { |
|
55 |
get { return port; } |
|
56 |
set { |
|
57 |
endPoint = null; |
|
58 |
port = value; |
|
59 |
} |
|
60 |
} |
|
61 | ||
62 |
// Explicit static constructor to tell C# compiler |
|
63 |
// not to mark type as beforefieldinit |
|
64 |
static Factory() |
|
65 |
{ |
|
66 |
} |
|
67 | ||
68 |
private Factory() |
|
69 |
{ |
|
70 |
} |
|
71 | ||
72 |
public static Factory Instance |
|
73 |
{ |
|
74 |
get |
|
75 |
{ |
|
76 |
return instance; |
|
77 |
} |
|
78 |
} |
|
79 | ||
80 |
private void CheckEndPoint() { |
|
81 |
if (endPoint == null) |
|
82 |
{ |
|
83 |
endPoint = new IPEndPoint(new IPAddress(ip), port); |
|
84 |
} |
|
85 |
} |
|
86 | ||
87 |
public Informer CreateInformer(Scope scope) { |
|
88 |
CheckEndPoint(); |
|
89 | ||
90 |
return new Informer(scope, |
|
91 |
new OutConnector( |
|
92 |
new BusClientConnection(endPoint), |
|
93 |
DefaultConverterRepository.Instance.SerializationConverterSelector)); |
|
94 |
} |
|
95 | ||
96 |
public Informer CreateInformer(string scope) { |
|
97 |
return CreateInformer(new Scope(scope)); |
|
98 |
} |
|
99 | ||
100 |
public Listener CreateListener(Scope scope) |
|
101 |
{ |
|
102 |
CheckEndPoint(); |
|
103 | ||
104 |
return new Listener( |
|
105 |
new InPushConnector( |
|
106 |
new BusClientConnection(endPoint), |
|
107 |
scope, |
|
108 |
DefaultConverterRepository.Instance.DeserializationConverterSelector |
|
109 |
)); |
|
110 |
} |
|
111 | ||
112 |
public Listener CreateListener(string scope) { |
|
113 |
return CreateListener(new Scope(scope)); |
|
114 |
} |
|
115 | ||
116 |
} |
|
117 |
} |
rsb-cil/rsb-cil.csproj | ||
---|---|---|
51 | 51 |
<Compile Include="Rsb\Converter\StringConverter.cs" /> |
52 | 52 |
<Compile Include="Rsb\Converter\UInt32Converter.cs" /> |
53 | 53 |
<Compile Include="Rsb\Converter\UInt64Converter.cs" /> |
54 |
<Compile Include="Rsb\Factory.cs" /> |
|
54 | 55 |
<Compile Include="Rsb\Protocol\EventId.cs" /> |
55 | 56 |
<Compile Include="Rsb\Protocol\EventMetaData.cs" /> |
56 | 57 |
<Compile Include="Rsb\Protocol\Notification.cs" /> |
57 |
- |