public void AddInstance(ServiceEndpointRole role, Uri uri)
 {
     if (instances == null)
     {
         instances = new List <SF_EndpointInstance>();
     }
     instances.Add(new SF_EndpointInstance(role, uri));
     instances.Sort((x, y) =>
     {
         if (x.role_ - y.role_ != 0)
         {
             return(x.role_ - y.role_);
         }
         return(string.CompareOrdinal(x.endpoint_.AbsolutePath, y.endpoint_.AbsolutePath));
     });
 }
        private EndpointType(string serviceAbsolutePath, Guid partitionId, string endpointName,
                             bool defaultEndpoint, ServiceEndpointRole role, ServicePartitionKind partitionKind, long?partitionLow,
                             long?partitionHigh, string partitionKey)
        {
            ServiceAbsolutePath = serviceAbsolutePath;
            PartitionId         = partitionId;
            EndpointName        = endpointName;
            DefaultEndpoint     = defaultEndpoint;
            Role          = role;
            PartitionKind = partitionKind;
            PartitionLow  = partitionLow;
            PartitionHigh = partitionHigh;
            PartitionKey  = partitionKey;
            _key          = $"{serviceAbsolutePath}|{partitionId}|{endpointName}|{role}";

            IngressId = $"I|{_key}";
            EgressId  = $"E|{_key}";
        }
        private static void Handler(Object sender, EventArgs eargs)
        {
            try
            {
                var notification = ((FabricClient.ServiceManagementClient.ServiceNotificationEventArgs)eargs).Notification;
                if (notification.Endpoints.Count == 0)
                {
                    //remove
                    lock (lock_)
                    {
                        if (partitions_ != null)
                        {
                            partitions_.Remove(notification.PartitionId);
                            RemovePartitionFromService(notification.PartitionId);
                        }
                        else
                        {
                            partitionsAdd_.Remove(notification.PartitionId);
                            partitionsRemove_[notification.PartitionId] = null;
                        }
                        EnvoyDefaults.LogMessage(String.Format("Removed: {0}", notification.PartitionId));

                        return;
                    }
                }

                List <SF_Endpoint>  listeners = new List <SF_Endpoint>();
                ServiceEndpointRole role      = ServiceEndpointRole.Invalid;
                foreach (var notificationEndpoint in notification.Endpoints)
                {
                    if (notificationEndpoint.Address.Length == 0)
                    {
                        continue;
                    }
                    JObject addresses;
                    try
                    {
                        addresses = JObject.Parse(notificationEndpoint.Address);
                    }
                    catch
                    {
                        continue;
                    }

                    var notificationListeners = addresses["Endpoints"].Value <JObject>();
                    foreach (var notificationListener in notificationListeners)
                    {
                        int listenerIndex = listeners.FindIndex(x => x.Name == notificationListener.Key);
                        if (listenerIndex == -1)
                        {
                            listeners.Add(new SF_Endpoint(notificationListener.Key));
                            listenerIndex = listeners.Count - 1;
                        }
                        try
                        {
                            var listenerAddressString = notificationListener.Value.ToString();
                            if (!listenerAddressString.StartsWith("http") &&
                                !listenerAddressString.StartsWith("https"))
                            {
                                continue;
                            }
                            var listenerAddress = new Uri(listenerAddressString);
                            if (listenerAddress.HostNameType == UriHostNameType.Dns)
                            {
                                var ipaddrs = Dns.GetHostAddresses(listenerAddress.Host);
                                foreach (var ipaddr in ipaddrs)
                                {
                                    if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                    {
                                        var saddrstring = ipaddr.ToString();
                                        if (saddrstring.StartsWith("172"))
                                        {
                                            listenerAddress = new Uri(listenerAddress.Scheme + "://" +
                                                                      saddrstring +
                                                                      ":" + listenerAddress.Port + listenerAddress.PathAndQuery);
                                            break;
                                        }
                                    }
                                }
                            }
                            listeners[listenerIndex].AddInstance(notificationEndpoint.Role, listenerAddress);
                        }
                        catch (System.Exception e)
                        {
                            EnvoyDefaults.LogMessage(String.Format("Error={0}", e));
                        }
                    }
                    if (role == ServiceEndpointRole.Invalid)
                    {
                        role = notificationEndpoint.Role;
                    }
                }

                // Remove any listeners without active endpoints
                listeners.RemoveAll(x => x.InstanceCount() == 0);

                if (listeners.Count == 0)
                {
                    return;
                }

                var partitionInfo = new SF_Partition(notification.ServiceName,
                                                     (role == ServiceEndpointRole.Stateless) ? ServiceKind.Stateless : ServiceKind.Stateful,
                                                     notification.PartitionInfo,
                                                     notification.Version,
                                                     listeners
                                                     );

                lock (lock_)
                {
                    if (partitions_ != null)
                    {
                        partitions_[notification.PartitionId] = partitionInfo;
                        AddPartitionToService(notification.PartitionId, partitionInfo);
                    }
                    else
                    {
                        partitionsRemove_.Remove(notification.PartitionId);
                        partitionsAdd_[notification.PartitionId] = partitionInfo;
                    }
                    EnvoyDefaults.LogMessage(String.Format("Added: {0}={1}", notification.PartitionId,
                                                           JsonConvert.SerializeObject(partitionInfo)));
                }
            }
            catch (Exception e)
            {
                EnvoyDefaults.LogMessage(String.Format("Error={0}", e.Message));
                EnvoyDefaults.LogMessage(String.Format("Error={0}", e.StackTrace));
            }
        }
 public SF_EndpointInstance(ServiceEndpointRole role, Uri uri)
 {
     role_     = role;
     endpoint_ = uri;
 }
 public static EndpointType CreateNamedPartitioned(string serviceAbsolutePath, Guid partitionId,
                                                   string endpointName, bool defaultEndpoint, ServiceEndpointRole role, string partitionName)
 {
     return(new EndpointType(serviceAbsolutePath,
                             partitionId, endpointName, defaultEndpoint, role, ServicePartitionKind.Named, null, null,
                             partitionName));
 }
 public static EndpointType CreateInt64Partitioned(string serviceAbsolutePath, Guid partitionId,
                                                   string endpointName, bool defaultEndpoint, ServiceEndpointRole role, long partitionLow,
                                                   long partitionHigh)
 {
     return(new EndpointType(serviceAbsolutePath,
                             partitionId, endpointName, defaultEndpoint, role, ServicePartitionKind.Int64Range, partitionLow,
                             partitionHigh, null));
 }
 public static EndpointType CreateSingleton(string serviceAbsolutePath, Guid partitionId,
                                            string endpointName, bool defaultEndpoint, ServiceEndpointRole role)
 {
     return(new EndpointType(serviceAbsolutePath,
                             partitionId, endpointName, defaultEndpoint, role, ServicePartitionKind.Singleton, null, null, null));
 }