Example #1
0
        private void ConnectToRemoteHost(object m)
        {
            bool connected = false;
            StatusNotifyMessage message = (StatusNotifyMessage)m;

            _remoteResolvers = Tools.getRemoteHosts();

            while (connected == false && _nHostIndex < _remoteResolvers.Count)
            {
                lock (_counterLock)
                {
                    //localhost connections filter
                    if (_remoteResolvers[_nHostIndex].IP == "127.0.0.1" || _remoteResolvers[_nHostIndex].IP.ToLower() == "localhost")
                    {
                        if (_nHostIndex < (_remoteResolvers.Count - 1))
                        {
                            _nHostIndex++;
                        }
                        else
                        {
                            _nHostIndex = 0;
                        }

                        continue;
                    }

                    XMLLogger.WriteRemoteActivity("Connecting to " + _remoteResolvers[_nHostIndex].IP);

                    NetTcpBinding   tcpBinding     = new NetTcpBinding();
                    EndpointAddress remoteEndpoint = new EndpointAddress(_remoteResolvers[_nHostIndex].netAddress);
                    tcpBinding.Security.Mode = SecurityMode.None;

                    ChannelFactory <IRemote> cf = new ChannelFactory <IRemote>(tcpBinding, remoteEndpoint);
                    _remoteChannel = cf.CreateChannel();

                    try
                    {
                        _remoteChannel.Open();

                        //Retrieve Remote IP Addresses
                        foreach (var newRemote in _remoteChannel.RetrieveContactList())
                        {
                            if (!_remoteResolvers.Exists(delegate(RemoteHost x) { return(x.netAddress == newRemote.netAddress); }))
                            {
                                _remoteResolvers.Add(newRemote);
                                Tools.updateRemoteHosts(newRemote);
                            }
                        }

                        XMLLogger.WriteRemoteActivity("Connected to: " + _remoteResolvers[_nHostIndex].IP);

                        XMLLogger.WriteRemoteActivity("Forwarding Energy Request from: " + message.header.Sender + "To: " + _remoteResolvers[_nHostIndex]);
                        XMLLogger.WriteRemoteActivity("Message ID: " + message.header.MessageID);

                        _remoteChannel.ManageRemoteEnergyRequest(MessageFactory.createRemoteEnergyRequestMessage(message,
                                                                                                                 _remoteResolvers[_nHostIndex].name,
                                                                                                                 this.name,
                                                                                                                 Tools.getLocalIP(),
                                                                                                                 Tools.getResolverServicePort()
                                                                                                                 ));

                        this.isRemoteConnected = true;
                        connected = true;
                    }

                    catch (Exception e)
                    {
                        XMLLogger.WriteErrorMessage(this.GetType().FullName.ToString(), "Unable to connect to remote host.");
                        //XMLLogger.WriteErrorMessage(this.GetType().FullName.ToString(), e.ToString()); //For debug purpose
                        _nHostIndex++;
                        if (_nHostIndex >= _remoteResolvers.Count)
                        {
                            _nHostIndex = 0;
                            _remoteChannel.Abort();
                        }

                        connected = false;
                    }
                }
            }


            if (_nHostIndex >= _remoteResolvers.Count)
            {
                lock (_counterLock)
                {
                    _nHostIndex = 0;
                }
            }
        }