/**
   * Constructor. <br>
   *
   * @param name Name of the layer
   * @param stack The stack in which the module will be
   */
  public ConsensusReplacer(
      String name, ProtocolStack stack, ReplaceProtocol replaceProtocol, Consensus cons, Abcast ab)
      throws AlreadyExistingProtocolModuleException {

    super(name, stack);

    this.replaceConsensus = replaceProtocol;
    this.consensus = cons;
    this.abcast = ab;

    replacingName = new LinkedList<TString>();
    replacingNewFeatures = new LinkedList<TGLinkedList<TElement>>();
    alreadyName = new LinkedList<TString>();
    alreadyNewFeatures = new LinkedList<TGLinkedList<TElement>>();

    LinkedList<ServiceCallOrResponse> initiatedReplace = new LinkedList<ServiceCallOrResponse>();
    initiatedReplace.add(ServiceCallOrResponse.createServiceCallOrResponse(abcast, true));
    replaceConsensusExecuter =
        replaceConsensus.new Executer(this, initiatedReplace) {
          @SuppressWarnings("unchecked")
          public void evaluate(ReplaceProtocolCallParameters infos, Message dmessage) {
            synchronized (this.parent) {
              abcast.call(
                  null,
                  new Message(
                      new RepInfo(infos.name, infos.newFeatures),
                      (Service.Listener) abcastListener));
            }
          }
        };

    LinkedList<ServiceCallOrResponse> initiatedAbcast = new LinkedList<ServiceCallOrResponse>();
    abcastListener =
        abcast.new Listener(this, initiatedAbcast) {
          public void evaluate(AbcastResponseParameters infos, Transportable message) {
            synchronized (this.parent) {
              RepInfo repInfo = (RepInfo) message;
              int index = alreadyName.indexOf(repInfo.name);

              // Add the replacement in the list except if the replacement
              // is already achieved
              if (index < 0) {
                replacingName.addLast(repInfo.name);
                replacingNewFeatures.addLast(repInfo.newFeatures);
              } else {
                alreadyName.remove(index);
                alreadyNewFeatures.remove(index);
              }
            }
          }
        };

    initInterceptor();
  }
  // Init interceptor
  private void initInterceptor() {
    LinkedList<ServiceCallOrResponse> initiatedConsensus = new LinkedList<ServiceCallOrResponse>();
    initiatedConsensus.add(
        ServiceCallOrResponse.createServiceCallOrResponse(replaceConsensus, false));
    consensusInterceptor =
        consensus.new Interceptor(this, initiatedConsensus) {
          public void interceptCall(ConsensusCallParameters params, Message dmessage) {
            synchronized (this.parent) {
              if (!replacingName.isEmpty()) {
                // A replacement is required
                dmessage.content =
                    new Terminate(
                        dmessage.content,
                        replacingName.getFirst(),
                        replacingNewFeatures.getFirst());
              }
              forwardCall(params, dmessage);
            }
          }

          public void interceptResponse(ConsensusResponseParameters infos, Message dmessage) {
            synchronized (this.parent) {
              if (dmessage.content instanceof Terminate) {
                Terminate term = (Terminate) dmessage.content;
                TString name = term.name;
                TGLinkedList<TElement> newFeatures = term.newFeatures;
                int index = replacingName.indexOf(name);

                // Remove from the list
                if (index < 0) {
                  alreadyName.add(name);
                  alreadyNewFeatures.add(newFeatures);
                } else {
                  replacingName.remove(index);
                  replacingNewFeatures.remove(index);
                }

                // Create the new protocol
                createFeatures(term.name.toString(), term.newFeatures);

                dmessage.content = term.content;
              }
              forwardResponse(infos, dmessage);
            }
          }
        };
  }
Beispiel #3
0
  /**
   * Constructor. <br>
   *
   * @param name Name of the layer
   * @param stack The stack in which the module will be
   */
  public ProtocolAbcast(
      String name, ProtocolStack stack, Abcast abcast, Consensus consensus, RPT2PT rpt2pt)
      throws AlreadyExistingProtocolModuleException {

    super(name, stack);
    handlers = new AbcastImpl(this, stack.getFlowControl(), this, stack.getPID());

    this.pValue = nbDynAbcast;
    nbDynAbcast++;
    this.timers = new HashMap<Transportable, AtomicTask>();

    this.abcast = abcast;
    this.consensus = consensus;
    this.rpt2pt = rpt2pt;

    this.abcastCallCOR = ServiceCallOrResponse.createServiceCallOrResponse(this.abcast, true);

    LinkedList<ServiceCallOrResponse> initiatedAbcast = new LinkedList<ServiceCallOrResponse>();
    for (int i = 0; i < MAX_PROCESSES; i++)
      initiatedAbcast.add(ServiceCallOrResponse.createServiceCallOrResponse(rpt2pt, true));
    initiatedAbcast.add(ServiceCallOrResponse.createServiceCallOrResponse(consensus, true));
    abcastExecuter =
        abcast.new Executer(this, initiatedAbcast) {
          public void evaluate(Object params, Message dmessage) {
            synchronized (this.parent) {
              GroupCommEventArgs ga = new GroupCommEventArgs();

              ga.addLast(dmessage.toGroupCommMessage());
              try {
                handlers.handleAbcast(ga);
              } catch (Exception ex) {
                ex.printStackTrace();
                throw new RuntimeException(
                    "ProtocolFastAbcast: " + "abcastExecuter: " + ex.getMessage());
              }
            }
          }
        };

    // Init listeners
    LinkedList<ServiceCallOrResponse> initiatedRpt2pt = new LinkedList<ServiceCallOrResponse>();
    for (int i = 0; i < MAX_PROCESSES; i++)
      initiatedRpt2pt.add(ServiceCallOrResponse.createServiceCallOrResponse(rpt2pt, true));
    initiatedRpt2pt.add(ServiceCallOrResponse.createServiceCallOrResponse(consensus, true));
    rpt2ptListener =
        rpt2pt.new Listener(this, initiatedRpt2pt) {
          public void evaluate(RPT2PTResponseParameters infos, Transportable message) {
            synchronized (this.parent) {
              GroupCommEventArgs ga = new GroupCommEventArgs();

              ga.addLast(message);
              ga.addLast(infos.pid);

              try {
                handlers.handlePt2PtDeliver(ga);
              } catch (Exception ex) {
                throw new RuntimeException(
                    "ProtocolFastAbcast: " + "rpt2ptListener: " + ex.getMessage());
              }
            }
          }
        };

    LinkedList<ServiceCallOrResponse> initiatedConsensus = new LinkedList<ServiceCallOrResponse>();
    for (int i = 0; i < MAX_PROCESSES; i++)
      initiatedConsensus.add(ServiceCallOrResponse.createServiceCallOrResponse(rpt2pt, true));
    initiatedConsensus.add(ServiceCallOrResponse.createServiceCallOrResponse(consensus, true));
    for (int i = 0; i < MAX_MESSAGES; i++)
      initiatedConsensus.add(ServiceCallOrResponse.createServiceCallOrResponse(abcast, false));
    consensusListener =
        consensus.new Listener(this, initiatedConsensus) {
          public void evaluate(ConsensusResponseParameters infos, Transportable message) {
            synchronized (this.parent) {
              GroupCommEventArgs ga = new GroupCommEventArgs();

              ga.addLast(message);
              ga.addLast(((ConsensusID) infos.id).value);

              try {
                handlers.handleDecide(ga);
              } catch (GroupCommException ex) {
                throw new RuntimeException(
                    "ProtocolDynAbcast: " + "handleDecide: " + ex.getMessage());
              }
            }
          }
        };
  }