Skip to content

Instantly share code, notes, and snippets.

@Arkotek
Created May 11, 2016 12:54
Show Gist options
  • Save Arkotek/f57e4b9f41038f3e8665fd84ae7033ac to your computer and use it in GitHub Desktop.
Save Arkotek/f57e4b9f41038f3e8665fd84ae7033ac to your computer and use it in GitHub Desktop.
Send and receive file over TCP socket node.js
var debug = require('debug')('filesmanager');
var fs = require('fs');
var defaults = require('../constants.js');
var net = require('net');
var chalk = require('chalk');
var NetFS = function() {
this.port = defaults.DSS_FS_PORT;
this.dss_running = false;
};
module.exports = NetFS;
NetFS.prototype.instanciateMaster = function(file) {
if (this.dss_running == true) return false;
var that = this;
this.dss_running = true;
this.server = net.createServer();
this.server.on('connection', function(socket) {
var sync = fs.createReadStream(file || defaults.SYNC_FILE);
sync.on('error', function(e) {
console.error(e);
});
sync.on('open', function() {
sync.pipe(socket);
});
sync.on('finish', function() {
socket.end();
});
});
this.server.listen(that.port, function() {
debug(chalk.bold.yellow('[DSS]') + ' Started on port %d', that.server.address().port);
});
};
NetFS.prototype.close = function() {
if (this.dss_running === true)
this.server.close();
};
/**
* Called by slave peers on connection
*/
NetFS.prototype.retrieveFile = function(meta, dest_file, cb) {
var dest = fs.createWriteStream(dest_file);
var cb_called = false;
var client = new net.Socket();
var that = this;
var public_ip = meta.public_ip;
var private_ip = meta.private_ip;
dest.on('error', function(err) {
return cb(err);
});
client.on('data', function(data) {
dest.write(data);
});
client.on('error', function(err) {
// if (cb_called == true) return false;
// cb_called = true;
return cb(err);
});
client.on('close', function() {
//if (cb_called == true) return false;
debug(chalk.bold.yellow('[DSS]') + ' File received on ', dest_file);
return cb();
});
client.connect({
port : that.port,
host : public_ip
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment