Module: NFS::SUNRPC::Client

Defined in:
lib/nfs/sunrpc/client.rb

Constant Summary collapse

@@xid =
0
@@xid_mutex =
Mutex.new

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nfs/sunrpc/client.rb', line 7

def method_missing(name, *args)
  procedure = @version.get_procedure(name)

  if procedure.nil?
    raise NoMethodError, name.to_s
  end

  if args.size == 0
    args = [nil]
  end

  if args.size != 1
    raise ArgumentError
  end

  xid = nil

  @@xid_mutex.synchronize do
    xid = @@xid
    @@xid += 1
  end

  message = RpcMsg.encode({
    xid: xid,
    body: {
      _discriminant: :CALL,
      cbody: {
        rpcvers: 2,
        prog: @program.number,
        vers: @version.number,
        proc: procedure.number,
        cred: {
          flavor: :AUTH_NULL,
          body: ''
        },
        verf: {
          flavor: :AUTH_NULL,
          body: ''
        }
      }
    }
  }) + procedure.encode(args[0])

  # This will return the result object or raise an exception that
  # contains the cause of the error.
  sendrecv(message) do |result|
    envelope = RpcMsg.decode(result)

    if envelope[:xid] == xid
      if envelope[:body][:_discriminant] != :REPLY
        raise envelope.inspect
      end

      if envelope[:body][:rbody][:_discriminant] != :MSG_ACCEPTED
        raise envelope[:body][:rbody].inspect
      end

      if envelope[:body][:rbody][:areply][:reply_data][:_discriminant] != :SUCCESS

        raise envelope[:body][:rbody][:areply][:reply_data].inspect
      end

      procedure.decode(result)
    else
      false # false means keep giving us received messages to inspect
    end
  end
end