Esempio n. 1
0
 public bool Unregister(string identity, out HostEntry entry)
 {
     if (identity == null)
     {
         throw new ArgumentNullException("key");
     }
     if (identity.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("key");
     }
     entry = null;
     lock (this.syncobj)
     {
         Host host;
         if (!this.hosts.TryRemove(identity, out host))
         {
             return(false);
         }
         else
         {
             int i = FindIndex(this.buckets, identity);
             if (i > -1)
             {
                 this.buckets.RemoveAt(i);
             }
             entry = host.Entry;
         }
         return(true);
     }
 }
Esempio n. 2
0
 public bool Register(string identity, HostEntry entry)
 {
     if (identity == null)
     {
         throw new ArgumentNullException("key");
     }
     if (identity.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("key");
     }
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     lock (this.syncobj)
     {
         if (!this.hosts.TryAdd(identity, new Host(identity, entry)))
         {
             Host host;
             if (this.hosts.TryGetValue(identity, out host))
             {
                 if (host.Entry == entry)
                 {
                     return(true);
                 }
             }
             return(false);
         }
         else
         {
             this.buckets.Add(identity);
         }
         return(true);
     }
 }
Esempio n. 3
0
        private void QueryHostEntry(MalockSocket socket, int sequence, string key)
        {
            string           identity;
            HostEntry        entry   = this.nnsTable.GetEntry(key, out identity);
            MalockNnsMessage message = new MalockNnsMessage();

            message.Key      = key;
            message.Sequence = sequence;
            message.Command  = MalockMessage.COMMON_COMMAND_ERROR;
            if (entry != null)
            {
                message.Command = MalockNnsMessage.CLIENT_COMMAND_QUERYHOSTENTRYINFO;
            }
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(stream))
                {
                    message.Serialize(bw);
                    if (entry != null)
                    {
                        entry.Serialize(bw);
                    }
                    MalockMessage.TrySendMessage(socket, stream);
                }
            }
            this.PostSynHostEntryMessage(this.nnsStanbyClient, key, identity, entry);
        }
Esempio n. 4
0
 public bool SetAvailable(string identity, string address, bool available)
 {
     if (identity == null)
     {
         throw new ArgumentNullException("key");
     }
     if (identity.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("key");
     }
     if (address == null || address.Length <= 0)
     {
         return(false);
     }
     lock (this.syncobj)
     {
         Host host;
         if (!this.hosts.TryGetValue(identity, out host))
         {
             return(false);
         }
         HostEntry entry = host.Entry;
         var       i     = entry.Select(address);
         if (i == null)
         {
             return(false);
         }
         else
         {
             i.Available = available;
         }
         return(true);
     }
 }
Esempio n. 5
0
 internal static bool TryDeserialize(Stream stream, out HostEntry entry)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     return(TryDeserialize(new BinaryReader(stream), out entry));
 }
Esempio n. 6
0
 public NnsError TryGetHostEntry(string key, int timeout, out HostEntry entry)
 {
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     if (key.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("key");
     }
     return(TryInternalInvoke((callback) => GetHostEntryAsync(key, timeout, callback), out entry));
 }
Esempio n. 7
0
 private void ProcessServer(MalockSocket socket, MalockNnsMessage message, Stream stream)
 {
     if (message.Command == MalockNnsMessage.SERVER_NNS_COMMAND_SYN_HOSTENTRYINFO)
     {
         this.SynQueryHostEntry(socket, message.Identity, message.Key, HostEntry.Deserialize(stream));
     }
     else if (message.Command == MalockNnsMessage.SERVER_NDN_COMMAND_REGISTERHOSTENTRYINFO)
     {
         this.RegisterHostEntry(socket, message.Sequence, HostEntry.Deserialize(stream));
     }
     else if (message.Command == MalockNnsMessage.SERVER_NNS_COMMAND_DUMPHOSTENTRYINFO)
     {
         this.DumpHostEntry(socket, message);
     }
 }
Esempio n. 8
0
 internal Host(string identity, HostEntry entry)
 {
     if (identity == null)
     {
         throw new ArgumentNullException("identity");
     }
     if (identity.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("identity");
     }
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     this.Entry    = entry;
     this.Identity = identity;
 }
Esempio n. 9
0
            internal static bool TryDeserialize(BinaryReader reader, out Host host)
            {
                host = null;
                HostEntry entry;

                if (!HostEntry.TryDeserialize(reader, out entry))
                {
                    return(false);
                }
                string identity;

                if (!MalockMessage.TryFromStringInReadStream(reader, out identity))
                {
                    return(false);
                }
                host = new Host(identity, entry);
                return(true);
            }
Esempio n. 10
0
 public bool SetEntry(string identity, string key, HostEntry entry)
 {
     if (identity == null)
     {
         throw new ArgumentNullException("identity");
     }
     if (identity.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("identity");
     }
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     if (key.Length <= 0)
     {
         throw new ArgumentOutOfRangeException("key");
     }
     if (entry == null)
     {
         return(false);
     }
     lock (this.syncobj)
     {
         this.Register(identity, entry);
         Host host = null;
         do
         {
             if (this.entrys.TryGetValue(key, out host))
             {
                 this.entrys.TryRemove(key, out host);
             }
         } while (false);
         if (!this.hosts.TryGetValue(identity, out host))
         {
             return(false);
         }
         if (host == null || host.Entry != entry)
         {
             return(false);
         }
         return(this.entrys.TryAdd(key, host));
     }
 }
Esempio n. 11
0
        private void RegisterHostEntry(MalockSocket socket, int sequence, HostEntry entry)
        {
            MalockNnsMessage message = new MalockNnsMessage();

            message.Sequence = sequence;
            message.Command  = MalockMessage.COMMON_COMMAND_ERROR;
            if (entry != null)
            {
                lock (this.nnsTable.GetSynchronizationObject())
                {
                    if (this.nnsTable.Register(socket.Identity, entry))
                    {
                        message.Command = MalockNnsMessage.SERVER_NDN_COMMAND_REGISTERHOSTENTRYINFO;
                    }
                    this.nnsTable.SetAvailable(socket.Identity, socket.Address, true);
                }
            }
            MalockMessage.TrySendMessage(socket, message);
        }
Esempio n. 12
0
 public void GetAllHostEntryAsync(int timeout, Action <NnsError, IEnumerable <HostEntry> > state)
 {
     if (state == null)
     {
         throw new ArgumentNullException("state");
     }
     if (timeout <= 0 && timeout != -1)
     {
         state(NnsError.kTimeout, emptryentries);
     }
     else
     {
         Exception exception = null;
         if (!MalockMessage.TryInvokeAsync(this, this.NewMessage(null, MSG.CLIENT_COMMAND_DUMPHOSTENTRYINFO),
                                           timeout, (errno, message, stream) =>
         {
             if (errno == MalockMessage.Mappable.ERROR_NOERROR)
             {
                 IList <HostEntry> hosts = new List <HostEntry>();
                 NnsTable.Host.DeserializeAll(stream, (host) =>
                 {
                     HostEntry entry = host.Entry;
                     hosts.Add(entry);
                 });
                 state(NnsError.kSuccess, hosts);
             }
             else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT)
             {
                 state(NnsError.kTimeout, emptryentries);
             }
             else if (errno == MalockMessage.Mappable.ERROR_ABORTED)
             {
                 state(NnsError.kAborted, emptryentries);
             }
         }, ref exception))
         {
             state(NnsError.kAborted, emptryentries);
         }
     }
 }
Esempio n. 13
0
 internal static bool TryDeserialize(BinaryReader reader, out HostEntry entry)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     entry = null;
     do
     {
         HostEntry host = new HostEntry();
         if (!host.Primary.Deserialize(reader))
         {
             return(false);
         }
         if (!host.Standby.Deserialize(reader))
         {
             return(false);
         }
         entry = host;
     } while (false);
     return(true);
 }
