示例#1
0
        /// <summary>
        /// Connect client
        /// </summary>
        /// <param name="baseAdapter"></param>
        /// <param name="connProperties"></param>
        /// <returns></returns>
        public NetGraph ConnectClient(IDataAdapter baseAdapter, PropertyBag connProperties)
        {
            IDataAdapter   server     = null;
            IDataAdapter   client     = null;
            ProxyToken     token      = null;
            NetGraph       graph      = null;
            NetGraph       retGraph   = null;
            MetaDictionary meta       = new MetaDictionary();
            PropertyBag    properties = new PropertyBag("Properties");

            try
            {
                properties.AddBag(connProperties);

                token = _proxyServer.Accept(baseAdapter, meta, _globalMeta, this);

                if (token != null)
                {
                    token = FilterToken(token);
                    if (token.Status == NetStatusCodes.Success)
                    {
                        ProxyClient proxyClient = token.Client ?? _proxyClient;

                        if (token.Bind)
                        {
                            client = proxyClient.Bind(token, _logger, meta, _globalMeta, properties.AddBag("Client"));
                        }
                        else
                        {
                            client = proxyClient.Connect(token, _logger, meta, _globalMeta, properties.AddBag("Client"));
                        }

                        server = _proxyServer.Complete(token, meta, _globalMeta, this, client);

                        if ((token.Status == NetStatusCodes.Success) && (client != null))
                        {
                            NetGraphFactory factory = token.Graph != null ? token.Graph : _factory;

                            token.PopulateBag(properties.AddBag("Token"));

                            // Negotiate SSL or other bespoke encryption mechanisms
                            if (token.Layers != null)
                            {
                                foreach (INetworkLayer layer in token.Layers)
                                {
                                    layer.Negotiate(ref server, ref client, token, _logger, meta,
                                                    _globalMeta, properties, DefaultBinding);
                                }
                            }

                            var clients = factory.GetNodes <ClientEndpointFactory>();
                            var servers = factory.GetNodes <ServerEndpointFactory>();

                            if ((clients.Length > 0) && (servers.Length > 0))
                            {
                                graph = CreateNetGraph(factory, meta, properties);

                                graph.BindEndpoint(clients[0].Id, client);
                                graph.BindEndpoint(servers[0].Id, server);
                                if (token.NetworkDescription != null)
                                {
                                    graph.NetworkDescription = token.NetworkDescription;
                                }
                                else
                                {
                                    graph.NetworkDescription = String.Format("{0} <=> {1}",
                                                                             server.Description, client.Description);
                                }

                                PropertyBag networkService = properties.AddBag("NetworkService");

                                networkService.AddValue("ClientId", clients[0].Id);
                                networkService.AddValue("ServerId", servers[0].Id);
                                networkService.AddValue("ClientAdapter", client);
                                networkService.AddValue("ServerAdapter", server);
                                networkService.AddValue("Token", token);

                                graph.Start();

                                OnNewConnection(graph);

                                retGraph = graph;
                            }
                            else
                            {
                                _logger.LogError(CANAPE.Net.Properties.Resources.ProxyNetworkService_InvalidGraph);
                            }
                        }
                    }
                    else
                    {
                        _logger.LogVerbose(CANAPE.Net.Properties.Resources.ProxyNetworkService_ConnectionFiltered);
                        server = _proxyServer.Complete(token, meta, _globalMeta, this, client);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
            finally
            {
                if (retGraph == null)
                {
                    try
                    {
                        if (graph != null)
                        {
                            ((IDisposable)graph).Dispose();
                        }
                        if (server != null)
                        {
                            server.Dispose();
                        }
                        if (client != null)
                        {
                            client.Dispose();
                        }
                        if (token != null)
                        {
                            token.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.SystemLogger.LogException(ex);
                    }
                }
            }

            return(retGraph);
        }