-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
151 lines (127 loc) · 4.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var url = require('url');
var path = require('path');
var events = require('events');
var fs = require('fs');
// Import Default Transport Modules
var axon = require('./transports/axon');
var http = require('./transports/http');
// Application Defaults
var defaults = require('./defaults');
// Configure Application Dependencies
var lib = require('./lib');
var app = {}
// Dependency Injection Magic
//
// o
// O /`-.__
// / \·'^|
// o T l *
// _|-..-|_
// O (^ '----' `)
// `\-....-/^ Dependicus Injectus
// O o ) "/ " (
// _( (-) )_
// O /\ ) ( /\
// / \( ) | \
// o o \) ( / \
// / |( )| \
// / o \ \( / \
// __.--' O \_ / .._ \
// //|)\ , (_) /(((\^)'\
// | | O ) ` |
// | / o___ / /
// / _.-''^^__O_^^''-._ /
// .' / -''^^ ^^''- \--'^
// .' .`. `'''----'''^ .`. \
// .' / `'--..____..--'^ \ \
// / _.-/ \ \
// .::'_/^ | | `.
// .-'| | `-.
// _.--'` \ / `-.
// / \ / `-._
// `'---..__ `. .´_.._ __ \
// ``'''`. .' `'^ `''---'^
// `-..______..-'
//
app.Node = lib.node .forge(app);
app.Transport = lib.transport .forge(app);
app.Vertex = lib.vertex .forge(app);
app.Gateway = lib.gateway .forge(app);
app.Hub = lib.hub .forge(app);
app.Actor = lib.actor .forge(app);
app.Director = lib.director .forge(app);
app.Router = lib.router .forge(app);
app.Producer = lib.producer .forge(app);
app.Route = lib.route .forge(app);
app.Table = lib.table .forge(app);
// This is the messy stuff, but thankfully most of the mess
// is contained here. Over time, the logic defined here should
// be refactored into its own modules.
function start(options){
var outbox = new events.EventEmitter();
var inbox = new events.EventEmitter();
var hub = app.Hub.NewWithEmitters(inbox,outbox);
var gateway = hub.createGateway();
var vertex = hub.createVertex();
// Create a Loopback Interface for Protocol-less Addresses
var loopback = gateway.createTransport();
loopback.enqueue = loopback.receive;
// Transports are loaded dynamically
//
// Loop over transports defined in `options` and
// attempt to load and initialize each transport
var transports = options.transports;
var transport_keys = Object.keys(transports);
transport_keys.forEach(function(key){
var tran_options = transports[key];
var tran_module = tran_options.module || 'transports/' + key;
var tran_path = path.resolve(__dirname, tran_module);
var tran_setup = require(tran_path);
if(tran_options.disabled) return;
var transport = gateway.createTransport(key + ':');
tran_setup.init(transport,tran_options);
});
// Configure Routes Table
var route_json = {
regex: /.*/,
address: '/'
}
var table = app.Table.New();
var route = app.Route.NewFromJSON(route_json);
table.addRoute(1000000,route);
// Load Optional Routes
//
// Addtional routes can be loaded from a JSON file
// Pass the path to the file in as part of the init options `table_file`
if(options.table_file){
var table_json = fs.readFileSync(options.table_file);
var table_obj = JSON.parse(table_json);
for(var i=0; i<table_obj.length; i++){
var rt_json = table_obj[i];
var tb_route = app.Route.NewFromJSON(rt_json);
table.addRoute(i * 100,tb_route);
}
}
// Configure Router
var r_emit = new events.EventEmitter();
var router = app.Router.NewWithTableAndEmitter(table,r_emit);
var node = vertex.createNode();
var producer = app.Producer.NewWithRouter(router);
// Bind router events to the node handling message transport
r_emit.on('send',function(address,packet){
node.send(address,packet);
});
// Route incoming node messages to the producer -> directory -> actor
node.receive = function(packet){
producer.enqueue(packet);
}
// Producer Contains all Relevant Sub-Systems
return producer;
}
// Expose defaults
module.exports.defaults = defaults;
// Go Baby Go!
module.exports.init = function(options){
var opts = options || defaults;
return start(opts);
}