Esempio n. 14
0
        public override bool Equals(object obj)
        {
            HostEntry key = obj as HostEntry;

            if (key == null)
            {
                return(false);
            }
            if (RuntimeHelpers.Equals(this, obj))
            {
                return(true);
            }
            if (this.Primary.Address == key.Primary.Address &&
                this.Standby.Address == key.Standby.Address)
            {
                return(true);
            }
            if (this.Primary.Address == key.Standby.Address &&
                this.Standby.Address == key.Primary.Address)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 15
0
 private void SynQueryHostEntry(MalockSocket socket, string identity, string key, HostEntry entry)
 {
     if (entry == null || socket == null || string.IsNullOrEmpty(identity) || string.IsNullOrEmpty(key))
     {
         return;
     }
     lock (this.nnsTable.GetSynchronizationObject())
     {
         this.nnsTable.SetEntry(identity, key, entry);
     }
 }
Esempio n. 16
0
 public NnsError TryGetHostEntry(string key, out HostEntry entry)
 {
     return(TryGetHostEntry(key, Malock.DefaultTimeout, out entry));
 }
Esempio n. 17
0
        private bool PostSynHostEntryMessage(IMalockSocket socket, string key, string identity, HostEntry entry)
        {
            if (entry == null || string.IsNullOrEmpty(identity) || string.IsNullOrEmpty(key))
            {
                return(false);
            }
            MalockNnsMessage message = new MalockNnsMessage();

            message.Key      = key;
            message.Identity = identity;
            message.Sequence = MalockMessage.NewId();
            message.Command  = MalockNnsMessage.SERVER_NNS_COMMAND_SYN_HOSTENTRYINFO;
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(stream))
                {
                    message.Serialize(bw);
                    if (entry != null)
                    {
                        entry.Serialize(bw);
                    }
                    MalockMessage.TrySendMessage(socket, stream);
                }
            }
            return(true);
        }
Esempio n. 18
0
 private void InternalRemoteGetHostEntryAsync(string key, int timeout, Action <NnsError, HostEntry> state, bool retrying)
 {
     if ((retrying && timeout <= 0) || (timeout <= 0 && timeout != -1))
     {
         state(NnsError.kTimeout, null);
     }
     else if (!this.Available)
     {
         state(NnsError.kAborted, null);
     }
     else
     {
         Stopwatch stopwatch = new Stopwatch();
         stopwatch.Start();
         Action <NnsClient> onaborted = (self) =>
         {
             if (!this.Available)
             {
                 state(NnsError.kAborted, null);
             }
             else
             {
                 var delaytick = Malock.NewTimer();
                 delaytick.Interval = Malock.SmoothingInvokeTime;
                 delaytick.Tick    += delegate
                 {
                     long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                     {
                         stopwatch.Stop();
                         delaytick.Stop();
                     }
                     if (!this.Available)
                     {
                         state(NnsError.kAborted, null);
                     }
                     else
                     {
                         if (timeout != -1)
                         {
                             timeout -= Convert.ToInt32(elapsedMilliseconds);
                         }
                         this.InternalRemoteGetHostEntryAsync(key, timeout, state, timeout == -1 ? false : true);
                     }
                 };
                 delaytick.Start();
             }
         };
         Action <int, MalockMessage, Stream> callback = (errno, response, stream) =>
         {
             bool closedstopwatch = true;
             if (errno == MalockMessage.Mappable.ERROR_NOERROR)
             {
                 if (response.Command != MSG.CLIENT_COMMAND_QUERYHOSTENTRYINFO)
                 {
                     state(NnsError.kError, null);
                 }
                 else
                 {
                     HostEntry entry;
                     if (!HostEntry.TryDeserialize(stream, out entry))
                     {
                         state(NnsError.kError, null);
                     }
                     else
                     {
                         state(NnsError.kSuccess, entry);
                     }
                 }
             }
             else if (errno == MalockMessage.Mappable.ERROR_ABORTED)
             {
                 closedstopwatch = false;
                 onaborted(this);
             }
             else if (errno == MalockMessage.Mappable.ERROR_TIMEOUT)
             {
                 state(NnsError.kTimeout, null);
             }
             if (closedstopwatch)
             {
                 stopwatch.Stop();
             }
         };
         MalockMessage message   = this.NewMessage(key, MSG.CLIENT_COMMAND_QUERYHOSTENTRYINFO);
         Exception     exception = null;
         if (!MalockMessage.TryInvokeAsync(this, message, timeout, callback, ref exception))
         {
             onaborted(this);
         }
     }
 }