Esempio n. 1
0
 public void Store(ResourceId resId, StoreKindData kindData) {
   string resourceId = resId.ToString();
   lock (resources) {
     if (!resources.ContainsKey(resourceId)) {
       resources.Add(resourceId, new Resource(resId, s_ReloadConfig));
     }
     Resource resource = resources[resourceId];
     foreach (StoredData storedData in kindData.Values) {
       resource.AddStoredData(kindData.Kind, storedData, kindData.Generation_counter);
     }
    ResourceStored(resId, kindData);
   }
 }
Esempio n. 2
0
    /// <summary>
    /// Returns FetchKindResponse structs for a given ResourceId and Specifier
    /// </summary>
    /// <param name="resId">The ResourceId</param>
    /// <param name="specifier">The StoredDataSpecifier</param>
    /// <param name="fetchKindResponse">The Fetch result als FetchKindResonse</param>
    /// <returns>true, if value found</returns>
    public bool Fetch(ResourceId resId, StoredDataSpecifier specifier,
      out FetchKindResponse fetchKindResponse) {

      string resouceId = resId.ToString();
      lock (resources) {
        if (!resources.ContainsKey(resouceId)) {
          fetchKindResponse = new FetchKindResponse();
          return false;
        }
        Resource resource = resources[resouceId];
        List<StoredData> machtes = resource.StoredData(specifier);
        if (machtes != null && machtes.Count >= 0) {
          fetchKindResponse = new FetchKindResponse(
            specifier.kindId, resource.GetGeneration(specifier.kindId), machtes);
          return true;
        }
      }
      fetchKindResponse = new FetchKindResponse();
      return false;
    }
Esempio n. 3
0
 public UInt32 CurrentIndex(ResourceId resId, UInt32 kindId) {
   string resourceKindPair = resId.ToString() + kindId.ToString();
   if (!indices.ContainsKey(resourceKindPair))
     indices.Add(resourceKindPair, 0);
   UInt32 index = indices[resourceKindPair];
   indices[resourceKindPair] = ++index;
   return index;
 }
Esempio n. 4
0
    /// <summary>
    /// Returns the current generation counter for a given Resource/Kind pair
    /// </summary>
    /// <param name="id">The resourceId</param>
    /// <param name="kindId"></param>
    /// <returns>The generation counter as UInt64</returns>
    public UInt64 GetGeneration(ResourceId id, UInt32 kindId) {

      if (resources.ContainsKey(id.ToString()))
        return resources[id.ToString()].GetGeneration(kindId);

      throw new KeyNotFoundException(String.Format("No Resouce found for Id {0}", id.ToString()));
    }
Esempio n. 5
0
 /// <summary>
 /// Creates a StoreReq that can be directed a specific NodeId 
 /// </summary>
 /// <param name="destination">The store destination NodeId</param>
 /// <param name="resourceId">The Id of the resource to store</param>
 /// <returns>A complete RELOAD StoreReq message including all headers</returns>
 public ReloadMessage create_store_req(Destination destination, ResourceId resourceId, List<StoreKindData> stored_kind_data, bool replica)
 {
     return create_reload_message(destination, ++m_ReloadConfig.TransactionID,
                                  new StoreReq(resourceId,
                                               stored_kind_data,
                                               m_machine.UsageManager, replica));
 }
