Exemplo n.º 1
0
  @Override
  public void produceData(final IOSession iosession, final ClientState sessionState)
      throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    String myHelo = heloName;
    if (myHelo == null) {
      myHelo = AddressUtils.resolveLocalDomain(iosession.getLocalAddress());
    }
    switch (this.codecState) {
      case EHLO_READY:
        SMTPCommand ehlo = new SMTPCommand("EHLO", myHelo);
        this.writer.write(ehlo, buf);
        this.codecState = CodecState.EHLO_RESPONSE_EXPECTED;
        break;
      case HELO_READY:
        SMTPCommand helo = new SMTPCommand("HELO", myHelo);
        this.writer.write(helo, buf);
        this.codecState = CodecState.HELO_RESPONSE_EXPECTED;
        break;
    }

    if (buf.hasData()) {
      buf.flush(iosession.channel());
    }
    if (!buf.hasData()) {
      iosession.clearEvent(SelectionKey.OP_WRITE);
    }
  }
  @Override
  public void produceData(final IOSession iosession, final ServerState sessionState)
      throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    synchronized (sessionState) {
      if (this.actionFuture != null) {
        SMTPReply reply = getReply(this.actionFuture);
        this.actionFuture = null;
        this.writer.write(reply, buf);
      }

      if (this.actionFuture == null) {
        while (!this.pendingActions.isEmpty()) {
          Action<ServerState> action = this.pendingActions.remove();
          Future<SMTPReply> future =
              action.execute(sessionState, new OutputTrigger<SMTPReply>(sessionState, iosession));
          if (future.isDone()) {
            SMTPReply reply = getReply(future);
            this.writer.write(reply, buf);
          } else {
            this.actionFuture = future;
            break;
          }
        }
      }

      if (buf.hasData()) {
        buf.flush(iosession.channel());
      }
      if (!buf.hasData()) {
        if (sessionState.getDataType() != null) {
          this.completed = true;
        }
        if (sessionState.isTerminated()) {
          iosession.close();
        } else {
          iosession.clearEvent(SelectionKey.OP_WRITE);
        }
      }
    }
  }