public ClientRemoteConnectorInfoManager(RemoteWSFrameworkConnectionInfo info,
            IMessageListener<WebSocketConnectionGroup, WebSocketConnectionHolder, RemoteOperationContext> listener,
            ConcurrentDictionary<string, WebSocketConnectionGroup> globalConnectionGroups)
            : base(listener, globalConnectionGroups)
        {
            _delegatingAsyncConnectorInfoManager =
                new RemoteDelegatingAsyncConnectorInfoManager(this);

            _info = info;
            _timer = new Timer(state =>
            {
                WebSocketConnectionHolder[] connections = state as WebSocketConnectionHolder[];
                if (null != connections)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        if (connections[i] == null || !connections[i].Operational)
                        {
                            WebSocketWrapper vr = WebSocketWrapper.Create(_info, listener, Handshake);
                            vr.ConnectAsync().ContinueWith((result, o) =>
                            {
                                if (result.IsCompleted)
                                {
                                    connections[(int)o] = result.Result;
                                }
                                else if (result.IsFaulted)
                                {
                                    TraceUtil.TraceException("Failed to establish connection", result.Exception);
                                }
                            }, i);
                        }
                    }
                }
            }, _connections, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(5));
        }
        public void Init()
        {
            try
            {
                ConnectorFramework serverConnectorFramework = new ConnectorFramework();
                ConnectorServerService.ConnectorServerService.InitializeConnectors(serverConnectorFramework.LocalManager);

                foreach (var connectorInfo in serverConnectorFramework.LocalManager.ConnectorInfos)
                {
                    Trace.TraceInformation("Found Connector {0}", connectorInfo.ConnectorKey);
                }

                int freePort = FreePort;
                EndpointAddress serviceAddress =
                    new EndpointAddress(String.Format("http://localhost:{0}/openicf", freePort));

                var secureString = new GuardedString();
                "changeit".ToCharArray().ToList().ForEach(p => secureString.AppendChar(p));

                ClientAuthenticationValidator validator = new ClientAuthenticationValidator();
                validator.Add(new SingleTenantPrincipal(serverConnectorFramework), secureString.GetBase64SHA1Hash());

                _close = Start(validator, serviceAddress);
                _close += () => serverConnectorFramework.Dispose();

                // ----------

                _clientConnectorFramework = new ConnectorFramework();

                _connectionInfo = new RemoteWSFrameworkConnectionInfo
                {
                    RemoteUri = new Uri(String.Format("http://localhost.fiddler:{0}/openicf", freePort)),
                    Principal = ConnectionPrincipal.DefaultName,
                    Password = secureString
                };
            }
            catch (Exception e)
            {
                TraceUtil.TraceException("Failed", e);
                throw;
            }
        }
 protected WebSocketWrapper(RemoteWSFrameworkConnectionInfo info,
     IMessageListener<WebSocketConnectionGroup, WebSocketConnectionHolder, RemoteOperationContext>
         messageListener,
     Func<WebSocketConnectionHolder, HandshakeMessage, RemoteOperationContext> handshaker)
 {
     _messageListener = messageListener;
     _handshaker = handshaker;
     _ws = new ClientWebSocket();
     _connectPromise = new TaskCompletionSource<WebSocketConnectionHolder>();
     _ws.Options.KeepAliveInterval = TimeSpan.FromSeconds(20);
     _ws.Options.AddSubProtocol(RemoteWSFrameworkConnectionInfo.OPENICF_PROTOCOL);
     string enc =
         Convert.ToBase64String(
             Encoding.GetEncoding("iso-8859-1")
                 .GetBytes(info.Principal + ":" +
                           IdentityConnectors.Common.Security.SecurityUtil.Decrypt(info.Password)));
     _ws.Options.SetRequestHeader("Authorization", "Basic " + enc);
     _info = info;
     _cancellationToken = _cancellationTokenSource.Token;
 }
 /// <summary>
 ///     Creates a new instance.
 /// </summary>
 /// <param name="uri">The URI of the WebSocket server.</param>
 /// <param name="messageListener"></param>
 /// <param name="handshaker"></param>
 /// <returns></returns>
 public static WebSocketWrapper Create(RemoteWSFrameworkConnectionInfo info,
     IMessageListener<WebSocketConnectionGroup, WebSocketConnectionHolder, RemoteOperationContext>
         messageListener,
     Func<WebSocketConnectionHolder, HandshakeMessage, RemoteOperationContext> handshaker)
 {
     return new WebSocketWrapper(info, messageListener, handshaker);
 }
        public IRemoteConnectorInfoManager Connect(RemoteWSFrameworkConnectionInfo info)
        {
            if (Running)
            {
                ClientRemoteConnectorInfoManager manager;
                registry.TryGetValue(info, out manager);
                if (null == manager)
                {
                    lock (registry)
                    {
                        registry.TryGetValue(info, out manager);
                        if (null == manager)
                        {
                            manager = new ClientRemoteConnectorInfoManager(info, MessageListener, connectionGroups);
                            manager.Disposed += (sender, args) =>
                            {
                                ClientRemoteConnectorInfoManager ignore;
                                registry.TryRemove(info, out ignore);
                            };

                            registry.TryAdd(info, manager);
                            if (!Running && registry.TryRemove(info, out manager))
                            {
                                manager.Dispose();
                                throw new InvalidOperationException("RemoteConnectionInfoManagerFactory is shut down");
                            }
                        }
                    }
                }
                return manager;
            }
            throw new InvalidOperationException("RemoteConnectionInfoManagerFactory is shut down");
        }