Esempio n. 6
0
        /// <summary>
        /// The Fetch request retrieves one or more data elements stored at a
        /// given Resource-ID.  A single Fetch request can retrieve multiple
        /// different kinds.
        /// 
        /// RELOAD base -13 p.92
        /// --alex
        /// </summary>
        /// <param name="resourceName">The resouces name (human readable)</param>
        /// <param name="specifiers">StoredSpecifier objects</param>
        /// <returns></returns>
        public IEnumerator<ITask> Fetch(string resourceName, List<StoredDataSpecifier> specifiers)
        {
            ReloadDialog reloadDialog = null;
            ReloadMessage reloadSendMsg;
            List<IUsage> recUsages = new List<IUsage>();
            ResourceId res_id = new ResourceId(resourceName);

            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR,
                String.Format("Fetch {0} as ResID: {1}", resourceName, res_id));

            Node node = m_topology.routing_table.FindNextHopTo(new NodeId(res_id), true, false);

            if (m_ReloadConfig.IamClient && node == null)
            {
                node = m_ReloadConfig.AdmittingPeer;
            }

            List<Destination> dest_list = new List<Destination>();
            dest_list.Add(new Destination(m_topology.LocalNode.Id));
            List<FetchKindResponse> fetchKindResponses = new List<FetchKindResponse>();
            FetchKindResponse fetchKindResponse = null;

            if (node == null || node.Id == m_ReloadConfig.LocalNodeID)
            {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "Local Fetch.");
                m_ReloadConfig.StartFetchAttach = DateTime.Now;
                foreach (StoredDataSpecifier specifier in specifiers)
                {
                    var responses = new List<FetchKindResponse>();
                    if (m_topology.Fetch(res_id, specifier, out fetchKindResponse))
                    {
                        responses.Add(fetchKindResponse);
                        foreach (StoredData sd in fetchKindResponse.values)
                        {
                            if (m_ReloadConfig.AccessController.validateDataSignature(res_id, fetchKindResponse.kind, sd))
                                recUsages.Add(sd.Value.GetUsageValue);
                            else
                            {
                                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "DATA SIGNATURE INVALID!!");
                            }
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO,
                                String.Format("Fetch successful, got {0}",
                                    sd.Value.GetUsageValue.Report()));
                        }
                        m_ReloadConfig.ConnEstEnd = DateTime.Now;
                    }
                    OnFetchedData(res_id, responses);
                }


                if (fetchDone != null)
                {
                    if (recUsages.Count == 0)
                    {
                        foreach (StoredDataSpecifier specifier in specifiers)
                            recUsages.Add(new NoResultUsage(specifier.ResourceName));
                    }
                    fetchDone.Post(recUsages);
                }
                yield break;
            }
            else
            {
                reloadSendMsg = create_fetch_req(new Destination(res_id), specifiers);
            }

            int RetransmissionTime = ReloadGlobals.RetransmissionTime +
              ReloadGlobals.MaxTimeToSendPacket;

            int iRetrans = ReloadGlobals.MaxRetransmissions;

            while (iRetrans >= 0 &&
              m_ReloadConfig.State < ReloadConfig.RELOAD_State.Shutdown)
            {
                try
                {
                    reloadDialog = new ReloadDialog(m_ReloadConfig, m_flm, node);

                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR,
                      String.Format("{0} ==> {1} TransId={2:x16}",
                        reloadSendMsg.reload_message_body.RELOAD_MsgCode.ToString().
                        PadRight(16, ' '), node.Id, reloadSendMsg.TransactionID));
                    m_ReloadConfig.StartFetchAttach = DateTime.Now;
                    Arbiter.Activate(m_DispatcherQueue,
                      new IterativeTask<ReloadMessage, ReloadMessageFilter, int>(
                      reloadSendMsg, new ReloadMessageFilter(reloadSendMsg.TransactionID),
                      RetransmissionTime, reloadDialog.Execute));
                }
                catch (Exception ex)
                {
                    fetchDone.Post(new List<IUsage> { new NullUsage() });
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR,
                      "Fetch: " + ex.Message);
                }

                yield return Arbiter.Receive(false, reloadDialog.Done, done => { });

                if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                    break;


                /* If a response has not been received when the timer fires, the request
                   is retransmitted with the same transaction identifier. 
                */
                --iRetrans;
            }

            try
            {
                if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                {
                    //the SourceNodeID delivered from reloadDialog comes from connection table and is the last hop of the message
                    ReloadMessage reloadRcvMsg = reloadDialog.ReceivedMessage;
                    RELOAD_MessageCode recMsgCode = reloadRcvMsg.reload_message_body.RELOAD_MsgCode;
                    if (recMsgCode == RELOAD_MessageCode.Fetch_Answer)
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD,
                          String.Format("{0} <== {1} TransId={2:x16}",
                            recMsgCode.ToString().PadRight(16, ' '), reloadRcvMsg.OriginatorID,
                              reloadRcvMsg.TransactionID));
                        FetchAns answ = (FetchAns)reloadRcvMsg.reload_message_body;

                        // TODO: For now add certificate to global PKC Store, but they are only temporarilly needed in validateDataSignature
                        m_ReloadConfig.AccessController.SetPKCs(reloadRcvMsg.security_block.Certificates);

                        if (answ != null)
                        {
                            fetchKindResponses = answ.KindResponses;
                            foreach (FetchKindResponse kind in fetchKindResponses)
                            {
                                foreach (StoredData sd in kind.values)
                                {
                                    if (m_ReloadConfig.AccessController.validateDataSignature(res_id, kind.kind, sd))
                                        recUsages.Add(sd.Value.GetUsageValue);
                                    else
                                    {
                                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "DATA SIGNATURE INVALID!!");
                                    }
                                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO,
                                      String.Format("Fetch successful, got {0}",
                                        sd.Value.GetUsageValue.Report()));
                                }
                            }
                            OnFetchedData(res_id, fetchKindResponses);
                            if (fetchDone != null)
                            {
                                if (recUsages.Count == 0)
                                {
                                    foreach (StoredDataSpecifier specifier in specifiers)
                                        recUsages.Add(new NoResultUsage(specifier.ResourceName));
                                }
                                fetchDone.Post(recUsages);
                            }
                        }
                    }
                }
                else
                {
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING, String.Format("Fetch failed"));
                    foreach (StoredDataSpecifier specifier in specifiers)
                        recUsages.Add(new NoResultUsage(specifier.ResourceName));
                    fetchDone.Post(recUsages);
                    m_statistics.IncTransmissionError();
                }
            }
            catch (Exception ex)
            {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Fetch: " + ex.Message);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Store Replicas: My next two successors have changed. Replicate my data
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public IEnumerator<ITask> StoreReplicas(NodeId node)
        {
            // For each Resource stored at this Peer, handover StoredData
            List<string> storedKeys;
            if ((storedKeys = m_topology.Storage.StoredKeys) != null && storedKeys.Count > 0)
            {

                m_topology.Storage.RemoveExpired();

                Dictionary<ResourceId, List<StoreKindData>> nodes = new Dictionary<ResourceId, List<StoreKindData>>();

                foreach (string key in storedKeys)
                {
                    ResourceId res_id = new ResourceId(ReloadGlobals.HexToBytes(key));

                    if (!m_topology.Replicas.Contains(key))
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "Store Replicas - will send store requests");
                        if (!nodes.ContainsKey(res_id))
                        {
                            nodes.Add(res_id, new List<StoreKindData>());
                            nodes[res_id].AddRange(m_topology.Storage.GetStoreKindData(key));
                        }
                        else
                        {
                            nodes[res_id].AddRange(m_topology.Storage.GetStoreKindData(key));
                        }
                    }
                }

                ReloadDialog reloadDialog = null;
                ReloadMessage reloadSendMsg;
                List<StoreKindData> storeKindData;

                foreach (ResourceId res_id in nodes.Keys)
                {
                    storeKindData = nodes[res_id];

                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "GOING TO REPLICATE UNDER RES_ID: " + res_id + " AT NODE: " + node);

                    List<SignerIdentity> signers = new List<SignerIdentity>();

                    foreach (StoreKindData skd in storeKindData)
                    {
                        foreach (StoredData sd in skd.Values)
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "STOREDATA: " + sd.Value.GetUsageValue.Report());

                            // add certificate
                            if (!signers.Contains(sd.Signature.Identity))
                                signers.Add(sd.Signature.Identity);
                        }
                    }

                    reloadSendMsg = create_store_req(new Destination(node), res_id, storeKindData, true);

                    // get certificates for this data
                    List<GenericCertificate> certs = new List<GenericCertificate>();
                    certs.AddRange(m_ReloadConfig.AccessController.GetPKCs(signers));

                    // add certificates to fetch answer
                    reloadSendMsg.security_block.Certificates.AddRange(certs);

                    int RetransmissionTime = ReloadGlobals.RetransmissionTime + ReloadGlobals.MaxTimeToSendPacket;

                    int iRetrans = ReloadGlobals.MaxRetransmissions;

                    while (iRetrans >= 0 && m_ReloadConfig.State < ReloadConfig.RELOAD_State.Exit)
                    {
                        try
                        {
                            reloadDialog = new ReloadDialog(m_ReloadConfig, m_flm, m_topology.routing_table.FindNextHopTo(node, false, false));

                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD, String.Format("{0} ==> {1} TransId={2:x16}",
                                reloadSendMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '), node, reloadSendMsg.TransactionID));

                            Arbiter.Activate(m_DispatcherQueue, new IterativeTask<ReloadMessage, ReloadMessageFilter, int>(reloadSendMsg, new ReloadMessageFilter(reloadSendMsg.TransactionID), RetransmissionTime, reloadDialog.Execute));
                        }
                        catch (Exception ex)
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store: " + ex.Message);
                        }

                        yield return Arbiter.Receive(false, reloadDialog.Done, done => { });

                        if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                            break;


                        /* If a response has not been received when the timer fires, the request
                           is retransmitted with the same transaction identifier. 
                        */
                        --iRetrans;
                    }

                    try
                    {
                        if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                        {
                            ReloadMessage reloadRcvMsg = reloadDialog.ReceivedMessage;

                            if (reloadRcvMsg.reload_message_body.RELOAD_MsgCode == RELOAD_MessageCode.Store_Answer)
                            {
                                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD, String.Format("{0} <== {1} TransId={2:x16}", reloadRcvMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '), reloadRcvMsg.OriginatorID, reloadRcvMsg.TransactionID));

                                //StoreReqAns answ = (StoreReqAns)reloadRcvMsg.reload_message_body; --old
                                StoreAns answ = (StoreAns)reloadRcvMsg.reload_message_body; // --alex

                                if (answ != null)
                                {
                                }
                            }
                        }
                        else
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING, String.Format("Store Replica failed"));
                            m_statistics.IncTransmissionError();
                        }
                    }
                    catch (Exception ex)
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store Replica: " + ex.Message);
                    }
                }
            }
        }
