public bool ConnectToServer() { bool result = false; //now register to server foreach (SecurityResource item in _SecurityResources.Children) { Console.Write(item.ServerURI); Console.Write(") .. "); Uri uri = new Uri(item.ServerURI); foreach (System.Net.IPAddress address in System.Net.Dns.GetHostAddresses(uri.DnsSafeHost)) { bool secure =false; int port = 5683; if (string.Compare(uri.Scheme, "coaps", true) == 0) { secure = true; port = 5684; } if (uri.Port > 0) port = uri.Port; if (_Channel.Secure != secure) { _Channel.Stop(); _Channel.Secure = secure; _Channel.Start(); } CoapClient coapClient = new CoapClient(); coapClient.EndPoint = _EndPoint; Request request = new Request(Method.POST); request.Destination = new System.Net.IPEndPoint(address, port); request.UriPath = "/rd"; request.UriQuery = string.Concat("ep=test",Environment.MachineName,"<=35"); StringBuilder payLoad = new StringBuilder(); foreach (IResource objectType in _Root.Children) { if (objectType.Visible) { bool instances = false; foreach (IResource instance in objectType.Children) { if (payLoad.Length > 0) payLoad.Append(','); payLoad.Append('<'); payLoad.Append('/'); payLoad.Append(objectType.Name); payLoad.Append('/'); payLoad.Append(instance.Name); payLoad.Append('>'); instances = true; } if (!instances) { if (payLoad.Length > 0) payLoad.Append(','); payLoad.Append('<'); payLoad.Append('/'); payLoad.Append(objectType.Name); payLoad.Append('>'); } } } request.PayloadString = payLoad.ToString(); request.ContentType = (int)MediaType.ApplicationLinkFormat; Response response = coapClient.Send(request); if (response != null && response.StatusCode == StatusCode.Created) { string location = response.LocationPath; int position = location.LastIndexOf('/'); if (position > 0) { ClientID = location.Substring(position + 1); } result = true; _ServerEndPoint = request.Destination; _Location = location; _Timer.Start(); break; } } } return result; }
//System.Net.IPAddress.Parse("fe80::18be:e89d:e85f:278%12") //15685 public bool Bootstrap(string url) { bool result = false; _Timer.Stop(); Uri uri = new Uri(url); foreach (System.Net.IPAddress address in System.Net.Dns.GetHostAddresses(uri.DnsSafeHost)) { CoapClient coapClient = new CoapClient(); coapClient.EndPoint = _EndPoint; Request request = new Request(Method.POST); int port = 5683; if (string.Compare(uri.Scheme, "coaps", true) ==0) port = 5684; if (uri.Port > 0) port = uri.Port; request.Destination = new System.Net.IPEndPoint(address, port); request.UriPath = "/bs"; request.UriQuery = "ep=test"; Response response = coapClient.Send(request); if (response != null && response.StatusCode == StatusCode.Changed) { result = _BootsrapComplete.Wait(30000); break; } } return result; }