Browse Source

dgram: reintroduce addMembership() and dropMembership()

Removed during the early stages of node 0.5 refactoring to libuv.
v0.7.4-release
Roman Shtylman 14 years ago
committed by Ben Noordhuis
parent
commit
a38fd6056c
  1. 22
      lib/dgram.js
  2. 41
      src/udp_wrap.cc
  3. 160
      test/simple/test-dgram-multicast-multi-process.js

22
lib/dgram.js

@ -243,16 +243,26 @@ Socket.prototype.setMulticastLoopback = function(arg) {
Socket.prototype.addMembership = function(multicastAddress, Socket.prototype.addMembership = function(multicastAddress,
multicastInterface) { interfaceAddress) {
// are we ever going to support this in libuv? this._healthCheck();
throw new Error('not yet implemented');
if (!multicastAddress) {
throw new Error('multicast address must be specified');
}
return this._handle.addMembership(multicastAddress, interfaceAddress);
}; };
Socket.prototype.dropMembership = function(multicastAddress, Socket.prototype.dropMembership = function(multicastAddress,
multicastInterface) { interfaceAddress) {
// are we ever going to support this in libuv? this._healthCheck();
throw new Error('not yet implemented');
if (!multicastAddress) {
throw new Error('multicast address must be specified');
}
return this._handle.dropMembership(multicastAddress, interfaceAddress);
}; };

41
src/udp_wrap.cc

@ -91,6 +91,8 @@ public:
static Handle<Value> RecvStart(const Arguments& args); static Handle<Value> RecvStart(const Arguments& args);
static Handle<Value> RecvStop(const Arguments& args); static Handle<Value> RecvStop(const Arguments& args);
static Handle<Value> GetSockName(const Arguments& args); static Handle<Value> GetSockName(const Arguments& args);
static Handle<Value> AddMembership(const Arguments& args);
static Handle<Value> DropMembership(const Arguments& args);
private: private:
static inline char* NewSlab(v8::Handle<v8::Object> global, v8::Handle<v8::Object> wrap_obj); static inline char* NewSlab(v8::Handle<v8::Object> global, v8::Handle<v8::Object> wrap_obj);
@ -100,6 +102,8 @@ private:
static Handle<Value> DoBind(const Arguments& args, int family); static Handle<Value> DoBind(const Arguments& args, int family);
static Handle<Value> DoSend(const Arguments& args, int family); static Handle<Value> DoSend(const Arguments& args, int family);
static Handle<Value> SetMembership(const Arguments& args,
uv_membership membership);
static uv_buf_t OnAlloc(uv_handle_t* handle, size_t suggested_size); static uv_buf_t OnAlloc(uv_handle_t* handle, size_t suggested_size);
static void OnSend(uv_udp_send_t* req, int status); static void OnSend(uv_udp_send_t* req, int status);
@ -147,6 +151,8 @@ void UDPWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart); NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart);
NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop); NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop);
NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName); NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
NODE_SET_PROTOTYPE_METHOD(t, "addMembership", AddMembership);
NODE_SET_PROTOTYPE_METHOD(t, "dropMembership", DropMembership);
target->Set(String::NewSymbol("UDP"), target->Set(String::NewSymbol("UDP"),
Persistent<FunctionTemplate>::New(t)->GetFunction()); Persistent<FunctionTemplate>::New(t)->GetFunction());
@ -204,6 +210,41 @@ Handle<Value> UDPWrap::Bind6(const Arguments& args) {
} }
Handle<Value> UDPWrap::SetMembership(const Arguments& args,
uv_membership membership) {
HandleScope scope;
UNWRAP
assert(args.Length() == 2);
String::Utf8Value address(args[0]->ToString());
String::Utf8Value interface(args[1]->ToString());
const char* interface_cstr = *interface;
if (args[1]->IsUndefined() || args[1]->IsNull()) {
interface_cstr = NULL;
}
int r = uv_udp_set_membership(&wrap->handle_, *address, interface_cstr,
membership);
if (r)
SetErrno(uv_last_error(uv_default_loop()));
return scope.Close(Integer::New(r));
}
Handle<Value> UDPWrap::AddMembership(const Arguments& args) {
return SetMembership(args, UV_JOIN_GROUP);
}
Handle<Value> UDPWrap::DropMembership(const Arguments& args) {
return SetMembership(args, UV_LEAVE_GROUP);
}
Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) { Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) {
HandleScope scope; HandleScope scope;
int r; int r;

160
test/simple/test-dgram-multicast-multi-process.js

@ -0,0 +1,160 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common'),
assert = require('assert'),
cluster = require('cluster'),
dgram = require('dgram'),
util = require('util'),
assert = require('assert'),
Buffer = require('buffer').Buffer,
LOCAL_BROADCAST_HOST = '224.0.0.1',
messages = [
new Buffer('First message to send'),
new Buffer('Second message to send'),
new Buffer('Third message to send'),
new Buffer('Fourth message to send')
];
if (cluster.isMaster) {
var workers = {},
listeners = 3,
listening = 0,
i = 0,
done = 0;
//launch child processes
for (var x = 0; x < listeners; x++) {
(function () {
var worker = cluster.fork();
workers[worker.pid] = worker;
worker.messagesReceived = [];
worker.on('message', function (msg) {
if (msg.listening) {
listening += 1;
if (listening === listeners) {
//all child process are listening, so start sending
sendSocket.sendNext();
}
}
else if (msg.message) {
worker.messagesReceived.push(msg.message);
if (worker.messagesReceived.length === messages.length) {
done += 1;
console.error('%d received %d messages total.', worker.pid,
worker.messagesReceived.length);
}
if (done === listeners) {
console.error('All workers have received the required number of'
+ 'messages. Will now compare.');
Object.keys(workers).forEach(function (pid) {
var worker = workers[pid];
var count = 0;
worker.messagesReceived.forEach(function(buf) {
for (var i = 0; i < messages.length; ++i) {
if (buf.toString() === messages[i].toString()) {
count++;
break;
}
}
});
console.error('%d received %d matching messges.', worker.pid
, count);
assert.equal(count, messages.length
,'A worker received an invalid multicast message');
});
}
}
});
})(x);
}
var sendSocket = dgram.createSocket('udp4');
//sendSocket.setBroadcast(true);
//sendSocket.setMulticastTTL(1);
//sendSocket.setMulticastLoopback(true);
sendSocket.on('close', function() {
console.error('sendSocket closed');
});
sendSocket.sendNext = function() {
var buf = messages[i++];
if (!buf) {
try { sendSocket.close(); } catch (e) {}
return;
}
sendSocket.send(buf, 0, buf.length,
common.PORT, LOCAL_BROADCAST_HOST, function(err) {
if (err) throw err;
console.error('sent %s to %s', util.inspect(buf.toString()),
LOCAL_BROADCAST_HOST + common.PORT);
process.nextTick(sendSocket.sendNext);
});
};
}
if (!cluster.isMaster) {
var receivedMessages = [];
var listenSocket = dgram.createSocket('udp4');
listenSocket.addMembership(LOCAL_BROADCAST_HOST);
listenSocket.on('message', function(buf, rinfo) {
console.error('%s received %s from %j', process.pid
,util.inspect(buf.toString()), rinfo);
receivedMessages.push(buf);
process.send({ message : buf.toString() });
if (receivedMessages.length == messages.length) {
listenSocket.dropMembership(LOCAL_BROADCAST_HOST);
process.nextTick(function() { // TODO should be changed to below.
// listenSocket.dropMembership(LOCAL_BROADCAST_HOST, function() {
listenSocket.close();
});
}
});
listenSocket.on('close', function() {
process.exit();
});
listenSocket.on('listening', function() {
process.send({ listening : true });
});
listenSocket.bind(common.PORT);
}
Loading…
Cancel
Save