/// <summary> /// Opens a host to accept incoming connections. Connects to a registration server. /// </summary> /// <param name="registrationServerNameOrAddress">Name or address of the registration server.</param> /// <param name="registrationServerPort">The port number to the registration server.</param> /// <param name="listenerPort">The port that this node accepts connections on.</param> private void OpenAndConnectToRegistrationServer(string registrationServerNameOrAddress, int registrationServerPort, int listenerPort) { if (_host == null) { try { //Open a connection to the registration server IPEndPoint registrationServerEndPoint = new IPEndPoint(Dns.GetHostEntry(registrationServerNameOrAddress).AddressList[0], registrationServerPort); _registrationServerProxy = ProxyFactory.CreateProxy <IRegistrationServer>(registrationServerEndPoint); //get the nodes that are already registered List <IPEndPoint> remoteNodeEndPoints = _registrationServerProxy.GetAllNodeEndPoints(); _remoteNodes.Clear(); foreach (IPEndPoint endpoint in remoteNodeEndPoints) { INode remoteNode = new RemoteNodeProxy(endpoint); _remoteNodes.Add(remoteNode.GetId(), remoteNode); } //open the host that contains this node _host = new ServiceHost(this, listenerPort); _host.Open(); //Register the host with the registration server. This causes all other registered nodes to connect to this one _registrationServerProxy.RegisterNode(_host.EndPoint); } catch (Exception ex) { string msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message; Log.Error("Could not connect to registration server. Message reads : {0}", msg); } } }
public bool Open(string registrationServerNameOrAddress, int registrationServerPort, int listenerPort) { if (_host == null) { try { //Open a connection to the registration server var addressList = Dns.GetHostEntry(registrationServerNameOrAddress).AddressList; IPAddress address = null; foreach (var a in addressList) { if (a.AddressFamily == AddressFamily.InterNetwork) { address = a; break; } } var registrationServerEndPoint = new IPEndPoint(address, registrationServerPort); _registrationServerProxy = ProxyFactory.CreateProxy <IRegistrationServer>(registrationServerEndPoint); //get node hosts that are already registered _remoteHosts.Clear(); var remoteHostEndPoints = _registrationServerProxy.GetAllHostEndPoints(); foreach (var remoteHostEndPoint in remoteHostEndPoints) { INodeHost remoteHost = ProxyFactory.CreateProxy <INodeHost>(remoteHostEndPoint); _remoteHosts.Add(remoteHost.GetId(), remoteHost); } //open the host that contains this node _host = new ServiceHost(this, listenerPort); _host.Open(); //Register the host with the registration server. This causes all other registered nodes to connect to this one _registrationServerProxy.RegisterHost(_host.EndPoint); return(true); } catch (Exception ex) { string msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message; Log.Error("Could not connect to registration server. Message reads : {0}", msg); } } return(false); }