1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 06:42:42 +01:00

Allow Aphlict to load Javelin and use Javelin class definitions

Summary:
Ref T4324. The server code is probably going to get a fair amount more complicated, so allow it to load Javelin classes in a mostly-reasonable way.

This integration has a few warts, but should be good enough to let us manage complexity through the next iteration of the server.

(Mostly I just want the concicse Javelin mechanism for defining new classes.)

Version bump is just so I can figure stuff out if this creates any issues for users based on which version of things they're running.

Test Plan: Started server, posted some messages through it.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4324

Differential Revision: https://secure.phabricator.com/D8253
This commit is contained in:
epriestley 2014-02-17 16:00:01 -08:00
parent 1b8e129145
commit 260eb5344b
5 changed files with 85 additions and 20 deletions

View file

@ -2,7 +2,7 @@
final class PhabricatorNotificationClient {
const EXPECT_VERSION = 2;
const EXPECT_VERSION = 3;
public static function getServerStatus() {
$uri = PhabricatorEnv::getEnvConfig('notification.server-uri');

View file

@ -6,6 +6,11 @@
* You can also specify `port`, `admin`, `host` and `log`.
*/
var JX = require('./lib/javelin').JX;
JX.require('lib/AphlictIDGenerator', __dirname);
var id_generator = new JX.AphlictIDGenerator();
var config = parse_command_line_arguments(process.argv);
function parse_command_line_arguments(argv) {
@ -108,25 +113,9 @@ function write_json(socket, data) {
var clients = {};
var current_connections = 0;
// According to the internet up to 2^53 can
// be stored in javascript, this is less than that
var MAX_ID = 9007199254740991;//2^53 -1
// If we get one connections per millisecond this will
// be fine as long as someone doesn't maintain a
// connection for longer than 6854793 years. If
// you want to write something pretty be my guest
function generate_id() {
if (typeof generate_id.current_id == 'undefined' ||
generate_id.current_id > MAX_ID) {
generate_id.current_id = 0;
}
return generate_id.current_id++;
}
var send_server = net.createServer(function(socket) {
var client_id = generate_id();
var client_id = id_generator.generateNext();
var client_name = '[' + socket.remoteAddress + '] [#' + client_id + '] ';
clients[client_id] = socket;
@ -189,11 +178,11 @@ var receive_server = http.createServer(function(request, response) {
var status = {
'uptime': (new Date().getTime() - start_time),
'clients.active': current_connections,
'clients.total': generate_id.current_id || 0,
'clients.total': id_generator.getTotalCount(),
'messages.in': messages_in,
'messages.out': messages_out,
'log': config.log,
'version': 2
'version': 3
};
response.write(JSON.stringify(status));

View file

@ -0,0 +1,18 @@
var JX = require('javelin').JX;
JX.install('AphlictIDGenerator', {
members : {
_next : 0,
generateNext : function() {
this._next = ((this._next + 1) % 1000000000000);
return this._next;
},
getTotalCount : function() {
return this._next;
}
}
});

View file

@ -0,0 +1,13 @@
var javelin_root = '../../../../webroot/rsrc/externals/javelin/';
var JX = require(javelin_root + 'core/init_node.js').JX;
JX.require('core/util');
JX.require('core/install');
// NOTE: This is faking out a piece of code in JX.install which waits for
// Stratcom before running static initializers.
JX.Stratcom = {ready : true};
JX.require('core/Event');
JX.require('core/Stratcom');
exports.JX = JX;

View file

@ -0,0 +1,45 @@
/**
* Alternative Javelin init file for Node.js.
*
* @javelin-installs JX.enableDispatch
* @javelin-installs JX.onload
* @javelin-installs JX.flushHoldingQueue
* @javelin-installs JX.require
*
* @javelin
*/
var JX = {};
var fs = require('fs');
var vm = require('vm');
var pathModule = require('path');
var noop = function() {};
JX.enableDispatch = noop;
JX.flushHoldingQueue = noop;
JX.onload = function(func) {
func();
};
JX.require = function(thing, relative) {
relative = relative || __dirname + '/..';
var path = relative + '/' + thing + '.js';
var content = fs.readFileSync(path);
var sandbox = {
JX : this,
__DEV__ : 0,
console : console,
window : {},
require : function (thing) {
return require(pathModule.dirname(path) + '/' + thing);
}
};
vm.createScript(content, path)
.runInNewContext(sandbox, path);
};
exports.JX = JX;