UNPKG

oncrpc

Version:

Library for creating ONC RPC Node.js servers and clients

199 lines (150 loc) 4.57 kB
// Copyright 2013 Joyent, Inc. All rights reserved. // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. var util = require('util'); var assert = require('assert-plus'); var clone = require('clone'); var once = require('once'); var RpcCall = require('../call').RpcCall; var RpcClient = require('../client').RpcClient; var PortmapDumpReply = require('./dump_reply').PortmapDumpReply; var PortmapGetPortCall = require('./get_port_call').PortmapGetPortCall; var PortmapGetPortReply = require('./get_port_reply').PortmapGetPortReply; var PortmapSetCall = require('./set_call').PortmapSetCall; var PortmapSetReply = require('./set_reply').PortmapSetReply; var PortmapUnsetCall = require('./unset_call').PortmapUnsetCall; var PortmapUnsetReply = require('./unset_reply').PortmapUnsetReply; ///--- API function PortmapClient(opts) { assert.object(opts, 'options'); if (opts.log) { var l = opts.log; delete opts.log; } var _opts = clone(opts); _opts.log = opts.log = l; _opts.name = 'portmap'; _opts.program = 100000; _opts.version = 2; RpcClient.call(this, _opts); } util.inherits(PortmapClient, RpcClient); PortmapClient.prototype.set = function set(opts, cb) { assert.object(opts, 'options'); assert.number(opts.prog, 'options.prog'); assert.number(opts.vers, 'options.vers'); assert.number(opts.prot, 'options.prot'); assert.number(opts.port, 'options.port'); assert.func(cb, 'callback'); cb = once(cb); var call = new PortmapSetCall({ incoming: false, proc: 1 }); call.mapping = { prog: opts.prog, vers: opts.vers, prot: opts.prot, port: opts.port }; this._rpc(call, PortmapSetReply, function (err, reply) { if (err) { cb(err, reply); return; } reply.on('data', function (chunk) {}); reply.once('error', cb); reply.once('end', function () { cb(null, reply); }); }); call.end(); return (this); }; PortmapClient.prototype.unset = function unset(opts, cb) { assert.object(opts, 'options'); assert.number(opts.prog, 'options.prog'); assert.number(opts.vers, 'options.vers'); assert.number(opts.prot, 'options.prot'); assert.number(opts.port, 'options.port'); assert.func(cb, 'callback'); cb = once(cb); var call = new PortmapUnsetCall({ incoming: false, proc: 2 }); call.mapping = { prog: opts.prog, vers: opts.vers, prot: opts.prot, port: opts.port }; this._rpc(call, PortmapUnsetReply, function (err, reply) { if (err) { cb(err, reply); return; } reply.on('data', function (chunk) {}); reply.once('error', cb); reply.once('end', function () { cb(null, reply); }); }); call.end(); return (this); }; PortmapClient.prototype.get_port = function get_port(opts, cb) { assert.object(opts, 'options'); assert.number(opts.prog, 'options.prog'); assert.number(opts.vers, 'options.vers'); assert.number(opts.prot, 'options.prot'); assert.func(cb, 'callback'); cb = once(cb); var call = new PortmapGetPortCall({ incoming: false, proc: 3 }); this._rpc(call, PortmapGetPortReply, function (err, reply) { if (err) { cb(err, reply); return; } reply.on('data', function (chunk) {}); reply.once('error', cb); reply.once('end', function () { cb(null, reply); }); }); call.end(); return (this); }; PortmapClient.prototype.dump = function dump(cb) { assert.func(cb, 'callback'); cb = once(cb); var call = new RpcCall({ incoming: false, proc: 4 }); this._rpc(call, PortmapDumpReply, function (err, reply) { if (err) { cb(err, reply); return; } reply.on('data', function (chunk) {}); reply.once('error', cb); reply.once('end', function () { cb(null, reply); }); }); call.end(); return (this); }; ///--- Exports module.exports = { PortmapClient: PortmapClient, createPortmapClient: function createPortmapClient(opts) { return (new PortmapClient(opts)); } };