The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / test / simple / test-cluster-dgram-1.js @ 5e7e51c2

History | View | Annotate | Download (3.21 KB)

1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21

    
22
var NUM_WORKERS = 4;
23
var PACKETS_PER_WORKER = 10;
24

    
25
var assert = require('assert');
26
var cluster = require('cluster');
27
var common = require('../common');
28
var dgram = require('dgram');
29

    
30

    
31
if (process.platform === 'win32') {
32
  console.warn("dgram clustering is currently not supported on windows.");
33
  process.exit(0);
34
}
35

    
36
if (cluster.isMaster)
37
  master();
38
else
39
  worker();
40

    
41

    
42
function master() {
43
  var listening = 0;
44

    
45
  // Fork 4 workers.
46
  for (var i = 0; i < NUM_WORKERS; i++)
47
    cluster.fork();
48

    
49
  // Wait until all workers are listening.
50
  cluster.on('listening', function() {
51
    if (++listening < NUM_WORKERS)
52
      return;
53

    
54
    // Start sending messages.
55
    var buf = new Buffer('hello world');
56
    var socket = dgram.createSocket('udp4');
57
    var sent = 0;
58
    doSend();
59

    
60
    function doSend() {
61
      socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1', afterSend);
62
    }
63

    
64
    function afterSend() {
65
      sent++;
66
      if (sent < NUM_WORKERS * PACKETS_PER_WORKER) {
67
        doSend();
68
      } else {
69
        console.log('master sent %d packets', sent);
70
        socket.close();
71
      }
72
    }
73
  });
74

    
75
  // Set up event handlers for every worker. Each worker sends a message when
76
  // it has received the expected number of packets. After that it disconnects.
77
  for (var key in cluster.workers) {
78
    if (cluster.workers.hasOwnProperty(key))
79
      setupWorker(cluster.workers[key]);
80
  }
81

    
82
  function setupWorker(worker) {
83
    var received = 0;
84

    
85
    worker.on('message', function(msg) {
86
      received = msg.received;
87
      console.log('worker %d received %d packets', worker.id, received);
88
    });
89

    
90
    worker.on('disconnect', function() {
91
      assert(received === PACKETS_PER_WORKER);
92
      console.log('worker %d disconnected', worker.id);
93
    });
94
  }
95
}
96

    
97

    
98
function worker() {
99
  var received = 0;
100

    
101
  // Create udp socket and start listening.
102
  var socket = dgram.createSocket('udp4');
103

    
104
  socket.on('message', function(data, info) {
105
    received++;
106

    
107
    // Every 10 messages, notify the master.
108
    if (received == PACKETS_PER_WORKER) {
109
      process.send({received: received});
110
      process.disconnect();
111
    }
112
  });
113

    
114
  socket.bind(common.PORT);
115
}