11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciif (common.isWindows)
251cb0ef41Sopenharmony_ci  common.skip('dgram clustering is currently not supported on Windows.');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst NUM_WORKERS = 4;
281cb0ef41Sopenharmony_ciconst PACKETS_PER_WORKER = 10;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst assert = require('assert');
311cb0ef41Sopenharmony_ciconst cluster = require('cluster');
321cb0ef41Sopenharmony_ciconst dgram = require('dgram');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciif (cluster.isPrimary)
351cb0ef41Sopenharmony_ci  primary();
361cb0ef41Sopenharmony_cielse
371cb0ef41Sopenharmony_ci  worker();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction primary() {
411cb0ef41Sopenharmony_ci  let listening = 0;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  // Fork 4 workers.
441cb0ef41Sopenharmony_ci  for (let i = 0; i < NUM_WORKERS; i++)
451cb0ef41Sopenharmony_ci    cluster.fork();
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  // Wait until all workers are listening.
481cb0ef41Sopenharmony_ci  cluster.on('listening', common.mustCall((worker, address) => {
491cb0ef41Sopenharmony_ci    if (++listening < NUM_WORKERS)
501cb0ef41Sopenharmony_ci      return;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci    // Start sending messages.
531cb0ef41Sopenharmony_ci    const buf = Buffer.from('hello world');
541cb0ef41Sopenharmony_ci    const socket = dgram.createSocket('udp4');
551cb0ef41Sopenharmony_ci    let sent = 0;
561cb0ef41Sopenharmony_ci    doSend();
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    function doSend() {
591cb0ef41Sopenharmony_ci      socket.send(buf, 0, buf.length, address.port, address.address, afterSend);
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    function afterSend() {
631cb0ef41Sopenharmony_ci      sent++;
641cb0ef41Sopenharmony_ci      if (sent < NUM_WORKERS * PACKETS_PER_WORKER) {
651cb0ef41Sopenharmony_ci        doSend();
661cb0ef41Sopenharmony_ci      } else {
671cb0ef41Sopenharmony_ci        socket.close();
681cb0ef41Sopenharmony_ci      }
691cb0ef41Sopenharmony_ci    }
701cb0ef41Sopenharmony_ci  }, NUM_WORKERS));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  // Set up event handlers for every worker. Each worker sends a message when
731cb0ef41Sopenharmony_ci  // it has received the expected number of packets. After that it disconnects.
741cb0ef41Sopenharmony_ci  for (const key in cluster.workers) {
751cb0ef41Sopenharmony_ci    if (Object.hasOwn(cluster.workers, key))
761cb0ef41Sopenharmony_ci      setupWorker(cluster.workers[key]);
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  function setupWorker(worker) {
801cb0ef41Sopenharmony_ci    let received = 0;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    worker.on('message', common.mustCall((msg) => {
831cb0ef41Sopenharmony_ci      received = msg.received;
841cb0ef41Sopenharmony_ci      worker.disconnect();
851cb0ef41Sopenharmony_ci    }));
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci    worker.on('exit', common.mustCall(() => {
881cb0ef41Sopenharmony_ci      assert.strictEqual(received, PACKETS_PER_WORKER);
891cb0ef41Sopenharmony_ci    }));
901cb0ef41Sopenharmony_ci  }
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_cifunction worker() {
951cb0ef41Sopenharmony_ci  let received = 0;
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  // Create udp socket and start listening.
981cb0ef41Sopenharmony_ci  const socket = dgram.createSocket('udp4');
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  socket.on('message', common.mustCall((data, info) => {
1011cb0ef41Sopenharmony_ci    received++;
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci    // Every 10 messages, notify the primary.
1041cb0ef41Sopenharmony_ci    if (received === PACKETS_PER_WORKER) {
1051cb0ef41Sopenharmony_ci      process.send({ received });
1061cb0ef41Sopenharmony_ci      socket.close();
1071cb0ef41Sopenharmony_ci    }
1081cb0ef41Sopenharmony_ci  }, PACKETS_PER_WORKER));
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  socket.bind(0);
1111cb0ef41Sopenharmony_ci}
112