2 // PeerDuplexChannel.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
33 using System.Net.Security;
34 using System.Net.Sockets;
35 using System.ServiceModel;
36 using System.ServiceModel.Description;
37 using System.ServiceModel.Security;
38 using System.Threading;
40 namespace System.ServiceModel.Channels
43 // PeerDuplexChannel can be created either from PeerChannelFactory
44 // (as IOutputChannel) or PeerChannelListener (as IInputChannel).
46 // PeerNode has to be created before Open() (at least at client side).
47 // On open, it tries to resolve the nodes in the mesh (and do something
48 // - but what?). Then registers itself to the mesh and refreshes it.
50 internal class PeerDuplexChannel : DuplexChannelBase
52 PeerTransportBindingElement binding;
53 EndpointAddress local_address;
54 PeerResolver resolver;
58 List<PeerNodeAddress> peers = new List<PeerNodeAddress> ();
60 public PeerDuplexChannel (IPeerChannelManager factory, EndpointAddress address, Uri via, PeerResolver resolver)
61 : base ((ChannelFactoryBase) factory, address, via)
63 binding = factory.Source;
64 this.resolver = factory.Resolver;
65 info = new TcpChannelInfo (binding, factory.MessageEncoder, null); // FIXME: fill properties correctly.
67 // It could be opened even with empty list of PeerNodeAddresses.
68 // So, do not create PeerNode per PeerNodeAddress, but do it with PeerNodeAddress[].
69 node = new PeerNodeImpl (RemoteAddress, factory.Source.ListenIPAddress, factory.Source.Port);
72 // FIXME: receive local_address too
73 public PeerDuplexChannel (IPeerChannelManager listener)
74 : base ((ChannelListenerBase) listener)
76 binding = listener.Source;
77 this.resolver = listener.Resolver;
78 info = new TcpChannelInfo (binding, listener.MessageEncoder, null); // FIXME: fill properties correctly.
80 node = new PeerNodeImpl (null, listener.Source.ListenIPAddress, listener.Source.Port);
83 public override EndpointAddress LocalAddress {
84 get { return local_address; }
87 public override T GetProperty<T> ()
89 if (typeof (T).IsInstanceOfType (node))
90 return (T) (object) node;
91 return base.GetProperty<T> ();
96 TcpDuplexSessionChannel CreateInnerChannel (PeerNodeAddress pna)
98 var cfb = Manager as ChannelFactoryBase;
100 return new TcpDuplexSessionChannel (cfb, info, pna.EndpointAddress, Via);
102 return new TcpDuplexSessionChannel ((ChannelListenerBase) Manager, info, listener.AcceptTcpClient ());
105 public override void Send (Message message, TimeSpan timeout)
107 ThrowIfDisposedOrNotOpen ();
109 DateTime start = DateTime.Now;
111 foreach (var pna in peers) {
112 var inner = CreateInnerChannel (pna);
113 inner.Open (timeout - (DateTime.Now - start));
114 inner.Send (message, timeout);
118 public override Message Receive (TimeSpan timeout)
120 ThrowIfDisposedOrNotOpen ();
122 throw new NotImplementedException ();
125 public override bool WaitForMessage (TimeSpan timeout)
127 ThrowIfDisposedOrNotOpen ();
129 throw new NotImplementedException ();
132 // CommunicationObject
134 protected override void OnAbort ()
136 OnClose (TimeSpan.Zero);
139 protected override void OnClose (TimeSpan timeout)
141 DateTime start = DateTime.Now;
143 resolver.Unregister (node.RegisteredId, timeout - (DateTime.Now - start));
145 if (listener != null)
147 node.RegisteredId = null;
150 protected override void OnOpen (TimeSpan timeout)
152 DateTime start = DateTime.Now;
154 // FIXME: supply maxAddresses
155 peers.AddRange (resolver.Resolve (node.MeshId, 3, timeout));
157 listener = node.GetTcpListener ();
158 var ep = (IPEndPoint) listener.LocalEndpoint;
159 string name = Dns.GetHostName ();
160 var nid = new Random ().Next (0, int.MaxValue);
161 var ea = new EndpointAddress ("net.tcp://" + name + ":" + ep.Port + "/PeerChannelEndpoints/" + Guid.NewGuid ());
162 var pna = new PeerNodeAddress (ea, new ReadOnlyCollection<IPAddress> (Dns.GetHostEntry (ep.Address).AddressList));
163 node.RegisteredId = resolver.Register (node.MeshId, pna, timeout - (DateTime.Now - start));