2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / PeerDuplexChannel.cs
1 //
2 // PeerDuplexChannel.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.IO;
32 using System.Net;
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;
39
40 namespace System.ServiceModel.Channels
41 {
42
43         // PeerDuplexChannel can be created either from PeerChannelFactory
44         // (as IOutputChannel) or PeerChannelListener (as IInputChannel).
45         //
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.
49
50         internal class PeerDuplexChannel : DuplexChannelBase
51         {
52                 PeerTransportBindingElement binding;
53                 EndpointAddress local_address;
54                 PeerResolver resolver;
55                 PeerNode node;
56                 TcpListener listener;
57                 TcpChannelInfo info;
58                 List<PeerNodeAddress> peers = new List<PeerNodeAddress> ();
59
60                 public PeerDuplexChannel (IPeerChannelManager factory, EndpointAddress address, Uri via, PeerResolver resolver)
61                         : base ((ChannelFactoryBase) factory, address, via)
62                 {
63                         binding = factory.Source;
64                         this.resolver = factory.Resolver;
65                         info = new TcpChannelInfo (binding, factory.MessageEncoder, null); // FIXME: fill properties correctly.
66
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);
70                 }
71
72                 // FIXME: receive local_address too
73                 public PeerDuplexChannel (IPeerChannelManager listener)
74                         : base ((ChannelListenerBase) listener)
75                 {
76                         binding = listener.Source;
77                         this.resolver = listener.Resolver;
78                         info = new TcpChannelInfo (binding, listener.MessageEncoder, null); // FIXME: fill properties correctly.
79
80                         node = new PeerNodeImpl (null, listener.Source.ListenIPAddress, listener.Source.Port);
81                 }
82
83                 public override EndpointAddress LocalAddress {
84                         get { return local_address; }
85                 }
86
87                 public override T GetProperty<T> ()
88                 {
89                         if (typeof (T).IsInstanceOfType (node))
90                                 return (T) (object) node;
91                         return base.GetProperty<T> ();
92                 }
93
94                 // DuplexChannelBase
95
96                 TcpDuplexSessionChannel CreateInnerChannel (PeerNodeAddress pna)
97                 {
98                         var cfb = Manager as ChannelFactoryBase;
99                         if (cfb != null)
100                                 return new TcpDuplexSessionChannel (cfb, info, pna.EndpointAddress, Via);
101                         else
102                                 return new TcpDuplexSessionChannel ((ChannelListenerBase) Manager, info, listener.AcceptTcpClient ());
103                 }
104
105                 public override void Send (Message message, TimeSpan timeout)
106                 {
107                         ThrowIfDisposedOrNotOpen ();
108
109                         DateTime start = DateTime.Now;
110
111                         foreach (var pna in peers) {
112                                 var inner = CreateInnerChannel (pna);
113                                 inner.Open (timeout - (DateTime.Now - start));
114                                 inner.Send (message, timeout);
115                         }
116                 }
117
118                 public override Message Receive (TimeSpan timeout)
119                 {
120                         ThrowIfDisposedOrNotOpen ();
121
122                         throw new NotImplementedException ();
123                 }
124
125                 public override bool WaitForMessage (TimeSpan timeout)
126                 {
127                         ThrowIfDisposedOrNotOpen ();
128
129                         throw new NotImplementedException ();
130                 }
131                 
132                 // CommunicationObject
133                 
134                 protected override void OnAbort ()
135                 {
136                         OnClose (TimeSpan.Zero);
137                 }
138
139                 protected override void OnClose (TimeSpan timeout)
140                 {
141                         DateTime start = DateTime.Now;
142                         peers.Clear ();
143                         resolver.Unregister (node.RegisteredId, timeout - (DateTime.Now - start));
144                         node.SetOffline ();
145                         if (listener != null)
146                                 listener.Stop ();
147                         node.RegisteredId = null;
148                 }
149
150                 protected override void OnOpen (TimeSpan timeout)
151                 {
152                         DateTime start = DateTime.Now;
153
154                         // FIXME: supply maxAddresses
155                         peers.AddRange (resolver.Resolve (node.MeshId, 3, timeout));
156
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));
164                         node.NodeId = nid;
165
166                         node.SetOnline ();
167                 }
168         }
169 }