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// See test/simple/test-sendfd.js for a complete description of what this
231cb0ef41Sopenharmony_ci// script is doing and how it fits into the test as a whole.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst net = require('net');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_civar receivedData = [];
281cb0ef41Sopenharmony_civar receivedFDs = [];
291cb0ef41Sopenharmony_civar numSentMessages = 0;
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifunction processData(s) {
321cb0ef41Sopenharmony_ci  if (receivedData.length == 0 || receivedFDs.length == 0) {
331cb0ef41Sopenharmony_ci    return;
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  var fd = receivedFDs.shift();
371cb0ef41Sopenharmony_ci  var d = receivedData.shift();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // Augment our received object before sending it back across the pipe.
401cb0ef41Sopenharmony_ci  d.pid = process.pid;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  // Create a stream around the FD that we received and send a serialized
431cb0ef41Sopenharmony_ci  // version of our modified object back. Clean up when we're done.
441cb0ef41Sopenharmony_ci  var pipeStream = new net.Stream(fd);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  var drainFunc = function() {
471cb0ef41Sopenharmony_ci    pipeStream.destroy();
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    if (++numSentMessages == 2) {
501cb0ef41Sopenharmony_ci      s.destroy();
511cb0ef41Sopenharmony_ci    }
521cb0ef41Sopenharmony_ci  };
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  pipeStream.on('drain', drainFunc);
551cb0ef41Sopenharmony_ci  pipeStream.resume();
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  if (pipeStream.write(JSON.stringify(d) + '\n')) {
581cb0ef41Sopenharmony_ci    drainFunc();
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// Create a UNIX socket to the path defined by argv[2] and read a file
631cb0ef41Sopenharmony_ci// descriptor and misc data from it.
641cb0ef41Sopenharmony_civar s = new net.Stream();
651cb0ef41Sopenharmony_cis.on('fd', function(fd) {
661cb0ef41Sopenharmony_ci  receivedFDs.unshift(fd);
671cb0ef41Sopenharmony_ci  processData(s);
681cb0ef41Sopenharmony_ci});
691cb0ef41Sopenharmony_cis.on('data', function(data) {
701cb0ef41Sopenharmony_ci  data.toString('utf8').trim().split('\n').forEach(function(d) {
711cb0ef41Sopenharmony_ci    receivedData.unshift(JSON.parse(d));
721cb0ef41Sopenharmony_ci  });
731cb0ef41Sopenharmony_ci  processData(s);
741cb0ef41Sopenharmony_ci});
751cb0ef41Sopenharmony_cis.connect(process.argv[2]);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci// vim:ts=2 sw=2 et
78