Ejemplo n.º 1
0
 public BeliefRepository(Serializer serializer, IHashFunction hashFunction)
 {
     this.beliefCache  = new SortedDictionary <CacheKey, CachedBelief>();
     this.serializer   = serializer;
     this.hashFunction = hashFunction;
     this.currentState = new RepositoryState(0);
 }
Ejemplo n.º 2
0
        public bool Commit(Belief belief)
        {
            lock (this)
            {
                CacheKey key = new CacheKey(belief.getTypeKey(), belief.getId());

                CachedBelief oldCache = null;
                if (beliefCache.TryGetValue(key, out oldCache))
                {
                    BeliefMerger merger = new BeliefMerger();
                    Belief       merged = merger.merge(oldCache.GetBelief(), belief);
                    if (merged == oldCache.GetBelief())
                    {
                        return(false);
                    }
                    belief = merged;
                }

                byte[]       serialized = serializer.serializeBelief(belief);
                Hash         hash       = hashFunction.generateHash(serialized);
                CachedBelief cached     = new CachedBelief(belief, serialized, hash);
                beliefCache[key] = cached;
                currentState     = RegenerateState();

                return(true);
            }
        }
Ejemplo n.º 3
0
 public RepositoryState RegenerateState()
 {
     lock (this)
     {
         RepositoryState state = new RepositoryState(currentState.RevisionNumber() + 1);
         foreach (KeyValuePair <CacheKey, CachedBelief> entry in beliefCache)
         {
             state.Add(entry.Key, entry.Value.GetHash());
         }
         return(state);
     }
 }
Ejemplo n.º 4
0
        public void handleMessage(BSPMessage message)
        {
            RepositoryStateSerializer serializer    = new RepositoryStateSerializer();
            RepositoryState           incomingState = serializer.deserialize(message.getData());

            if (incomingState.RevisionNumber() > remoteState.RevisionNumber())
            {
                this.remoteState = incomingState;
                IEnumerable <CachedBelief> changedBeliefs = repo.Diff(remoteState);
                foreach (CachedBelief belief in changedBeliefs)
                {
                    protocol.post(belief);
                }
            }
        }
Ejemplo n.º 5
0
        public ICollection <CacheKey> Diff(RepositoryState other)
        {
            HashSet <CacheKey> diffSet = new HashSet <CacheKey>();

            foreach (RepositoryObject obj in GetObjects())
            {
                Hash otherHash = other.Find(obj.key);
                if (otherHash == null || !obj.hash.Equals(otherHash))
                {
                    diffSet.Add(obj.key);
                }
            }

            return(diffSet);
        }
Ejemplo n.º 6
0
        public IEnumerable <CachedBelief> Diff(RepositoryState state)
        {
            lock (this)
            {
                RepositoryState        myState  = CurrentState();
                ICollection <CacheKey> diffKeys = myState.Diff(state);

                List <CachedBelief> beliefs = new List <CachedBelief>();
                foreach (CacheKey key in diffKeys)
                {
                    CachedBelief cachedBelief = beliefCache[key];
                    beliefs.Add(cachedBelief);
                }
                return(beliefs);
            }
        }
Ejemplo n.º 7
0
        public RepositoryState deserialize(NetworkBuffer buffer)
        {
            int             revisionNumber = buffer.parseInt32(COMMIT_OFFSET);
            RepositoryState state          = new RepositoryState(revisionNumber);

            int numberOfBeliefs = buffer.parseInt32(NUM_BELIEFS_OFFSET);
            int baseOffset      = HEADER_SIZE;

            for (int i = 0; i < numberOfBeliefs; ++i, baseOffset += BELIEF_SIZE)
            {
                int typeVal            = buffer.readByte(baseOffset + TYPE_OFFSET);
                Belief.BeliefType type = Belief.BeliefType.INVALID;
                if (Enum.IsDefined(typeof(Belief.BeliefType), typeVal))
                {
                    type = (Belief.BeliefType)typeVal;
                }
                else
                {
                    Log.error("Invalid belief type: " + typeVal);
                }

                int    customType = buffer.parseInt32(baseOffset + CUSTOM_TYPE_OFFSET);
                int    id         = buffer.parseInt32(baseOffset + BELIEF_ID_OFFSET);
                byte[] digest     = buffer.readBytes(baseOffset + DIGEST_OFFSET, DIGEST_SIZE);
                Hash   hash       = new Hash(digest);

                Belief.Key key = null;
                if (type == Belief.BeliefType.CUSTOM)
                {
                    key = new Belief.Key(customType);
                }
                else
                {
                    key = new Belief.Key(type);
                }

                state.Add(new CacheKey(key, id), hash);
            }

            return(state);
        }
Ejemplo n.º 8
0
        public NetworkBuffer serialize(RepositoryState state)
        {
            NetworkBuffer buffer = new NetworkBuffer(HEADER_SIZE + (state.Size() * BELIEF_SIZE));

            buffer.writeInt32(COMMIT_OFFSET, state.RevisionNumber());
            buffer.writeInt32(NUM_BELIEFS_OFFSET, state.Size());

            int offset = HEADER_SIZE;

            foreach (RepositoryState.RepositoryObject belief in state.GetObjects())
            {
                buffer.writeByte(offset + TYPE_OFFSET, (byte)belief.key.getBeliefType());
                buffer.writeInt32(offset + CUSTOM_TYPE_OFFSET, belief.key.getCustomType());
                buffer.writeInt32(offset + BELIEF_ID_OFFSET, belief.key.getBeliefID());

                byte[] hashBytes = belief.hash.GetBytes();
                buffer.writeBytes(hashBytes, 0, offset + DIGEST_OFFSET, hashBytes.Length);
            }

            return(buffer);
        }
Ejemplo n.º 9
0
 public SyncMessageHandler(BeliefRepository repo, AgentMessageHandler protocol)
 {
     this.repo     = repo;
     this.protocol = protocol;
     remoteState   = new RepositoryState(-1);
 }