IPC.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. 'use strict';
  2. const Defaults = require('../entities/Defaults.js'),
  3. Client = require('../dao/client.js'),
  4. Server = require('../dao/socketServer.js'),
  5. util = require('util');
  6. class IPC{
  7. constructor(){
  8. Object.defineProperties(
  9. this,
  10. {
  11. config : {
  12. enumerable:true,
  13. writable:true,
  14. value:new Defaults
  15. },
  16. connectTo : {
  17. enumerable:true,
  18. writable:false,
  19. value:connect
  20. },
  21. connectToNet: {
  22. enumerable:true,
  23. writable:false,
  24. value:connectNet
  25. },
  26. disconnect : {
  27. enumerable:true,
  28. writable:false,
  29. value:disconnect
  30. },
  31. serve : {
  32. enumerable:true,
  33. writable:false,
  34. value:serve
  35. },
  36. serveNet : {
  37. enumerable:true,
  38. writable:false,
  39. value:serveNet
  40. },
  41. of : {
  42. enumerable:true,
  43. writable:true,
  44. value:{}
  45. },
  46. server : {
  47. enumerable:true,
  48. writable:true,
  49. configurable:true,
  50. value:false
  51. },
  52. log : {
  53. enumerable:true,
  54. writable:false,
  55. value:log
  56. }
  57. }
  58. );
  59. }
  60. }
  61. function log(...args){
  62. if(this.config.silent){
  63. return;
  64. }
  65. for(let i=0, count=args.length; i<count; i++){
  66. if(typeof args[i] != 'object'){
  67. continue;
  68. }
  69. args[i]=util.inspect(
  70. args[i],
  71. {
  72. depth:this.config.logDepth,
  73. colors:this.config.logInColor
  74. }
  75. );
  76. }
  77. this.config.logger(
  78. args.join(' ')
  79. );
  80. }
  81. function disconnect(id){
  82. if(!this.of[id]){
  83. return;
  84. }
  85. this.of[id].explicitlyDisconnected=true;
  86. this.of[id].off('*','*');
  87. if(this.of[id].socket){
  88. if(this.of[id].socket.destroy){
  89. this.of[id].socket.destroy();
  90. }
  91. }
  92. delete this.of[id];
  93. }
  94. function serve(path,callback){
  95. if(typeof path=='function'){
  96. callback=path;
  97. path=false;
  98. }
  99. if(!path){
  100. this.log(
  101. 'Server path not specified, so defaulting to',
  102. 'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id',
  103. this.config.socketRoot+this.config.appspace+this.config.id
  104. );
  105. path=this.config.socketRoot+this.config.appspace+this.config.id;
  106. }
  107. if(!callback){
  108. callback=emptyCallback;
  109. }
  110. this.server=new Server(
  111. path,
  112. this.config,
  113. log
  114. );
  115. this.server.on(
  116. 'start',
  117. callback
  118. );
  119. }
  120. function emptyCallback(){
  121. //Do Nothing
  122. }
  123. function serveNet(host,port,UDPType,callback){
  124. if(typeof host=='number'){
  125. callback=UDPType;
  126. UDPType=port;
  127. port=host;
  128. host=false;
  129. }
  130. if(typeof host=='function'){
  131. callback=host;
  132. UDPType=false;
  133. host=false;
  134. port=false;
  135. }
  136. if(!host){
  137. this.log(
  138. 'Server host not specified, so defaulting to',
  139. 'ipc.config.networkHost',
  140. this.config.networkHost
  141. );
  142. host=this.config.networkHost;
  143. }
  144. if(host.toLowerCase()=='udp4' || host.toLowerCase()=='udp6'){
  145. callback=port;
  146. UDPType=host.toLowerCase();
  147. port=false;
  148. host=this.config.networkHost;
  149. }
  150. if(typeof port=='string'){
  151. callback=UDPType;
  152. UDPType=port;
  153. port=false;
  154. }
  155. if(typeof port=='function'){
  156. callback=port;
  157. UDPType=false;
  158. port=false;
  159. }
  160. if(!port){
  161. this.log(
  162. 'Server port not specified, so defaulting to',
  163. 'ipc.config.networkPort',
  164. this.config.networkPort
  165. );
  166. port=this.config.networkPort;
  167. }
  168. if(typeof UDPType=='function'){
  169. callback=UDPType;
  170. UDPType=false;
  171. }
  172. if(!callback){
  173. callback=emptyCallback;
  174. }
  175. this.server=new Server(
  176. host,
  177. this.config,
  178. log,
  179. port
  180. );
  181. if(UDPType){
  182. this.server[UDPType]=true;
  183. if(UDPType === "udp4" && host === "::1") {
  184. // bind udp4 socket to an ipv4 address
  185. this.server.path = "127.0.0.1";
  186. }
  187. }
  188. this.server.on(
  189. 'start',
  190. callback
  191. );
  192. }
  193. function connect(id,path,callback){
  194. if(typeof path == 'function'){
  195. callback=path;
  196. path=false;
  197. }
  198. if(!callback){
  199. callback=emptyCallback;
  200. }
  201. if(!id){
  202. this.log(
  203. 'Service id required',
  204. 'Requested service connection without specifying service id. Aborting connection attempt'
  205. );
  206. return;
  207. }
  208. if(!path){
  209. this.log(
  210. 'Service path not specified, so defaulting to',
  211. 'ipc.config.socketRoot + ipc.config.appspace + id',
  212. (this.config.socketRoot+this.config.appspace+id).data
  213. );
  214. path=this.config.socketRoot+this.config.appspace+id;
  215. }
  216. if(this.of[id]){
  217. if(!this.of[id].socket.destroyed){
  218. this.log(
  219. 'Already Connected to',
  220. id,
  221. '- So executing success without connection'
  222. );
  223. callback();
  224. return;
  225. }
  226. this.of[id].socket.destroy();
  227. }
  228. this.of[id] = new Client(this.config,this.log);
  229. this.of[id].id = id;
  230. this.of[id].path = path;
  231. this.of[id].connect();
  232. callback(this);
  233. }
  234. function connectNet(id,host,port,callback){
  235. if(!id){
  236. this.log(
  237. 'Service id required',
  238. 'Requested service connection without specifying service id. Aborting connection attempt'
  239. );
  240. return;
  241. }
  242. if(typeof host=='number'){
  243. callback=port;
  244. port=host;
  245. host=false;
  246. }
  247. if(typeof host=='function'){
  248. callback=host;
  249. host=false;
  250. port=false;
  251. }
  252. if(!host){
  253. this.log(
  254. 'Server host not specified, so defaulting to',
  255. 'ipc.config.networkHost',
  256. this.config.networkHost
  257. );
  258. host=this.config.networkHost;
  259. }
  260. if(typeof port=='function'){
  261. callback=port;
  262. port=false;
  263. }
  264. if(!port){
  265. this.log(
  266. 'Server port not specified, so defaulting to',
  267. 'ipc.config.networkPort',
  268. this.config.networkPort
  269. );
  270. port=this.config.networkPort;
  271. }
  272. if(typeof callback == 'string'){
  273. UDPType=callback;
  274. callback=false;
  275. }
  276. if(!callback){
  277. callback=emptyCallback;
  278. }
  279. if(this.of[id]){
  280. if(!this.of[id].socket.destroyed){
  281. this.log(
  282. 'Already Connected to',
  283. id,
  284. '- So executing success without connection'
  285. );
  286. callback();
  287. return;
  288. }
  289. this.of[id].socket.destroy();
  290. }
  291. this.of[id] = new Client(this.config,this.log);
  292. this.of[id].id = id;
  293. (this.of[id].socket)? this.of[id].socket.id=id:null;
  294. this.of[id].path = host;
  295. this.of[id].port = port;
  296. this.of[id].connect();
  297. callback(this);
  298. }
  299. module.exports=IPC;