Skip to content

Commit 260eb53

Browse files
author
epriestley
committed
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
1 parent 1b8e129 commit 260eb53

File tree

5 files changed

+85
-20
lines changed

5 files changed

+85
-20
lines changed

src/applications/notification/client/PhabricatorNotificationClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
final class PhabricatorNotificationClient {
44

5-
const EXPECT_VERSION = 2;
5+
const EXPECT_VERSION = 3;
66

77
public static function getServerStatus() {
88
$uri = PhabricatorEnv::getEnvConfig('notification.server-uri');

support/aphlict/server/aphlict_server.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
* You can also specify `port`, `admin`, `host` and `log`.
77
*/
88

9+
var JX = require('./lib/javelin').JX;
10+
JX.require('lib/AphlictIDGenerator', __dirname);
11+
12+
var id_generator = new JX.AphlictIDGenerator();
13+
914
var config = parse_command_line_arguments(process.argv);
1015

1116
function parse_command_line_arguments(argv) {
@@ -108,25 +113,9 @@ function write_json(socket, data) {
108113

109114
var clients = {};
110115
var current_connections = 0;
111-
// According to the internet up to 2^53 can
112-
// be stored in javascript, this is less than that
113-
var MAX_ID = 9007199254740991;//2^53 -1
114-
115-
// If we get one connections per millisecond this will
116-
// be fine as long as someone doesn't maintain a
117-
// connection for longer than 6854793 years. If
118-
// you want to write something pretty be my guest
119-
120-
function generate_id() {
121-
if (typeof generate_id.current_id == 'undefined' ||
122-
generate_id.current_id > MAX_ID) {
123-
generate_id.current_id = 0;
124-
}
125-
return generate_id.current_id++;
126-
}
127116

128117
var send_server = net.createServer(function(socket) {
129-
var client_id = generate_id();
118+
var client_id = id_generator.generateNext();
130119
var client_name = '[' + socket.remoteAddress + '] [#' + client_id + '] ';
131120

132121
clients[client_id] = socket;
@@ -189,11 +178,11 @@ var receive_server = http.createServer(function(request, response) {
189178
var status = {
190179
'uptime': (new Date().getTime() - start_time),
191180
'clients.active': current_connections,
192-
'clients.total': generate_id.current_id || 0,
181+
'clients.total': id_generator.getTotalCount(),
193182
'messages.in': messages_in,
194183
'messages.out': messages_out,
195184
'log': config.log,
196-
'version': 2
185+
'version': 3
197186
};
198187

199188
response.write(JSON.stringify(status));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var JX = require('javelin').JX;
2+
3+
JX.install('AphlictIDGenerator', {
4+
5+
members : {
6+
_next : 0,
7+
8+
generateNext : function() {
9+
this._next = ((this._next + 1) % 1000000000000);
10+
return this._next;
11+
},
12+
13+
getTotalCount : function() {
14+
return this._next;
15+
}
16+
}
17+
18+
});

support/aphlict/server/lib/javelin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var javelin_root = '../../../../webroot/rsrc/externals/javelin/';
2+
var JX = require(javelin_root + 'core/init_node.js').JX;
3+
4+
JX.require('core/util');
5+
JX.require('core/install');
6+
7+
// NOTE: This is faking out a piece of code in JX.install which waits for
8+
// Stratcom before running static initializers.
9+
JX.Stratcom = {ready : true};
10+
JX.require('core/Event');
11+
JX.require('core/Stratcom');
12+
13+
exports.JX = JX;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Alternative Javelin init file for Node.js.
3+
*
4+
* @javelin-installs JX.enableDispatch
5+
* @javelin-installs JX.onload
6+
* @javelin-installs JX.flushHoldingQueue
7+
* @javelin-installs JX.require
8+
*
9+
* @javelin
10+
*/
11+
12+
var JX = {};
13+
var fs = require('fs');
14+
var vm = require('vm');
15+
var pathModule = require('path');
16+
17+
var noop = function() {};
18+
19+
JX.enableDispatch = noop;
20+
JX.flushHoldingQueue = noop;
21+
22+
JX.onload = function(func) {
23+
func();
24+
};
25+
26+
JX.require = function(thing, relative) {
27+
relative = relative || __dirname + '/..';
28+
var path = relative + '/' + thing + '.js';
29+
var content = fs.readFileSync(path);
30+
31+
var sandbox = {
32+
JX : this,
33+
__DEV__ : 0,
34+
console : console,
35+
window : {},
36+
require : function (thing) {
37+
return require(pathModule.dirname(path) + '/' + thing);
38+
}
39+
};
40+
41+
vm.createScript(content, path)
42+
.runInNewContext(sandbox, path);
43+
};
44+
45+
exports.JX = JX;

0 commit comments

Comments
 (0)