Example #1
0
  private OncRpcClient createRpcClient() throws OncRpcException, IOException {
    // invoke portmap
    OncRpcPortmapClient portmap = new OncRpcPortmapClient(host);
    int port;
    try {
      port =
          portmap.getPort(
              NFS_PROGRAM,
              NFS_VERSION,
              protocol == Protocol.UDP ? OncRpcProtocols.ONCRPC_UDP : OncRpcProtocols.ONCRPC_TCP);
    } finally {
      portmap.close();
    }

    // create the client
    // We create the client with a buffer with lenght equals witn MAX_DATA +
    // 424 ( max header length)
    OncRpcClient client = null;
    if (protocol == Protocol.UDP) {
      client = new OncRpcUdpClient(host, NFS_PROGRAM, NFS_VERSION, port, MAX_DATA + HEADER_DATA);
    } else if (protocol == Protocol.TCP) {
      client = new OncRpcTcpClient(host, NFS_PROGRAM, NFS_VERSION, port, MAX_DATA + HEADER_DATA);
    } else {
      // TODO Do something
    }
    client.setTimeout(10000);
    if (uid != -1 && gid != -1) {
      client.setAuth(new OncRpcClientAuthUnix("test", uid, gid));
    }
    return client;
  }
Example #2
0
  private void call(final int functionId, final XdrAble parameter, final XdrAble result)
      throws NFS2Exception, IOException {
    OncRpcClient client = null;
    int countCall = 0;
    while (true) {
      try {
        countCall++;
        client = getRpcClient();
        if (result == XdrVoid.XDR_VOID) {
          client.call(functionId, parameter, result);
        } else {
          ResultWithCode nfsResult = new ResultWithCode(result);
          client.call(functionId, parameter, nfsResult);
          if (nfsResult.getResultCode() != ResultCode.NFS_OK) {
            throw new NFS2Exception(nfsResult.getResultCode());
          }
        }
        break;
      } catch (Exception e) {
        if (client != null) {
          try {
            client.close();
          } catch (OncRpcException e1) {
            // Ignore this
          }
          client = null;
        }
        if (e instanceof RuntimeException) {
          throw (RuntimeException) e;
        }
        if (e instanceof OncRpcException) {
          if (countCall > 5) {
            throw new NFS2Exception(e.getMessage(), e);
          } else {
            LOGGER.warn(
                "An error occurs when nfs file system try to call the rpc method. Reason: "
                    + e.getMessage()
                    + " . It will try again");
            continue;
          }
        } else {
          throw new NFS2Exception(e.getMessage(), e);
        }
      } finally {

        if (client != null) {
          try {
            releaseRpcClient(client);
          } catch (IOException e) {
            // ignore
          }
        }
      }
    }
  }
Example #3
0
 public synchronized void close() throws IOException {
   closed = true;
   List<OncRpcException> exceptionList = new ArrayList<OncRpcException>();
   for (OncRpcClient client : rpcClientPool) {
     try {
       client.close();
     } catch (OncRpcException e) {
       exceptionList.add(e);
     }
   }
   if (exceptionList.size() != 0) {
     StringBuilder builder = new StringBuilder();
     builder.append("An error occurs when the mount client close connections. Reason:");
     for (OncRpcException anExceptionList : exceptionList) {
       builder.append(anExceptionList.getMessage());
       builder.append('.');
     }
     throw new IOException(builder.toString());
   }
 }