Example #1
0
    /** {@inheritDoc} */
    @Override
    protected void body() throws InterruptedException, IgniteInterruptedCheckedException {
      try {
        IgfsDataInputStream dis = new IgfsDataInputStream(endpoint.inputStream());

        byte[] hdr = new byte[IgfsMarshaller.HEADER_SIZE];

        boolean first = true;

        while (!Thread.currentThread().isInterrupted()) {
          dis.readFully(hdr);

          final long reqId = U.bytesToLong(hdr, 0);

          int ordinal = U.bytesToInt(hdr, 8);

          if (first) { // First message must be HANDSHAKE.
            if (reqId != 0 || ordinal != IgfsIpcCommand.HANDSHAKE.ordinal()) {
              if (log.isDebugEnabled())
                log.debug(
                    "IGFS IPC handshake failed [reqId=" + reqId + ", ordinal=" + ordinal + ']');

              return;
            }

            first = false;
          }

          final IgfsIpcCommand cmd = IgfsIpcCommand.valueOf(ordinal);

          IgfsMessage msg = marsh.unmarshall(cmd, hdr, dis);

          IgniteInternalFuture<IgfsMessage> fut = hnd.handleAsync(ses, msg, dis);

          // If fut is null, no response is required.
          if (fut != null) {
            if (fut.isDone()) {
              IgfsMessage res;

              try {
                res = fut.get();
              } catch (IgniteCheckedException e) {
                res = new IgfsControlResponse();

                ((IgfsControlResponse) res).error(e);
              }

              try {
                synchronized (out) {
                  // Reuse header.
                  IgfsMarshaller.fillHeader(hdr, reqId, res.command());

                  marsh.marshall(res, hdr, out);

                  out.flush();
                }
              } catch (IOException | IgniteCheckedException e) {
                shutdown0(e);
              }
            } else {
              fut.listen(
                  new CIX1<IgniteInternalFuture<IgfsMessage>>() {
                    @Override
                    public void applyx(IgniteInternalFuture<IgfsMessage> fut) {
                      IgfsMessage res;

                      try {
                        res = fut.get();
                      } catch (IgniteCheckedException e) {
                        res = new IgfsControlResponse();

                        ((IgfsControlResponse) res).error(e);
                      }

                      try {
                        synchronized (out) {
                          byte[] hdr = IgfsMarshaller.createHeader(reqId, res.command());

                          marsh.marshall(res, hdr, out);

                          out.flush();
                        }
                      } catch (IOException | IgniteCheckedException e) {
                        shutdown0(e);
                      }
                    }
                  });
            }
          }
        }
      } catch (EOFException ignored) {
        // Client closed connection.
      } catch (IgniteCheckedException | IOException e) {
        if (!isCancelled())
          U.error(log, "Failed to read data from client (will close connection)", e);
      } finally {
        onFinished();
      }
    }