<commit_before>var bCore = require("buster-core");
var when = require("when");
var http = require("http");
var net = require("net");
var bCapServPubsubClient = require("./pubsub-client.js");
var bCapServSessionClient = require("./session-client.js");
var buster = require("buster");

module.exports = {
    create: function (port, host) {
        var instance = buster.create(this);
        instance.serverHost = host;
        instance.serverPort = port;

        instance._pubsubClient = bCapServPubsubClient.create({
            host: host,
            port: port
        });
        instance._pubsubClient.extend(instance);
        instance.connect = instance._connect;

        return instance;
    },

    _connect: function () {
        var d = when.defer();
        var socket = new net.Socket();
        socket.connect(this.serverPort, this.serverHost);

        socket.on("connect", function () {
            socket.destroy();
            this._pubsubClient.connect().then(d.resolve, d.reject);
        }.bind(this));

        socket.on("error", function (e) { d.reject(e); });
        return d.promise;
    },

    createSession: function (resourceSet, opts) {
        var self = this;
        var deferred = when.defer();
        opts = opts || {};

        if (opts.cache) {
            this._request({method: "GET", path: "/resources"}, function (res, body) {
                if (status === 200) {
                    self._createSession(resourceSet, opts, JSON.parse(body), deferred);
                } else {
                    deferred.reject("Unable to get caches from server, unknown reason.");
                }
            });
        } else {
            self._createSession(resourceSet, opts, null, deferred);
        }

        return deferred.promise;
    },

    _createSession: function (resourceSet, opts, cacheManifest, deferred) {
        var self = this;
        resourceSet.serialize(cacheManifest).then(function (sResourceSet) {
            opts.resourceSet = sResourceSet;
            self._request({method: "POST", path: "/sessions"}, function (res, body) {
                body = JSON.parse(body);

                if (res.statusCode === 201) {
                    var sessionClient = bCapServSessionClient._create(
                        body,
                        self._pubsubClient
                    );
                    deferred.resolve(sessionClient);
                } else {
                    deferred.reject(body);
                }
            }).end(JSON.stringify(opts));
        }, deferred.reject);
    },

    clearCache: function () {
        var deferred = when.defer();

        var opts = {
            method: "DELETE",
            path: "/resources"
        };
        var req = this._request(opts, function (res, body) {
            deferred.resolve();
        });
        req.end();

        return deferred.promise;
    },

    setHeader: function (resourceSet, height) {
        var deferred = when.defer();
        var opts = {
            method: "POST",
            path: "/header"
        };
        var req = this._request(opts, function (res, body) {
            deferred.resolve();
        });
        resourceSet.serialize().then(function (srs) {
            req.end(JSON.stringify({
                resourceSet: srs,
                height: height
            }));
        });

        return deferred.promise;
    },

    _request: function (opts, cb) {
        opts.host = this.serverHost;
        opts.port = this.serverPort;

        return http.request(opts, function (res) {
            var body = "";
            res.setEncoding("utf8");
            res.on("data", function (chunk) { body += chunk; });
            res.on("end", function () { cb(res, body); });
        });
    }
};<commit_msg>More cleanups in the server client.<commit_after>var bCore = require("buster-core");
var when = require("when");
var http = require("http");
var net = require("net");
var bCapServPubsubClient = require("./pubsub-client.js");
var bCapServSessionClient = require("./session-client.js");
var buster = require("buster");

module.exports = {
    create: function (port, host) {
        var instance = buster.create(this);
        instance.serverHost = host;
        instance.serverPort = port;

        instance._pubsubClient = bCapServPubsubClient.create({
            host: host,
            port: port
        });
        instance._pubsubClient.extend(instance);
        instance.connect = instance._connect;

        return instance;
    },

    _connect: function () {
        var d = when.defer();
        var socket = new net.Socket();
        socket.connect(this.serverPort, this.serverHost);

        socket.on("connect", function () {
            socket.destroy();
            this._pubsubClient.connect().then(d.resolve, d.reject);
        }.bind(this));

        socket.on("error", function (e) { d.reject(e); });
        return d.promise;
    },

    createSession: function (resourceSet, opts) {
        var self = this;
        var deferred = when.defer();
        opts = opts || {};

        if (opts.cache) {
            this._request("GET", "/resources", function (res, body) {
                if (status === 200) {
                    self._createSession(resourceSet, opts, JSON.parse(body), deferred);
                } else {
                    deferred.reject("Unable to get caches from server, unknown reason.");
                }
            });
        } else {
            self._createSession(resourceSet, opts, null, deferred);
        }

        return deferred.promise;
    },

    _createSession: function (resourceSet, opts, cacheManifest, deferred) {
        var self = this;
        resourceSet.serialize(cacheManifest).then(function (sResourceSet) {
            opts.resourceSet = sResourceSet;
            self._request("POST", "/sessions", function (res, body) {
                body = JSON.parse(body);

                if (res.statusCode === 201) {
                    var sessionClient = bCapServSessionClient._create(
                        body,
                        self._pubsubClient
                    );
                    deferred.resolve(sessionClient);
                } else {
                    deferred.reject(body);
                }
            }).end(JSON.stringify(opts));
        }, deferred.reject);
    },

    clearCache: function () {
        var deferred = when.defer();

        this._request("DELETE", "/resources", function (res, body) {
            deferred.resolve();
        }).end();

        return deferred.promise;
    },

    setHeader: function (resourceSet, height) {
        var deferred = when.defer();
        var req = this._request("POST", "/header", function (res, body) {
            deferred.resolve();
        });
        resourceSet.serialize().then(function (srs) {
            req.end(JSON.stringify({
                resourceSet: srs,
                height: height
            }));
        }, deferred.reject);

        return deferred.promise;
    },

    _request: function (method, path, cb) {
        opts = {};
        opts.method = method;
        opts.path = path;
        opts.host = this.serverHost;
        opts.port = this.serverPort;

        return http.request(opts, function (res) {
            var body = "";
            res.setEncoding("utf8");
            res.on("data", function (chunk) { body += chunk; });
            res.on("end", function () { cb(res, body); });
        });
    }
};
{'fraction_non_alphanumeric': 0.13347599304533905, 'fraction_numerical': 0.0018724087200748963, 'mean_word_length': 1.7381911387770048, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3472999', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1934, 'n_words': 436}