Esempio n. 8
0
        // --josch

        /// <summary>
        /// Proprietary: Stores the Usage data in a different RELOAD overlay using viaGateWay as gateway
        /// </summary>
        /// <param name="ResourceName"></param>
        /// <param name="kind_data"></param>
        /// <param name="viaGateWay"></param>
        /// <returns></returns>
        public IEnumerator<ITask> Store(string ResourceName, List<StoreKindData> kind_data, NodeId viaGateWay)
        {
            if (m_ReloadConfig.IamClient)
                m_ReloadConfig.StartStoreMobile = DateTime2.Now;
            else
                m_ReloadConfig.StartStore = DateTime.Now;

            ReloadDialog reloadDialog = null;
            ReloadMessage reloadSendMsg;
            ResourceId res_id = new ResourceId(ResourceName);
            //List<StoreKindData> kind_data = new List<StoreKindData>();


            Node node = null;

            if (viaGateWay != null)
            {
                //NodeId gateway = new ResourceId(viaGateWay);

                node = m_topology.routing_table.FindNextHopTo(viaGateWay, true, false);

                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_USAGE, String.Format("Store {0} as ResID: {1} via Gateway {2}", ResourceName, res_id, viaGateWay));

                if (m_ReloadConfig.IamClient && node == null)
                {
                    node = m_ReloadConfig.AdmittingPeer;
                }

                foreach (StoreKindData storeKindData in kind_data)
                {
                    if (node == null || node.Id == m_ReloadConfig.LocalNodeID)
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, String.Format("Local storage at NodeId {0}", node.Id));
                        m_topology.Store(res_id, storeKindData);
                        yield break;
                    }
                }

                Destination gateway = new Destination(new NodeId(viaGateWay.Data));
                Destination storeDestination = new Destination(res_id);
                StoreReq storeRequest = new StoreReq(storeDestination.destination_data.ressource_id,
                                                      kind_data,
                                                      m_machine.UsageManager, false);
                reloadSendMsg = create_reload_message(gateway, ++m_ReloadConfig.TransactionID, storeRequest);
                reloadSendMsg.forwarding_header.destination_list.Add(storeDestination);  //this is the real destination

                if (reloadSendMsg.AddDestinationOverlay(ResourceName))
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "AddDestinationOverlay successful");
            }
            else
            {
                res_id = new ResourceId(ResourceName);

                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_USAGE, String.Format("Store {0} as ResID: {1}", ResourceName, res_id));
                node = m_topology.routing_table.FindNextHopTo(new NodeId(res_id), true, false);

                if (m_ReloadConfig.IamClient && node == null)
                {
                    node = m_ReloadConfig.AdmittingPeer;
                }
                if (node == null || node.Id == m_ReloadConfig.LocalNodeID)
                {
                    foreach (StoreKindData storeKindData in kind_data)
                    {

                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO,
                          String.Format("Local storage at NodeId {0}", node.Id));
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_MEASURE,
                          "Store:0,011111");
                        m_topology.Store(res_id, storeKindData);
                    }
                    if (storeDone != null) storeDone.Post(reloadDialog);
                    yield break;
                }
                reloadSendMsg = create_store_req(new Destination(res_id), kind_data, false);
            }

            int RetransmissionTime = ReloadGlobals.RetransmissionTime + ReloadGlobals.MaxTimeToSendPacket;

            int iRetrans = ReloadGlobals.MaxRetransmissions;

            while (iRetrans >= 0 && m_ReloadConfig.State < ReloadConfig.RELOAD_State.Exit)
            {
                try
                {
                    reloadDialog = new ReloadDialog(m_ReloadConfig, m_flm, node);

                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD, String.Format("{0} ==> {1} TransId={2:x16}",
                        reloadSendMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '), node.Id, reloadSendMsg.TransactionID));

                    Arbiter.Activate(m_DispatcherQueue,
                        new IterativeTask<ReloadMessage, ReloadMessageFilter, int>(reloadSendMsg,
                            new ReloadMessageFilter(reloadSendMsg.TransactionID), RetransmissionTime, reloadDialog.Execute));
                }
                catch (Exception ex)
                {
                    storeDone.Post(reloadDialog);
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store: " + ex.Message);
                    ReloadGlobals.PrintException(m_ReloadConfig, ex);
                }

                yield return Arbiter.Receive(false, reloadDialog.Done, done => { });

                if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                    break;


                /* If a response has not been received when the timer fires, the request
                   is retransmitted with the same transaction identifier. 
                */
                --iRetrans;
            }

            try
            {
                if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                {
                    //the SourceNodeID delivered from reloadDialog comes from connection table and is the last hop of the message
                    ReloadMessage reloadRcvMsg = reloadDialog.ReceivedMessage;

                    if (reloadRcvMsg.reload_message_body.RELOAD_MsgCode == RELOAD_MessageCode.Store_Answer)
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_USAGE,
                          String.Format("Store successful:{0} <== {1} TransId={2:x16}",
                          reloadRcvMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '),
                          reloadRcvMsg.OriginatorID, reloadRcvMsg.TransactionID));

                        m_ReloadConfig.EndStore = DateTime.Now;

                        TimeSpan storeSpan = storeSpan = m_ReloadConfig.EndStore - m_ReloadConfig.StartStore;

                        if (m_ReloadConfig.IamClient)
                            storeSpan = DateTime2.Now - m_ReloadConfig.StartStoreMobile;

                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_MEASURE,
                                              String.Format("Store:{0}",
                                              storeSpan.TotalSeconds.ToString()));

                        if (storeDone != null) storeDone.Post(reloadDialog);
                    }
                }
                else
                {
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING,
                      String.Format("Store failed"));
                    m_statistics.IncTransmissionError();
                }
            }
            catch (Exception ex)
            {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store 2: " + ex.Message);
                ReloadGlobals.PrintException(m_ReloadConfig, ex);
            }

            if (m_ReloadConfig.State == ReloadConfig.RELOAD_State.Shutdown)
                m_machine.Finish();

        }
Esempio n. 9
0
 /// <summary>
 /// Computes the signature of the stored data.
 /// The input to the signature algorithm is:
 ///
 /// resource_id || kind || storage_time || StoredDataValue ||
 /// SignerIdentity
 /// Where || indicates concatenation.
 /// </summary>
 /// <param name="resId"></param>
 /// <param name="kindId"></param>
 public void SignData(ResourceId resId, UInt32 kindId, SignerIdentity id,
   ReloadConfig rc) {
     signature = new Signature(resId, kindId, storage_time,
       value, id, rc);
 }
