Exemplo n.º 1
0
  /**
   * Send the CREATE_MEDIATOR command with the necessary parameter in order to create the BackEnd in
   * the fixed network. Executed - at bootstrap time by the thread that creates the
   * FrontEndContainer. - To re-attach to the platform after a fault of the BackEnd
   */
  private synchronized void createBackEnd() throws IMTPException {
    StringBuffer sb = BackEndStub.encodeCreateMediatorRequest(props);
    if (myMediatorID != null) {
      // This is a request to re-create my expired back-end
      BackEndStub.appendProp(sb, JICPProtocol.MEDIATOR_ID_KEY, myMediatorID);
      BackEndStub.appendProp(sb, "outcnt", String.valueOf(outCnt));
      BackEndStub.appendProp(sb, "lastsid", String.valueOf(lastSid));
    }
    JICPPacket pkt =
        new JICPPacket(
            JICPProtocol.CREATE_MEDIATOR_TYPE,
            JICPProtocol.DEFAULT_INFO,
            null,
            sb.toString().getBytes());

    // Try first with the current transport address, then with the various backup addresses
    for (int i = -1; i < backEndAddresses.length; i++) {

      if (i >= 0) {
        // Set the mediator address to a new address..
        String addr = backEndAddresses[i];
        int colonPos = addr.indexOf(':');
        String host = addr.substring(0, colonPos);
        String port = addr.substring(colonPos + 1, addr.length());
        mediatorTA = new JICPAddress(host, port, myMediatorID, "");
      }

      try {
        HTTPClientConnection hc = (HTTPClientConnection) getConnection(mediatorTA);
        myLogger.log(
            Logger.INFO,
            "Creating BackEnd on "
                + hc.getProtocol()
                + mediatorTA.getHost()
                + ":"
                + mediatorTA.getPort());
        pkt = deliver(pkt);

        String replyMsg = new String(pkt.getData());
        if (pkt.getType() != JICPProtocol.ERROR_TYPE) {
          // BackEnd creation successful
          BackEndStub.parseCreateMediatorResponse(replyMsg, props);
          myMediatorID = props.getProperty(JICPProtocol.MEDIATOR_ID_KEY);
          // Complete the mediator address with the mediator ID
          mediatorTA =
              new JICPAddress(mediatorTA.getHost(), mediatorTA.getPort(), myMediatorID, null);
          myDisconnectionManager.setReachable();
          myKeepAliveManager.update();
          myLogger.log(Logger.INFO, "BackEnd OK. Mediator ID is " + myMediatorID);
          return;
        } else {
          myLogger.log(Logger.WARNING, "Mediator error: " + replyMsg);
        }
      } catch (IOException ioe) {
        // Ignore it, and try the next address...
        myLogger.log(Logger.WARNING, "Connection error", ioe);
      }
    }

    // No address succeeded!
    throw new IMTPException("Error creating the BackEnd.");
  }