| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  | module.exports.create = function (prefixes) { | 
					
						
							|  |  |  |   if (prefixes.debug) { console.log('[cluster-rpc] master created'); } | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |   var m = new (require('events').EventEmitter)(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   m.addWorker = function (worker) { | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  |     if (prefixes.debug) { console.log('[cluster-rpc] [master] adding worker'); } | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |     m._workers = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var w = new (require('events').EventEmitter)(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  |     function emitConnection() { | 
					
						
							|  |  |  |       if (w.__online) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       w.__online = true; | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |       m.emit('connection', w); | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     worker.on('online', function () { | 
					
						
							|  |  |  |       if (prefixes.debug) { console.log('[cluster-rpc] [master] worker came online, at fork'); } | 
					
						
							|  |  |  |       emitConnection(); | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     worker.on('message', function (data) { | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  |       if (prefixes.connect === data.type) { | 
					
						
							|  |  |  |         if (prefixes.debug) { console.log('[cluster-rpc] [master] worker connected, manually'); } | 
					
						
							|  |  |  |         emitConnection(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (prefixes.debug) { console.log('[cluster-rpc] [master] worker sent message', data); } | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |       w.emit('message', data); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     w.send = function (data) { | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  |       if (prefixes.debug) { console.log('[cluster-rpc] [master] sending', data); } | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |       worker.send(data); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-20 19:13:48 -06:00
										 |  |  |     // TODO remove workers that exit
 | 
					
						
							| 
									
										
										
										
											2016-09-08 16:23:40 -06:00
										 |  |  |     m._workers.push(w); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return m; | 
					
						
							|  |  |  | }; |