Esempio n. 10
0
    private void processFetchResult(ResourceId resid, List<RedirServiceProviderData> resultList) {
      machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: fetchResult resid=" + resid);
      List<NodeId> ServiceProviderIDs = new List<NodeId>();
      string nameSpace = null;
      if (resultList != null) {
        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "fetchResult resultList.Count=" + resultList.Count);
        foreach (RedirServiceProviderData result in resultList) {
          ServiceProviderIDs.Add(result.serviceProvider);
          if (nameSpace != null && nameSpace != result.nameSpace)
            throw new System.Exception(String.Format("Invalid ReDiR Fetch: different Namespaces within one fetch"));
          else
            nameSpace = result.nameSpace;
        }
      }

      if (status == STATE.LOOKUP) {
        if (ServiceProviderIDs.Count == 0) {
          machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: No Successor found at " + resid);
          if (m_currentLevel == 0) {
            machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: FAILED " + resid);
            deliverResult(resid, null, null);
            status = STATE.IDLE;
            return;
          }
          else {
            m_currentLevel = m_currentLevel - 1;
            fetch(m_currentLevel);
            return;
          }
        }
        else if (ServiceProviderIDs.Count == 1) {
          NodeId id = ServiceProviderIDs[0];
          machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: Successor found: NodeId=" + id + " at Level = " + m_currentLevel);
          deliverResult(resid, nameSpace, id);
          status = STATE.IDLE;
          return;
        }
        else
          ServiceProviderIDs.Sort();

        if (ServiceProviderIDs.Count > 1 && ServiceProviderIDs.Last() > m_key && ServiceProviderIDs.First() < m_key) {

          machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: sandwiched in I(" + m_currentLevel + "," + m_key.ToString() + ")");
          m_currentLevel = m_currentLevel + 1;
          fetch(m_currentLevel);
          return;
        }
        else if (ServiceProviderIDs.Last() == m_key || ServiceProviderIDs.First() == m_key) {
          NodeId id = ServiceProviderIDs[0];
          machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: Successor found (its me!!!!): NodeId=" + id + " at Level = " + m_currentLevel);
          deliverResult(resid, nameSpace, id);
          status = STATE.IDLE;
          return;
        }
        else {
          NodeId id = ServiceProviderIDs[0];
          machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR: Successor found NodeId=" + id + " at Level = " + m_currentLevel);
          deliverResult(resid, nameSpace, id);

        }


        //if (ServiceProviderIDs.Count > 0 && m_currentLevel > 0) {
        //  ServiceProviderIDs.Sort();

        //  if (ServiceProviderIDs.Last() < m_key) { // successor of node n is not present in the tree node associated with I(level,n.id) ??
        //    machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "successor of node n is not present in the tree node at I(" + m_currentLevel + "," + m_key.ToString() + ")");
        //    m_currentLevel = m_currentLevel - 1;
        //    fetch(m_currentLevel);
        //  }
        //  else if (ServiceProviderIDs.Count > 1 && ServiceProviderIDs.Last() >= m_key && ServiceProviderIDs.First() <= m_key) {
        //    machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "sandwiched in I(" + m_currentLevel + "," + m_key.ToString() + ")");
        //    m_currentLevel = m_currentLevel + 1;
        //    fetch(m_currentLevel);
        //  }
        //  else {
        //    foreach (NodeId id in ServiceProviderIDs) {
        //      machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR " + id);
        //      if (id >= m_key) {
        //        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR closest successor with NodeID=" + id + " found at Level=" + m_currentLevel);
        //        deliverResult(resid, nameSpace, id);
        //        status = STATE.IDLE;
        //        return;
        //      }
        //    }
        //  }
        //}
        //else if (m_currentLevel == 0) {
        //  if (ServiceProviderIDs.Count > 0) {
        //    ServiceProviderIDs.Sort();
        //    foreach (NodeId id in ServiceProviderIDs) {
        //      machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR" + id);
        //      if (id >= m_key) {
        //        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR closest successor with NodeID=" + id + " found at Level=" + m_currentLevel);
        //        deliverResult(resid, nameSpace, id);
        //        status = STATE.IDLE;
        //        return;
        //      }
        //    }
        //    machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR closest successor with NodeID=" + ServiceProviderIDs.First().ToString() + " found at Level=" + m_currentLevel);
        //    deliverResult(resid, nameSpace, ServiceProviderIDs.First());
        //    status = STATE.IDLE;
        //    return;
        //  }
        //  else {
        //    machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "REDIR Nothin");
        //    deliverResult(resid, null, null);
        //    status = STATE.IDLE;
        //    return;
        //  }
        //}
        //else {
        //  m_currentLevel = m_currentLevel - 1;
        //  //deliverResult(resid, null, null);
        //  fetch(m_currentLevel);
        //}

      }
      else if (status != STATE.JOINED) {
        if (status == STATE.UPWALK) {
          ServiceProviderIDs.Sort();

          store(m_currentLevel);

          //check if ServiceProviderID (my ID) is the numerically lowest of highest among the Node-IDs stored under res_id
          if (ServiceProviderIDs.Count == 0 || ServiceProviderIDs.Last() < m_key || ServiceProviderIDs.First() > m_key) {
            if (m_currentLevel > 0) {
              m_currentLevel = m_currentLevel - 1;
              fetch(m_currentLevel);
            }
            else {
              status = STATE.DOWNWALK;
              m_currentLevel = Lstart;
              fetch(m_currentLevel);
            }
          }
          else {
            status = STATE.DOWNWALK;
            m_currentLevel = Lstart;
            fetch(m_currentLevel);
          }
        }
        else if (status == STATE.DOWNWALK) {
          ServiceProviderIDs.Sort();

          if (ServiceProviderIDs.Count == 1 && ServiceProviderIDs[0] == m_key) {
            status = STATE.JOINED;
            machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, String.Format("JOINED REDIR"));
            if (refreshTimer == null)
              refreshTimer = new Timer(refresh_callback, null, 60000, 60000);
            else
              refreshTimer.Change(60000, 60000);
            return;
          }

          if (ServiceProviderIDs.Count == 0) {
            store(m_currentLevel);
            status = STATE.JOINED;
            machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, String.Format("JOINED REDIR"));
            if (refreshTimer == null)
              refreshTimer = new Timer(refresh_callback, null, 60000, 60000);
            else
              refreshTimer.Change(60000, 60000);
            return;
          }
          //check if ServiceProviderID (my ID) is the numerically lowest of highest among the Node-IDs stored under res_id
          if (ServiceProviderIDs.Last() < m_key || ServiceProviderIDs.First() > m_key) {
            // if so: store yourself under res_id
            store(m_currentLevel);
          }
          // and probe the next level
          m_currentLevel = m_currentLevel + 1;
          fetch(m_currentLevel);
        }
      }
    }
Esempio n. 11
0
    private void deliverResult(ResourceId resid, string nameSpace, NodeId id) {      

      if (nameSpace == null) {
        if (ReDiRLookupFailed != null)
          ReDiRLookupFailed(m_nameSpace);
        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "ReDiR: No Result for " + resid);
      }
      else {
        if ( ReDiRLookupCompleted != null)
          ReDiRLookupCompleted(nameSpace, id);
        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "ReDiR: Result for " + resid + " : nameSpace=" + nameSpace + "ID=" + id.ToString());
      }

    }
Esempio n. 12
0
    /// <summary>
    /// This method validates if an ACP applies.
    /// </summary>
    /// <param name="resId">The requested resId</param>
    /// <param name="resId">The KindId</param>
    /// <param name="data">The StoredData</param>
    /// <returns>True, if ACP applies</returns>
    public Boolean ValuePermitted(ResourceId resId, UInt32 kindId, StoredData data) {
      //var nodeid = ReloadGlobals.retrieveNodeIDfromCertificate( signerCert, ref rfc822Name);


      return true;

    }
Esempio n. 13
0
    public bool validateDataSignature(ResourceId resId, uint kind, StoredData sd) {
      //FetchAns fetch_answer = (FetchAns)(reloadMsg.reload_message_body);

      var ascii = new ASCIIEncoding();
      /* Set alogorithm and identity */
      SignatureAndHashAlgorithm algorithm = new SignatureAndHashAlgorithm(HashAlgorithm.sha256, ReloadGlobals.SignatureAlg);
      /* Covert Idenity to string */
      String identity = sd.Signature.Identity.ToString();
      /* Get string of stored data value */
      var ms = new MemoryStream();
      var bw = new BinaryWriter(ms);
      sd.Value.Dump(bw);
      sd.Value.GetUsageValue.dump(bw);
      ms.Position = 0;
      var sr = new StreamReader(ms);
      string strValue = sr.ReadToEnd();
      sr.Close();
      bw.Close();
      /* Concatenate signature input */
      String strSignaturInput = String.Format("{0}{1}{2}{3}{4}",
        ascii.GetString(resId.Data, 0, resId.Data.Length), kind, sd.StoreageTime,
        strValue, identity);

      byte[] signatureInput = ascii.GetBytes(strSignaturInput);
      byte[] sigVal = sd.Signature.SignaureValue;

      GenericCertificate gencert = GetPKC(sd.Signature.Identity);
      byte[] bcert = gencert.Certificate; //TODO: TEST
      X509Certificate2 signerCert = new X509Certificate2(bcert);

      if (!Utils.X509Utils.VerifyCertificate(signerCert, m_ReloadConfig.RootCertificate))
      {
        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR,
          String.Format("validateDataSignatures: NodeID {0}, Certificate" +
          "validation failed (CA Issuer {1})",
          null, signerCert.Issuer));
        //return false;
      }
      var cert = new X509Certificate2(bcert);

      switch (sd.Signature.Algorithm.signature) {
        case SignatureAlgorithm.rsa:
          var cryptoIPT = (RSACryptoServiceProvider)cert.PublicKey.Key;

          switch (sd.Signature.Algorithm.hash) {
            case HashAlgorithm.sha256:
              var sha256 = new SHA256CryptoServiceProvider();

              if (!cryptoIPT.VerifyData(signatureInput, sha256, sigVal)) {
                throw new InvalidOperationException("Invalid signature");
                return false;
              }
              else {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_FORWARDING, "DATA SIGNATURE VALID!!");
                return true;
              }
              break;
            default:
              throw new NotImplementedException("AccessController:" +
                "hash algoritm not implemented!");
          }

          break;
        case SignatureAlgorithm.dsa:
          throw new NotImplementedException("AccessController:" +
            "DSA encryption not implemented!");
        default:
          throw new NotImplementedException("AccessController:" +
            "encryption not implemented!");
      }
    }
Esempio n. 14
0
        /// <summary>
        /// This method maintains data storage of incomming store requests.
        /// </summary>
        /// <param name="resource_id"></param>
        /// <param name="kind_data"></param>        
        public void Store(ResourceId resource_id, StoreKindData kind_data) {

            storage.Store(resource_id, kind_data);
        }
Esempio n. 15
0
    private bool redir_FetchCompleted(List<IUsage> usages) {
      ResourceId resid = null;
      //machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "redir_FetchCompleted usages.Count=" + usages.Count);
      if (usages.Count != 0) {
        List<RedirServiceProviderData> providerList = new List<RedirServiceProviderData>();
        foreach (var usage in usages) {
          if (usage is NoResultUsage) {
            machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "redir_FetchCompleted: NoResultUsage");
            resid = new ResourceId(usages[0].ResourceName);
            //processFetchResult(resid, null);
            break;
          }
          else {
            machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "redir_FetchCompleted: " + usage.Report());
            if (usage.CodePoint == Usage_Code_Point.REDIR_SERVICE_PROVIDER) {
              providerList.Add(((RedirServiceProvider)usage).Data);
              resid = new ResourceId(usages[0].ResourceName); //TODO: cleanup
              if (resid != new ResourceId(usage.ResourceName)) {
                throw new System.Exception(String.Format("Invalid ReDiR Result: Different ResourceIds for one fetch"));
              }
            }
          }
        }
        processFetchResult(resid, providerList);
      }
      else {
        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, "redir_FetchCompleted: usages.Count==0");
        resid = new ResourceId(usages[0].ResourceName);
        processFetchResult(resid, null);
      }


      if (status == STATE.IDLE || status == STATE.JOINED) {
        machine.FetchCompleted -= new DFetchCompleted(redir_FetchCompleted);
        machine.ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_REDIR, String.Format("machine.FetchCompleted -= new DFetchCompleted(redir_FetchCompleted);"));
      }
      return true;
    }
Esempio n. 16
0
        public bool Fetch(ResourceId res_id, StoredDataSpecifier specifier, out FetchKindResponse kindResponse) {

            FetchKindResponse result = null;
            if (storage.Fetch(res_id, specifier, out result)) {
                kindResponse = result;
                return true;
            }
            kindResponse = new FetchKindResponse();
            return false;

        }
Esempio n. 17
0
        public StoreReq(ResourceId resId, List<StoreKindData> store_kind_data,
          UsageManager manager, bool replica)
        {
            resourceId = resId;
            this.RELOAD_MsgCode = RELOAD_MessageCode.Store_Request;
            if (store_kind_data != null && store_kind_data.Count != 0)
            {
                this.store_kind_data = store_kind_data;
            }
            myManager = manager;

            if (replica)
                setReplicaNumber();

        }
Esempio n. 18
0
        /// <summary>
        /// Stores the Usage data in the RELOAD overlay
        /// </summary>
        /// <param name="ResourceName"></param>
        /// <param name="DestUrl"></param>
        /// <param name="exists">if true, stores the values, else stores a "non-existent" value.</param>
        /// <param name="usages">The Usage data to be stored.</param>
        /// <returns></returns>        
        public IEnumerator<ITask> Store(string ResourceName, List<StoreKindData> kind_data)
        {
            if (m_ReloadConfig.IamClient)
                m_ReloadConfig.StartStoreMobile = DateTime2.Now;
            else
                m_ReloadConfig.StartStore = DateTime.Now;

            ReloadDialog reloadDialog = null;
            ReloadMessage reloadSendMsg;
            ResourceId res_id = new ResourceId(ResourceName);

            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_USAGE, String.Format("Store {0} as ResID: {1}", ResourceName, res_id));
            Node node = m_topology.routing_table.FindNextHopTo(new NodeId(res_id), true, false);

            if (m_ReloadConfig.IamClient && node == null)
            {
                node = m_ReloadConfig.AdmittingPeer;
            }
            if (node == null || node.Id == m_ReloadConfig.LocalNodeID)
            {
                foreach (StoreKindData storeKindData in kind_data)
                {

                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO,
                      String.Format("Local storage at NodeId {0}", node.Id));
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_MEASURE,
                      "Store:0,011111");
                    m_topology.Store(res_id, storeKindData);
                }

                // REPLICATEST
                // incoming store request is not a replication request
                int numberReplicas = m_topology.routing_table.Successors.Count >= 2 ? 2 : m_topology.routing_table.Successors.Count; // Replica number is max 2
                // send replica to all successors
                for (int i = 0; i < numberReplicas; i++)
                {
                    NodeId successorNode = m_topology.routing_table.Successors[i];
                    ReloadMessage replica = create_store_req(new Destination(successorNode), res_id, kind_data, true);
                    send(replica, m_topology.routing_table.GetNode(successorNode));
                }

                if (storeDone != null) storeDone.Post(reloadDialog);
                yield break;
            }

            Destination dest = new Destination(res_id);

            reloadSendMsg = create_store_req(dest, kind_data, false);

            int RetransmissionTime = ReloadGlobals.RetransmissionTime + ReloadGlobals.MaxTimeToSendPacket;

            int iRetrans = ReloadGlobals.MaxRetransmissions;

            while (iRetrans >= 0 && m_ReloadConfig.State < ReloadConfig.RELOAD_State.Exit)
            {
                try
                {
                    reloadDialog = new ReloadDialog(m_ReloadConfig, m_flm, node);

                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD, String.Format("{0} ==> {1} TransId={2:x16}",
                        reloadSendMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '), node.Id, reloadSendMsg.TransactionID));

                    Arbiter.Activate(m_DispatcherQueue,
                        new IterativeTask<ReloadMessage, ReloadMessageFilter, int>(reloadSendMsg,
                            new ReloadMessageFilter(reloadSendMsg.TransactionID), RetransmissionTime, reloadDialog.Execute));
                }
                catch (Exception ex)
                {
                    storeDone.Post(reloadDialog);
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store: " + ex.Message);
                    ReloadGlobals.PrintException(m_ReloadConfig, ex);
                    break;
                }

                yield return Arbiter.Receive(false, reloadDialog.Done, done => { });

                //if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null && reloadDialog.ReceivedMessage.reload_message_body.RELOAD_MsgCode == RELOAD_MessageCode.Store_Answer)
                //    break;

                if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null && reloadDialog.ReceivedMessage.reload_message_body.RELOAD_MsgCode == RELOAD_MessageCode.Store_Answer)
                {

                    ReloadMessage reloadRcvMsg = reloadDialog.ReceivedMessage;

                    if (dest.type == DestinationType.node)
                    {
                        if (reloadRcvMsg.OriginatorID != dest.destination_data.node_id)
                        {
                            // drop message and retransmit request
                            reloadRcvMsg = null;
                        }
                    }
                    else if (dest.type == DestinationType.resource)
                    {
                        int suc = m_topology.routing_table.GetSuccessorCount(false);
                        int pre = m_topology.routing_table.GetPredecessorCount(false);

                        if (suc >= 2 && pre >= 2)
                        {
                            // check if resource is mapping to a node in my routing table
                            if (dest.destination_data.ressource_id.ElementOfInterval(
                                m_topology.routing_table.Predecessors[pre - 2],
                                m_topology.routing_table.Successors[suc - 1],
                                true)
                            )
                            {
                                if (reloadRcvMsg.OriginatorID < m_topology.routing_table.Predecessors[pre - 2] && reloadRcvMsg.OriginatorID > m_topology.routing_table.Successors[suc - 1])
                                {
                                    // drop message and retransmit request
                                    reloadRcvMsg = null;
                                }
                            }
                        }
                    }
                    if (reloadRcvMsg != null)
                        break;
                }


                /* If a response has not been received when the timer fires, the request
                   is retransmitted with the same transaction identifier. 
                */
                --iRetrans;
            }

            try
            {
                if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                {
                    //the SourceNodeID delivered from reloadDialog comes from connection table and is the last hop of the message
                    ReloadMessage reloadRcvMsg = reloadDialog.ReceivedMessage;

                    if (reloadRcvMsg.reload_message_body.RELOAD_MsgCode == RELOAD_MessageCode.Store_Answer)
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_USAGE,
                          String.Format("Store successful:{0} <== {1} TransId={2:x16}",
                          reloadRcvMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '),
                          reloadRcvMsg.OriginatorID, reloadRcvMsg.TransactionID));

                        m_ReloadConfig.EndStore = DateTime.Now;

                        TimeSpan storeSpan = storeSpan = m_ReloadConfig.EndStore - m_ReloadConfig.StartStore;

                        if (m_ReloadConfig.IamClient)
                            storeSpan = DateTime2.Now - m_ReloadConfig.StartStoreMobile;

                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_MEASURE,
                                              String.Format("Store:{0}",
                                              storeSpan.TotalSeconds.ToString()));

                        if (storeDone != null) storeDone.Post(reloadDialog);
                    }
                }
                else
                {
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING,
                      String.Format("Store failed"));
                    m_statistics.IncTransmissionError();
                }
            }
            catch (Exception ex)
            {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store 2: " + ex.Message);
                ReloadGlobals.PrintException(m_ReloadConfig, ex);
            }

            if (m_ReloadConfig.State == ReloadConfig.RELOAD_State.Shutdown)
                m_machine.Finish();

        }
Esempio n. 19
0
        /// <summary>
        /// Deserializes a StoreReq message from wire.
        /// </summary>
        /// <param name="rm"></param>
        /// <param name="reader"></param>
        /// <param name="reload_msg_size"></param>
        /// <returns></returns>
        public override RELOAD_MessageBody FromReader(ReloadMessage rm, BinaryReader reader, long reload_msg_size)
        {

            UInt32 message_len = 0;
            /* try to read the packet as a StoreReq packet */
            try
            {
                long posBeforeMsg = reader.BaseStream.Position;
                RELOAD_MsgCode = (RELOAD_MessageCode)(UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16());
                message_len = (UInt32)(IPAddress.HostToNetworkOrder((int)reader.ReadInt32()));

                Byte res_id_length = reader.ReadByte();
                if (res_id_length == 0)
                    throw new System.Exception("Resource ID length == 0!");
                resourceId = new ResourceId(reader.ReadBytes(res_id_length));
                replica_number = reader.ReadByte();

                long posBeforeRead = reader.BaseStream.Position;
                UInt32 kindDataLen = (UInt32)(IPAddress.NetworkToHostOrder(reader.ReadInt32()));
                /* StoreKindData Receive loop */
                while (StreamUtil.ReadBytes(posBeforeRead, reader) < kindDataLen)
                {

                    UInt32 kindId = (UInt32)(IPAddress.HostToNetworkOrder(reader.ReadInt32()));
                    UInt64 generation = (UInt64)(IPAddress.NetworkToHostOrder(reader.ReadInt64()));

                    var store_kind_data = new StoreKindData(kindId, generation);

                    long posBeforeSD = reader.BaseStream.Position;
                    UInt32 storedDataLen = (UInt32)(IPAddress.HostToNetworkOrder(reader.ReadInt32()));

                    if (RELOAD_MsgCode == RELOAD_MessageCode.Store_Request)
                    {
                        while (StreamUtil.ReadBytes(posBeforeSD, reader) < storedDataLen)
                        {
                            /* reading properties of StoredData struct */
                            UInt32 stored_data_lenght = (UInt32)(IPAddress.NetworkToHostOrder(reader.ReadInt32()));
                            UInt64 storage_time = (UInt64)(IPAddress.NetworkToHostOrder(reader.ReadInt64()));
                            UInt32 lifetime = (UInt32)(IPAddress.NetworkToHostOrder(reader.ReadInt32()));

                            ReloadGlobals.DataModel data_model = myManager.GetDataModelfromKindId(store_kind_data.Kind);

                            Boolean exists;
                            IUsage usage;
                            StoredDataValue stored_data_value;

                            switch (data_model)
                            {
                                case ReloadGlobals.DataModel.SINGLE_VALUE:
                                    throw new NotImplementedException("There is no Usage with Single Value atm");

                                case ReloadGlobals.DataModel.ARRAY:
                                    UInt32 index = (UInt32)(IPAddress.NetworkToHostOrder((int)reader.ReadInt32()));
                                    exists = (reader.ReadByte() == 0x00 ? false : true);
                                    usage = myManager.GetUsageFromReader(rm, reader, reload_msg_size, store_kind_data.Kind);

                                    stored_data_value = new StoredDataValue(index, usage, exists);
                                    break;

                                case ReloadGlobals.DataModel.DICTIONARY:
                                    UInt16 keyLength = (UInt16)(IPAddress.NetworkToHostOrder((short)reader.ReadInt16()));
                                    string key = BitConverter.ToString(reader.ReadBytes(keyLength), 0, keyLength);  //key is a hex string
                                    key = key.Replace("-", "");
                                    exists = (reader.ReadByte() == 0x00 ? false : true);
                                    usage = myManager.GetUsageFromReader(rm, reader, reload_msg_size, store_kind_data.Kind);

                                    stored_data_value = new StoredDataValue(key, usage, exists);
                                    break;

                                default:
                                    throw new NotSupportedException(String.Format("The data_model {0} is not supported", data_model));
                            }
                            StoredData stored_data = new StoredData(storage_time, lifetime, stored_data_value);
                            stored_data.Signature = new Signature(myManager.m_ReloadConfig).FromReader(reader, reload_msg_size);
                            // TODO Process signature
                            store_kind_data.Add(stored_data);
                            appendStoreKindData(store_kind_data);
                        }
                    }
                }
                UInt32 totalRead = StreamUtil.ReadBytes(posBeforeMsg, reader);
                reload_msg_size = reload_msg_size - (totalRead + 1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return this;
        }
Esempio n. 20
0
        // REPLICATEST

        public void EvaluateReplicas()
        {
            List<String> ReplicasToRemove = new List<string>();

            foreach (String rep in m_topology.Replicas)
            {
                ResourceId id = new ResourceId(rep);

                if (m_topology.routing_table.Predecessors.Count > 0)
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ALL, String.Format("EvaluateReplicas: Is data {0} in interval {1} - {2}", rep, m_topology.routing_table.Predecessors[0], m_topology.LocalNode.Id));
                else
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ALL, String.Format("Nobody is left. All the data belong to me"));

                if (m_topology.routing_table.Predecessors.Count > 0 && id.ElementOfInterval(m_topology.routing_table.Predecessors[0], m_topology.LocalNode.Id, false))
                {
                    ReplicasToRemove.Add(rep);
                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ALL, String.Format("EvaluateReplicas: I'm now responsible for data {0}", rep));

                    if (m_topology.routing_table.Successors.Count > 0)
                        Arbiter.Activate(m_ReloadConfig.DispatcherQueue, new IterativeTask<NodeId>(m_topology.routing_table.Successors[0], StoreReplicas));
                    if (m_topology.routing_table.Successors.Count > 1)
                        Arbiter.Activate(m_ReloadConfig.DispatcherQueue, new IterativeTask<NodeId>(m_topology.routing_table.Successors[1], StoreReplicas));
                }
            }

            foreach (String rep in ReplicasToRemove)
            {
                m_topology.Replicas.Remove(rep);
            }
        }
Esempio n. 21
0
 public Destination(ResourceId id)
 {
     type = DestinationType.resource;
     destination_data.ressource_id = id;
 }
Esempio n. 22
0
        /// <summary>
        /// Handover key if: 1. leave overlay 2. I'm AP while a join req happens.
        /// </summary>
        /// <param name="fSendLeaveFirst"></param>
        /// <returns></returns>
        public IEnumerator<ITask> HandoverKeys(bool fSendLeaveFirst)
        {
            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "Handover Keys!");

            if (fSendLeaveFirst)
            {
                yield return Arbiter.ExecuteToCompletion(m_DispatcherQueue, new IterativeTask(Leave));
            }

            // For each Resource stored at this Peer, handover StoredData
            List<string> storedKeys;
            if ((storedKeys = m_topology.Storage.StoredKeys) != null && storedKeys.Count > 0)
            {

                m_topology.Storage.RemoveExpired();

                Dictionary<ResourceId, List<StoreKindData>> nodes = new Dictionary<ResourceId, List<StoreKindData>>();

                Dictionary<ResourceId, Node> destinations = new Dictionary<ResourceId, Node>();

                foreach (string key in storedKeys)
                {
                    ResourceId res_id = new ResourceId(ReloadGlobals.HexToBytes(key));
                    Node currentNode = m_topology.routing_table.FindNextHopTo(new NodeId(res_id), true, fSendLeaveFirst);
                    if (currentNode == null || currentNode.Id == m_ReloadConfig.LocalNodeID)
                    {
                        //everything's fine, key still belongs to me
                        continue;
                    }
                    // REPLICATEST
                    // peer is no longer in the replica set for the resource
                    else if (m_topology.routing_table.Predecessors.Count > 2 && !res_id.ElementOfInterval(m_topology.routing_table.Predecessors[2], m_topology.LocalNode.Id, false))
                    {
                        m_topology.Storage.Remove(res_id.ToString());
                        m_topology.Replicas.Remove(res_id.ToString());
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ALL, String.Format("Data {0} no longer in range. Delete replica", res_id.ToString()));
                    }
                    else
                    {
                        if (!m_topology.Replicas.Contains(key))
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "Handover Keys - will send store requests");
                            if (!nodes.ContainsKey(res_id))
                            {
                                nodes.Add(res_id, new List<StoreKindData>());
                                nodes[res_id].AddRange(m_topology.Storage.GetStoreKindData(key));

                                destinations.Add(res_id, currentNode);
                            }
                            else
                            {
                                nodes[res_id].AddRange(m_topology.Storage.GetStoreKindData(key));
                            }
                        }
                    }
                }

                ReloadDialog reloadDialog = null;
                ReloadMessage reloadSendMsg;
                List<StoreKindData> storeKindData;

                foreach (ResourceId res_id in nodes.Keys)
                {
                    Node node = destinations[res_id];
                    storeKindData = nodes[res_id];

                    List<SignerIdentity> signers = new List<SignerIdentity>();

                    m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "GOING TO STORE UNDER RES_ID: " + res_id);

                    foreach (StoreKindData skd in storeKindData)
                    {
                        foreach (StoredData sd in skd.Values)
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_TOPO, "STOREDATA: " + sd.Value.GetUsageValue.Report());

                            // add certificate
                            if (!signers.Contains(sd.Signature.Identity))
                                signers.Add(sd.Signature.Identity);
                        }
                    }

                    if (m_machine.ReloadConfig.State == ReloadConfig.RELOAD_State.Leave)
                    {
                        node = m_topology.routing_table.GetSuccessor(0);
                        reloadSendMsg = create_store_req(new Destination(node.Id), res_id, storeKindData, false);
                    }
                    else
                    {
                        reloadSendMsg = create_store_req(new Destination(res_id), storeKindData, false);
                    }

                    // get certificates for this data
                    List<GenericCertificate> certs = new List<GenericCertificate>();
                    certs.AddRange(m_ReloadConfig.AccessController.GetPKCs(signers));

                    // add certificates to fetch answer
                    reloadSendMsg.security_block.Certificates.AddRange(certs);

                    int RetransmissionTime = ReloadGlobals.RetransmissionTime + ReloadGlobals.MaxTimeToSendPacket;

                    int iRetrans = ReloadGlobals.MaxRetransmissions;

                    while (iRetrans >= 0 && m_ReloadConfig.State < ReloadConfig.RELOAD_State.Exit)
                    {
                        try
                        {
                            reloadDialog = new ReloadDialog(m_ReloadConfig, m_flm, node);

                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD, String.Format("{0} ==> {1} TransId={2:x16}",
                                reloadSendMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '), node.Id, reloadSendMsg.TransactionID));

                            Arbiter.Activate(m_DispatcherQueue, new IterativeTask<ReloadMessage, ReloadMessageFilter, int>(reloadSendMsg, new ReloadMessageFilter(reloadSendMsg.TransactionID), RetransmissionTime, reloadDialog.Execute));
                        }
                        catch (Exception ex)
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store: " + ex.Message);
                        }

                        yield return Arbiter.Receive(false, reloadDialog.Done, done => { });

                        if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                            break;


                        /* If a response has not been received when the timer fires, the request
                           is retransmitted with the same transaction identifier. 
                        */
                        --iRetrans;
                    }

                    try
                    {
                        if (!reloadDialog.Error && reloadDialog.ReceivedMessage != null)
                        {
                            ReloadMessage reloadRcvMsg = reloadDialog.ReceivedMessage;

                            if (reloadRcvMsg.reload_message_body.RELOAD_MsgCode == RELOAD_MessageCode.Store_Answer)
                            {
                                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_RELOAD, String.Format("{0} <== {1} TransId={2:x16}", reloadRcvMsg.reload_message_body.RELOAD_MsgCode.ToString().PadRight(16, ' '), reloadRcvMsg.OriginatorID, reloadRcvMsg.TransactionID));

                                //StoreReqAns answ = (StoreReqAns)reloadRcvMsg.reload_message_body; --old
                                StoreAns answ = (StoreAns)reloadRcvMsg.reload_message_body; // --alex

                                if (answ != null)
                                {
                                    // m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING, String.Format("Delete Key {0}", res_id));
                                    // m_topology.StoredValues.Remove(StoredKey); --old

                                    // REPLICATEST
                                    // Keep stored data but mark it as replica
                                    if (!m_topology.Replicas.Contains(res_id.ToString()))
                                        m_topology.Replicas.Add(res_id.ToString());

                                    //m_topology.Storage.Remove(res_id.ToString());
                                }
                            }
                        }
                        else
                        {
                            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_WARNING, String.Format("Store failed"));
                            m_statistics.IncTransmissionError();
                        }
                    }
                    catch (Exception ex)
                    {
                        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR, "Send Store: " + ex.Message);
                    }
                }
            }

            // this code might be redundant to code in RoutingTable.Leave()

            //// check if there are replicas I should be responsible for
            //if (m_topology.routing_table.Predecessors.Count == 0)
            //{
            //    m_topology.Replicas.Clear();
            //}
            //else
            //{
            //    List<string> removeReplicas = new List<string>();
            //    foreach (string replica in m_topology.Replicas)
            //    {
            //        // Convert the Resource String in a ResourceId
            //        int NumberChars = replica.Length;
            //        byte[] bytes = new byte[NumberChars / 2];
            //        for (int i = 0; i < NumberChars; i += 2)
            //            bytes[i / 2] = Convert.ToByte(replica.Substring(i, 2), 16);
            //        ResourceId id = new ResourceId(bytes);

            //        if (id.ElementOfInterval(m_topology.routing_table.Predecessors[0], m_topology.LocalNode.Id, false))
            //        {
            //            m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ALL, String.Format("ResId: {0} is in interval [{1} - {2}]", id, m_topology.routing_table.Predecessors[0], m_topology.LocalNode.Id));
            //            removeReplicas.Add(replica);
            //        }
            //    }
            //    foreach (string removeRep in removeReplicas)
            //    {
            //        m_topology.Replicas.Remove(removeRep);
            //        Arbiter.Activate(m_ReloadConfig.DispatcherQueue, new IterativeTask<NodeId>(m_topology.routing_table.Successors[0], StoreReplicas));
            //        if(m_topology.routing_table.Successors.Count > 1)
            //            Arbiter.Activate(m_ReloadConfig.DispatcherQueue, new IterativeTask<NodeId>(m_topology.routing_table.Successors[1], StoreReplicas));
            //    }
            //}

            if (m_ReloadConfig.State == ReloadConfig.RELOAD_State.Leave)
                m_machine.SendCommand("Exit");

            if (fSendLeaveFirst)
                //this will reset neighbor tables
                m_topology.Leave();
        }
Esempio n. 23
0
 public FetchReq(ResourceId resourceId, List<StoredDataSpecifier> specifiers,
   UsageManager manager)
 {
     this.RELOAD_MsgCode = RELOAD_MessageCode.Fetch_Request;
     this.resource = resourceId;
     this.specifiers = new List<StoredDataSpecifier>();
     this.specifiers.AddRange(specifiers);
     myManager = manager;
 }
Esempio n. 24
0
        /// <summary>
        /// On data fetch execute the Usages AppProcedure
        /// </summary>
        /// <param name="res_id"></param>
        /// <param name="fetchKindResponse"></param>
        private void OnFetchedData(ResourceId res_id,
          List<FetchKindResponse> fetchKindResponses)
        {
            foreach (var fetchKindResponse in fetchKindResponses)
            {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_USAGE,
                  String.Format("Fetch on {0} returns {1}",
                    res_id, fetchKindResponse.ToString()));
            }

            m_machine.UsageManager.AppProcedure(fetchKindResponses);

        }
Esempio n. 25
0
        public override RELOAD_MessageBody FromReader(ReloadMessage rm,
          BinaryReader reader, long reload_msg_size)
        {

            /* try to read the packet as a FetchReq packet */
            try
            {
                long posBeforeMsg = reader.BaseStream.Position;
                RELOAD_MsgCode = (RELOAD_MessageCode)(UInt16)IPAddress.NetworkToHostOrder(
                  reader.ReadInt16());
                UInt32 message_len = (UInt32)(IPAddress.HostToNetworkOrder(
                  reader.ReadInt32()));

                Byte res_id_length = reader.ReadByte();
                if (res_id_length == 0)
                    throw new System.Exception("Resource ID length == 0!");
                resource = new ResourceId(reader.ReadBytes(res_id_length));
                long posBeforeSpec = reader.BaseStream.Position;
                UInt16 specifiers_length = (UInt16)IPAddress.NetworkToHostOrder(
                  reader.ReadInt16());

                while (StreamUtil.ReadBytes(posBeforeSpec, reader) < specifiers_length)
                {
                    UInt32 kindId = (UInt32)IPAddress.NetworkToHostOrder(
                      reader.ReadInt32());

                    ReloadGlobals.DataModel model = myManager.GetDataModelfromKindId(kindId);

                    UInt64 generation = (UInt64)IPAddress.NetworkToHostOrder(
                      reader.ReadInt64());

                    long posBeforeIndex = reader.BaseStream.Position;
                    UInt16 spec_length = (UInt16)IPAddress.NetworkToHostOrder(
                      reader.ReadInt16());

                    var specifier = new StoredDataSpecifier(myManager);
                    switch (model)
                    {
                        case ReloadGlobals.DataModel.SINGLE_VALUE:
                            break;
                        case ReloadGlobals.DataModel.ARRAY:
                            List<ArrayRange> arrayRanges = new List<ArrayRange>();
                            while (StreamUtil.ReadBytes(posBeforeIndex, reader) <
                              spec_length)
                            {
                                UInt32 first = (UInt32)IPAddress.NetworkToHostOrder(
                                  reader.ReadInt32());
                                UInt32 last = (UInt32)IPAddress.NetworkToHostOrder(
                                  reader.ReadInt32());
                                arrayRanges.Add(new ArrayRange(first, last));
                            }
                            specifier = new StoredDataSpecifier(arrayRanges, kindId,
                              generation, myManager);
                            break;
                        case ReloadGlobals.DataModel.DICTIONARY:
                            List<string> keys = new List<string>();
                            if (spec_length == 0)
                            {
                                // wildcast
                                specifier = new StoredDataSpecifier(keys, kindId,
                                  generation, myManager);
                            }
                            else
                            {
                                while (StreamUtil.ReadBytes(posBeforeIndex, reader) <
                                  spec_length)
                                {
                                    UInt16 key_length = (UInt16)IPAddress.NetworkToHostOrder(
                                      reader.ReadInt16());
                                    keys.Add(Encoding.ASCII.GetString(reader.ReadBytes((short)key_length),
                                      0, key_length));
                                }
                                specifier = new StoredDataSpecifier(keys, kindId,
                                  generation, myManager);
                            }
                            break;
                        default:
                            throw new Exception("An error at FetchReq.FromReader()");
                    }
                    this.specifiers.Add(specifier);
                }
                UInt32 totalRead = StreamUtil.ReadBytes(posBeforeMsg, reader);
                reload_msg_size = reload_msg_size - (totalRead + 1);
            }
            catch (Exception ex)
            {
                myManager.m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR,
                  String.Format("FetchReq.FromBytes: {0}", ex.Message));
            }
            return this;
        }
Esempio n. 26
0
    /// <summary>
    /// Each StoredData element is individually signed.  However, the
    /// signature also must be self-contained and cover the Kind-ID and
    /// Resource-ID even though they are not present in the StoredData
    /// structure.  The input to the signature algorithm is:
    /// resource_id || kind || storage_time || StoredDataValue ||
    /// SignerIdentity
    /// </summary>
    /// <param name="resId"></param>
    /// <param name="kind"></param>
    /// <param name="storageTime"></param>
    /// <param name="storedDataValue"></param>
    /// <param name="identity"></param>
    public Signature(ResourceId resId, UInt32 kind, UInt64 storageTime,
      StoredDataValue value, SignerIdentity signerIdentity,
      ReloadConfig config) {

      m_ReloadConfig = config;
      var ascii = new ASCIIEncoding();
      /* Set alogorithm and identity */
      algorithm =  new SignatureAndHashAlgorithm(HashAlgorithm.sha256,
        ReloadGlobals.SignatureAlg);
      identity = signerIdentity;
      /* Get string of stored data value */
      var ms = new MemoryStream();
      var bw = new BinaryWriter(ms);
      value.Dump(bw);
      value.GetUsageValue.dump(bw);
      ms.Position = 0;
      var sr = new StreamReader(ms);
      string strValue = sr.ReadToEnd();
      sr.Close();
      bw.Close();
      /* Concatenate signature input */
      String signaturInput = String.Format("{0}{1}{2}{3}{4}",
        ascii.GetString(resId.Data, 0, resId.Data.Length), kind, storageTime,
        strValue, identity.ToString());
      signatureValue = Sign(signaturInput);
    }
Esempio n. 27
0
        public NodeId(ResourceId resourceid)
              // call constructor above
            : this()
        {

            if (resourceid == null)
                System.Diagnostics.Debug.Assert(resourceid == null);
            else{
                //the following function fills the array with zeros, the intention is to have leading zero padding bytes if resourceid is smaller
                Array.Clear(this.Data, 0, this.Data.Length);
                if (this.Data.Length > resourceid.Data.Length)
                    Array.Copy(resourceid.Data, 0, this.Data, this.Data.Length - resourceid.Data.Length, resourceid.Data.Length);
                else
                    Array.Copy(resourceid.Data, this.Data, this.Data.Length);
            }
        }