@bigtyphoon/melo 1.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +55 -0
  3. package/bin/commadtest.ts +10 -0
  4. package/bin/commands/add.ts +43 -0
  5. package/bin/commands/init.ts +292 -0
  6. package/bin/commands/kill.ts +21 -0
  7. package/bin/commands/list.ts +60 -0
  8. package/bin/commands/masterha.ts +40 -0
  9. package/bin/commands/restart.ts +48 -0
  10. package/bin/commands/start.ts +65 -0
  11. package/bin/commands/stop.ts +26 -0
  12. package/bin/melo.ts +30 -0
  13. package/bin/utils/constants.ts +27 -0
  14. package/bin/utils/utils.ts +130 -0
  15. package/dist/bin/commadtest.js +9 -0
  16. package/dist/bin/commands/add.js +40 -0
  17. package/dist/bin/commands/init.js +279 -0
  18. package/dist/bin/commands/kill.js +21 -0
  19. package/dist/bin/commands/list.js +65 -0
  20. package/dist/bin/commands/masterha.js +36 -0
  21. package/dist/bin/commands/restart.js +45 -0
  22. package/dist/bin/commands/start.js +58 -0
  23. package/dist/bin/commands/stop.js +20 -0
  24. package/dist/bin/melo.js +26 -0
  25. package/dist/bin/utils/constants.js +28 -0
  26. package/dist/bin/utils/utils.js +134 -0
  27. package/dist/lib/application.js +888 -0
  28. package/dist/lib/common/manager/appManager.js +112 -0
  29. package/dist/lib/common/manager/taskManager.js +39 -0
  30. package/dist/lib/common/remote/backend/msgRemote.js +63 -0
  31. package/dist/lib/common/remote/frontend/channelRemote.js +78 -0
  32. package/dist/lib/common/remote/frontend/sessionRemote.js +76 -0
  33. package/dist/lib/common/service/backendSessionService.js +337 -0
  34. package/dist/lib/common/service/channelService.js +514 -0
  35. package/dist/lib/common/service/connectionService.js +95 -0
  36. package/dist/lib/common/service/filterService.js +112 -0
  37. package/dist/lib/common/service/handlerService.js +187 -0
  38. package/dist/lib/common/service/sessionService.js +610 -0
  39. package/dist/lib/components/backendSession.js +14 -0
  40. package/dist/lib/components/channel.js +13 -0
  41. package/dist/lib/components/connection.js +12 -0
  42. package/dist/lib/components/connector.js +437 -0
  43. package/dist/lib/components/dictionary.js +93 -0
  44. package/dist/lib/components/master.js +39 -0
  45. package/dist/lib/components/monitor.js +25 -0
  46. package/dist/lib/components/protobuf.js +156 -0
  47. package/dist/lib/components/proxy.js +236 -0
  48. package/dist/lib/components/pushScheduler.js +62 -0
  49. package/dist/lib/components/remote.js +127 -0
  50. package/dist/lib/components/server.js +63 -0
  51. package/dist/lib/components/session.js +20 -0
  52. package/dist/lib/connectors/commands/handshake.js +119 -0
  53. package/dist/lib/connectors/commands/heartbeat.js +67 -0
  54. package/dist/lib/connectors/commands/kick.js +15 -0
  55. package/dist/lib/connectors/common/coder.js +90 -0
  56. package/dist/lib/connectors/common/handler.js +57 -0
  57. package/dist/lib/connectors/hybrid/IHybridSocket.js +3 -0
  58. package/dist/lib/connectors/hybrid/switcher.js +100 -0
  59. package/dist/lib/connectors/hybrid/tcpprocessor.js +40 -0
  60. package/dist/lib/connectors/hybrid/tcpsocket.js +171 -0
  61. package/dist/lib/connectors/hybrid/wsprocessor.js +49 -0
  62. package/dist/lib/connectors/hybridconnector.js +89 -0
  63. package/dist/lib/connectors/hybridsocket.js +139 -0
  64. package/dist/lib/connectors/mqtt/generate.js +113 -0
  65. package/dist/lib/connectors/mqtt/mqttadaptor.js +81 -0
  66. package/dist/lib/connectors/mqtt/protocol.js +48 -0
  67. package/dist/lib/connectors/mqttconnector.js +107 -0
  68. package/dist/lib/connectors/mqttsocket.js +59 -0
  69. package/dist/lib/connectors/sioconnector.js +135 -0
  70. package/dist/lib/connectors/siosocket.js +69 -0
  71. package/dist/lib/connectors/udpconnector.js +76 -0
  72. package/dist/lib/connectors/udpsocket.js +93 -0
  73. package/dist/lib/filters/handler/serial.js +44 -0
  74. package/dist/lib/filters/handler/time.js +32 -0
  75. package/dist/lib/filters/handler/timeout.js +45 -0
  76. package/dist/lib/filters/handler/toobusy.js +36 -0
  77. package/dist/lib/filters/rpc/rpcLog.js +43 -0
  78. package/dist/lib/filters/rpc/toobusy.js +41 -0
  79. package/dist/lib/index.js +81 -0
  80. package/dist/lib/interfaces/IComponent.js +3 -0
  81. package/dist/lib/interfaces/IConnector.js +3 -0
  82. package/dist/lib/interfaces/IHandlerFilter.js +3 -0
  83. package/dist/lib/interfaces/ILifeCycle.js +3 -0
  84. package/dist/lib/interfaces/IPlugin.js +3 -0
  85. package/dist/lib/interfaces/IPushScheduler.js +3 -0
  86. package/dist/lib/interfaces/ISocket.js +3 -0
  87. package/dist/lib/interfaces/IStore.js +3 -0
  88. package/dist/lib/interfaces/define.js +3 -0
  89. package/dist/lib/master/master.js +129 -0
  90. package/dist/lib/master/starter.js +236 -0
  91. package/dist/lib/master/watchdog.js +120 -0
  92. package/dist/lib/melo.js +125 -0
  93. package/dist/lib/modules/console.js +436 -0
  94. package/dist/lib/modules/masterwatcher.js +98 -0
  95. package/dist/lib/modules/monitorwatcher.js +124 -0
  96. package/dist/lib/modules/onlineUser.js +69 -0
  97. package/dist/lib/modules/restartNotifyModule.js +107 -0
  98. package/dist/lib/modules/watchServer.js +737 -0
  99. package/dist/lib/monitor/monitor.js +80 -0
  100. package/dist/lib/pushSchedulers/buffer.js +96 -0
  101. package/dist/lib/pushSchedulers/direct.js +58 -0
  102. package/dist/lib/pushSchedulers/multi.js +80 -0
  103. package/dist/lib/server/server.js +500 -0
  104. package/dist/lib/util/appUtil.js +306 -0
  105. package/dist/lib/util/constants.js +117 -0
  106. package/dist/lib/util/countDownLatch.js +51 -0
  107. package/dist/lib/util/events.js +20 -0
  108. package/dist/lib/util/handlerHelper.js +8 -0
  109. package/dist/lib/util/log.js +14 -0
  110. package/dist/lib/util/moduleUtil.js +101 -0
  111. package/dist/lib/util/pathUtil.js +134 -0
  112. package/dist/lib/util/remoterHelper.js +8 -0
  113. package/dist/lib/util/utils.js +358 -0
  114. package/dist/test/application.js +522 -0
  115. package/dist/test/config/log4js.json +28 -0
  116. package/dist/test/config/master.json +9 -0
  117. package/dist/test/config/servers.json +6 -0
  118. package/dist/test/filters/handler/serial.js +41 -0
  119. package/dist/test/filters/handler/time.js +41 -0
  120. package/dist/test/filters/handler/timeout.js +41 -0
  121. package/dist/test/filters/handler/toobusy.js +57 -0
  122. package/dist/test/filters/rpc/rpcLog.js +22 -0
  123. package/dist/test/filters/rpc/toobusy.js +39 -0
  124. package/dist/test/manager/mockChannelManager.js +77 -0
  125. package/dist/test/manager/taskManager.js +68 -0
  126. package/dist/test/mock-base/app/servers/other-file +0 -0
  127. package/dist/test/mock-plugin/components/mockPlugin.js +10 -0
  128. package/dist/test/mock-plugin/events/mockEvent.js +12 -0
  129. package/dist/test/modules/console.js +242 -0
  130. package/dist/test/pomelo.js +19 -0
  131. package/dist/test/remote/channelRemote.js +159 -0
  132. package/dist/test/service/channel.js +134 -0
  133. package/dist/test/service/channelService.js +216 -0
  134. package/dist/test/service/connectionService.js +114 -0
  135. package/dist/test/service/filterService.js +144 -0
  136. package/dist/test/service/handlerService.js +65 -0
  137. package/dist/test/service/sessionService.js +387 -0
  138. package/dist/test/util/countDownLatch.js +70 -0
  139. package/dist/test/util/pathUtil.js +108 -0
  140. package/dist/test/util/utils.js +140 -0
  141. package/lib/application.ts +1240 -0
  142. package/lib/common/manager/appManager.ts +118 -0
  143. package/lib/common/manager/taskManager.ts +50 -0
  144. package/lib/common/remote/backend/msgRemote.ts +134 -0
  145. package/lib/common/remote/frontend/channelRemote.ts +91 -0
  146. package/lib/common/remote/frontend/sessionRemote.ts +91 -0
  147. package/lib/common/service/backendSessionService.ts +388 -0
  148. package/lib/common/service/channelService.ts +609 -0
  149. package/lib/common/service/connectionService.ts +112 -0
  150. package/lib/common/service/filterService.ts +118 -0
  151. package/lib/common/service/handlerService.ts +224 -0
  152. package/lib/common/service/sessionService.ts +731 -0
  153. package/lib/components/backendSession.ts +14 -0
  154. package/lib/components/channel.ts +11 -0
  155. package/lib/components/connection.ts +13 -0
  156. package/lib/components/connector.ts +533 -0
  157. package/lib/components/dictionary.ts +121 -0
  158. package/lib/components/master.ts +41 -0
  159. package/lib/components/monitor.ts +30 -0
  160. package/lib/components/protobuf.ts +208 -0
  161. package/lib/components/proxy.ts +282 -0
  162. package/lib/components/pushScheduler.ts +70 -0
  163. package/lib/components/remote.ts +166 -0
  164. package/lib/components/server.ts +71 -0
  165. package/lib/components/session.ts +22 -0
  166. package/lib/connectors/commands/handshake.ts +155 -0
  167. package/lib/connectors/commands/heartbeat.ts +83 -0
  168. package/lib/connectors/commands/kick.ts +11 -0
  169. package/lib/connectors/common/coder.ts +93 -0
  170. package/lib/connectors/common/handler.ts +62 -0
  171. package/lib/connectors/hybrid/IHybridSocket.ts +9 -0
  172. package/lib/connectors/hybrid/switcher.ts +142 -0
  173. package/lib/connectors/hybrid/tcpprocessor.ts +43 -0
  174. package/lib/connectors/hybrid/tcpsocket.ts +223 -0
  175. package/lib/connectors/hybrid/wsprocessor.ts +57 -0
  176. package/lib/connectors/hybridconnector.ts +134 -0
  177. package/lib/connectors/hybridsocket.ts +168 -0
  178. package/lib/connectors/mqtt/generate.ts +103 -0
  179. package/lib/connectors/mqtt/mqttadaptor.ts +114 -0
  180. package/lib/connectors/mqtt/protocol.ts +49 -0
  181. package/lib/connectors/mqttconnector.ts +134 -0
  182. package/lib/connectors/mqttsocket.ts +79 -0
  183. package/lib/connectors/sioconnector.ts +161 -0
  184. package/lib/connectors/siosocket.ts +85 -0
  185. package/lib/connectors/udpconnector.ts +113 -0
  186. package/lib/connectors/udpsocket.ts +110 -0
  187. package/lib/filters/handler/serial.ts +46 -0
  188. package/lib/filters/handler/time.ts +35 -0
  189. package/lib/filters/handler/timeout.ts +50 -0
  190. package/lib/filters/handler/toobusy.ts +37 -0
  191. package/lib/filters/rpc/rpcLog.ts +42 -0
  192. package/lib/filters/rpc/toobusy.ts +41 -0
  193. package/lib/index.ts +74 -0
  194. package/lib/interfaces/IComponent.ts +47 -0
  195. package/lib/interfaces/IConnector.ts +20 -0
  196. package/lib/interfaces/IHandlerFilter.ts +15 -0
  197. package/lib/interfaces/ILifeCycle.ts +16 -0
  198. package/lib/interfaces/IPlugin.ts +65 -0
  199. package/lib/interfaces/IPushScheduler.ts +52 -0
  200. package/lib/interfaces/ISocket.ts +26 -0
  201. package/lib/interfaces/IStore.ts +10 -0
  202. package/lib/interfaces/define.ts +15 -0
  203. package/lib/master/master.ts +148 -0
  204. package/lib/master/starter.ts +234 -0
  205. package/lib/master/watchdog.ts +135 -0
  206. package/lib/melo.ts +152 -0
  207. package/lib/modules/console.ts +465 -0
  208. package/lib/modules/masterwatcher.ts +120 -0
  209. package/lib/modules/monitorwatcher.ts +151 -0
  210. package/lib/modules/onlineUser.ts +78 -0
  211. package/lib/modules/restartNotifyModule.ts +128 -0
  212. package/lib/modules/watchServer.ts +766 -0
  213. package/lib/monitor/monitor.ts +99 -0
  214. package/lib/pushSchedulers/buffer.ts +117 -0
  215. package/lib/pushSchedulers/direct.ts +65 -0
  216. package/lib/pushSchedulers/multi.ts +94 -0
  217. package/lib/server/server.ts +554 -0
  218. package/lib/util/appUtil.ts +313 -0
  219. package/lib/util/constants.ts +154 -0
  220. package/lib/util/countDownLatch.ts +72 -0
  221. package/lib/util/events.ts +15 -0
  222. package/lib/util/handlerHelper.ts +5 -0
  223. package/lib/util/log.ts +11 -0
  224. package/lib/util/moduleUtil.ts +110 -0
  225. package/lib/util/pathUtil.ts +132 -0
  226. package/lib/util/remoterHelper.ts +68 -0
  227. package/lib/util/utils.ts +365 -0
  228. package/package.json +93 -0
  229. package/template/game-server/.vscode/launch.json +27 -0
  230. package/template/game-server/app/servers/connector/handler/entryHandler.ts +50 -0
  231. package/template/game-server/app/servers/connector/remote/authRemoter.ts +36 -0
  232. package/template/game-server/app.ts +30 -0
  233. package/template/game-server/app.ts.mqtt +32 -0
  234. package/template/game-server/app.ts.sio +36 -0
  235. package/template/game-server/app.ts.sio.wss +34 -0
  236. package/template/game-server/app.ts.udp +31 -0
  237. package/template/game-server/app.ts.wss +40 -0
  238. package/template/game-server/config/adminServer.json +5 -0
  239. package/template/game-server/config/adminUser.json +22 -0
  240. package/template/game-server/config/clientProtos.json +1 -0
  241. package/template/game-server/config/dictionary.json +1 -0
  242. package/template/game-server/config/log4js.json +150 -0
  243. package/template/game-server/config/master.json +8 -0
  244. package/template/game-server/config/serverProtos.json +1 -0
  245. package/template/game-server/config/servers.json +12 -0
  246. package/template/game-server/copy.js +5 -0
  247. package/template/game-server/package.json +28 -0
  248. package/template/game-server/preload.ts +40 -0
  249. package/template/game-server/tsconfig.json +36 -0
  250. package/template/npm-install.bat +4 -0
  251. package/template/npm-install.sh +5 -0
  252. package/template/readme.md +31 -0
  253. package/template/shared/server.crt +15 -0
  254. package/template/shared/server.key +15 -0
  255. package/template/web-server/app.js +25 -0
  256. package/template/web-server/app.js.https +36 -0
  257. package/template/web-server/bin/component.bat +1 -0
  258. package/template/web-server/bin/component.sh +1 -0
  259. package/template/web-server/package.json +10 -0
  260. package/template/web-server/public/css/base.css +76 -0
  261. package/template/web-server/public/image/logo.png +0 -0
  262. package/template/web-server/public/image/sp.png +0 -0
  263. package/template/web-server/public/index.html +57 -0
  264. package/template/web-server/public/index.html.sio +58 -0
  265. package/template/web-server/public/js/lib/build/build.js +1730 -0
  266. package/template/web-server/public/js/lib/build/build.js.wss +1721 -0
  267. package/template/web-server/public/js/lib/component.json +6 -0
  268. package/template/web-server/public/js/lib/local/boot/component.json +11 -0
  269. package/template/web-server/public/js/lib/local/boot/index.js +11 -0
  270. package/template/web-server/public/js/lib/meloclient.js +456 -0
  271. package/template/web-server/public/js/lib/meloclient.js.wss +456 -0
  272. package/template/web-server/public/js/lib/socket.io.js +3 -0
  273. package/test/application.ts +607 -0
  274. package/test/filters/handler/serial.ts +47 -0
  275. package/test/filters/handler/time.ts +47 -0
  276. package/test/filters/handler/timeout.ts +46 -0
  277. package/test/filters/handler/toobusy.ts +59 -0
  278. package/test/filters/rpc/rpcLog.ts +20 -0
  279. package/test/filters/rpc/toobusy.ts +40 -0
  280. package/test/manager/mockChannelManager.ts +92 -0
  281. package/test/manager/taskManager.ts +78 -0
  282. package/test/mock-base/app/servers/other-file +0 -0
  283. package/test/mock-plugin/components/mockPlugin.ts +8 -0
  284. package/test/mock-plugin/events/mockEvent.ts +12 -0
  285. package/test/modules/console.ts +264 -0
  286. package/test/pomelo.ts +18 -0
  287. package/test/remote/channelRemote.ts +176 -0
  288. package/test/service/channel.ts +161 -0
  289. package/test/service/channelService.ts +243 -0
  290. package/test/service/connectionService.ts +142 -0
  291. package/test/service/filterService.ts +165 -0
  292. package/test/service/handlerService.ts +77 -0
  293. package/test/service/sessionService.ts +464 -0
  294. package/test/util/countDownLatch.ts +81 -0
  295. package/test/util/pathUtil.ts +122 -0
  296. package/test/util/utils.ts +165 -0
@@ -0,0 +1,766 @@
1
+ /*!
2
+ * Melo -- consoleModule watchServer
3
+ * Copyright(c) 2013 fantasyni <fantasyni@163.com>
4
+ * MIT Licensed
5
+ */
6
+ import { getLogger } from '@bigtyphoon/melo-logger';
7
+ import * as countDownLatch from '../util/countDownLatch';
8
+ import * as monitor from '@bigtyphoon/melo-monitor';
9
+ import * as utils from '../util/utils';
10
+ import * as util from 'util';
11
+ import * as fs from 'fs';
12
+ import * as vm from 'vm';
13
+ import { IModule, MonitorCallback, MasterCallback, ModuleType , MonitorAgent, MasterAgent } from '@bigtyphoon/melo-admin';
14
+ import { ServerInfo } from '../util/constants';
15
+ import { Application } from '../application';
16
+ import * as path from 'path';
17
+ let logger = getLogger('melo', path.basename(__filename));
18
+
19
+
20
+ enum HandleType {
21
+ client = 'client',
22
+ monitor = 'monitor'
23
+ }
24
+
25
+ export class WatchServerModule implements IModule {
26
+ static moduleId = 'watchServer';
27
+
28
+ app: Application;
29
+ constructor(opts ?: {app ?: Application}) {
30
+ opts = opts || {};
31
+ this.app = opts.app;
32
+ }
33
+
34
+ monitorHandler(agent: MonitorAgent, msg: any, cb: MonitorCallback) {
35
+ let comd = msg['comd'];
36
+ let context = msg['context'];
37
+ let param = msg['param'];
38
+ let app = this.app;
39
+
40
+ let handle = HandleType.monitor;
41
+
42
+ switch (comd) {
43
+ case 'servers':
44
+ showServers(handle, agent, comd, context, cb);
45
+ break;
46
+ case 'connections':
47
+ showConnections(handle, agent, app, comd, context, cb);
48
+ break;
49
+ case 'logins':
50
+ showLogins(handle, agent, app, comd, context, cb);
51
+ break;
52
+ case 'modules':
53
+ showModules(handle, agent, comd, context, cb);
54
+ break;
55
+ case 'status':
56
+ showStatus(handle, agent, comd, context, cb);
57
+ break;
58
+ case 'config':
59
+ showConfig(handle, agent, app, comd, context, param, cb);
60
+ break;
61
+ case 'proxy':
62
+ showProxy(handle, agent, app, comd, context, param, cb);
63
+ break;
64
+ case 'handler':
65
+ showHandler(handle, agent, app, comd, context, param, cb);
66
+ break;
67
+ case 'components':
68
+ showComponents(handle, agent, app, comd, context, param, cb);
69
+ break;
70
+ case 'settings':
71
+ showSettings(handle, agent, app, comd, context, param, cb);
72
+ break;
73
+ case 'cpu':
74
+ dumpCPU(handle, agent, comd, context, param, cb);
75
+ break;
76
+ case 'memory':
77
+ dumpMemory(handle, agent, comd, context, param, cb);
78
+ break;
79
+ case 'get':
80
+ getApp(handle, agent, app, comd, context, param, cb);
81
+ break;
82
+ case 'set':
83
+ setApp(handle, agent, app, comd, context, param, cb);
84
+ break;
85
+ case 'enable':
86
+ enableApp(handle, agent, app, comd, context, param, cb);
87
+ break;
88
+ case 'disable':
89
+ disableApp(handle, agent, app, comd, context, param, cb);
90
+ break;
91
+ case 'run':
92
+ runScript(handle, agent, app, comd, context, param, cb);
93
+ break;
94
+ default:
95
+ showError(handle, agent, comd, context, cb);
96
+ }
97
+ }
98
+
99
+ clientHandler(agent: MasterAgent, msg: any, cb: MasterCallback) {
100
+ let comd = msg['comd'];
101
+ let context = msg['context'];
102
+ let param = msg['param'];
103
+ let app = this.app; // master app
104
+
105
+ if (!comd || !context) {
106
+ cb('lack of comd or context param');
107
+ return;
108
+ }
109
+
110
+ let handle = HandleType.client;
111
+ switch (comd) {
112
+ case 'servers':
113
+ showServers(handle, agent, comd, context, cb);
114
+ break;
115
+ case 'connections':
116
+ showConnections(handle, agent, app, comd, context, cb);
117
+ break;
118
+ case 'logins':
119
+ showLogins(handle, agent, app, comd, context, cb);
120
+ break;
121
+ case 'modules':
122
+ showModules(handle, agent, comd, context, cb);
123
+ break;
124
+ case 'status':
125
+ showStatus(handle, agent, comd, context, cb);
126
+ break;
127
+ case 'config':
128
+ showConfig(handle, agent, app, comd, context, param, cb);
129
+ break;
130
+ case 'proxy':
131
+ showProxy(handle, agent, app, comd, context, param, cb);
132
+ break;
133
+ case 'handler':
134
+ showHandler(handle, agent, app, comd, context, param, cb);
135
+ break;
136
+ case 'components':
137
+ showComponents(handle, agent, app, comd, context, param, cb);
138
+ break;
139
+ case 'settings':
140
+ showSettings(handle, agent, app, comd, context, param, cb);
141
+ break;
142
+ case 'cpu':
143
+ dumpCPU(handle, agent, comd, context, param, cb);
144
+ break;
145
+ case 'memory':
146
+ dumpMemory(handle, agent, comd, context, param, cb);
147
+ break;
148
+ case 'get':
149
+ getApp(handle, agent, app, comd, context, param, cb);
150
+ break;
151
+ case 'set':
152
+ setApp(handle, agent, app, comd, context, param, cb);
153
+ break;
154
+ case 'enable':
155
+ enableApp(handle, agent, app, comd, context, param, cb);
156
+ break;
157
+ case 'disable':
158
+ disableApp(handle, agent, app, comd, context, param, cb);
159
+ break;
160
+ case 'run':
161
+ runScript(handle, agent, app, comd, context, param, cb);
162
+ break;
163
+ default:
164
+ showError(handle, agent, comd, context, cb);
165
+ }
166
+ }
167
+ }
168
+
169
+ function showServers(handle: HandleType, agent_: MonitorAgent | MasterAgent, comd: string, context: string , cb: (err?: Error | string , data?: any) => void) {
170
+ if (handle === 'client') {
171
+ let agent = agent_ as MasterAgent;
172
+ let sid, record;
173
+ let serverInfo: any = {};
174
+ let count = Object.keys(agent.idMap).length;
175
+ let latch = countDownLatch.createCountDownLatch(count, function () {
176
+ cb(null, {
177
+ msg: serverInfo
178
+ });
179
+ });
180
+
181
+ for (sid in agent.idMap) {
182
+ record = agent.idMap[sid];
183
+ agent.request(record.id, WatchServerModule.moduleId, {
184
+ comd: comd,
185
+ context: context
186
+ }, function (err , msg) {
187
+ serverInfo[msg.serverId] = msg.body;
188
+ latch.done();
189
+ });
190
+ }
191
+ } else if (handle === 'monitor') {
192
+ let agent = agent_ as MonitorAgent;
193
+ let serverId = agent.id;
194
+ let serverType = agent.type;
195
+ let info = agent.info;
196
+ let pid = process.pid;
197
+ let heapUsed = (process.memoryUsage().heapUsed / (1000 * 1000)).toFixed(2);
198
+ let uptime = (process.uptime() / 60).toFixed(2);
199
+ cb(null, {
200
+ serverId: serverId,
201
+ body: {
202
+ serverId: serverId,
203
+ serverType: serverType,
204
+ host: info['host'],
205
+ port: info['port'],
206
+ pid: pid,
207
+ heapUsed: heapUsed,
208
+ uptime: uptime
209
+ }
210
+ });
211
+ }
212
+
213
+ }
214
+
215
+ function showConnections(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application , comd: string, context: string , cb: (err?: Error | string , data?: any) => void) {
216
+ if (handle === 'client') {
217
+ let agent = _agent as MasterAgent;
218
+ if (context === 'all') {
219
+ let sid, record;
220
+ let serverInfo: any = {};
221
+ let count = 0;
222
+ for (let key in agent.idMap) {
223
+ if ((agent.idMap[key].info as ServerInfo).frontend) {
224
+ count++;
225
+ }
226
+ }
227
+ let latch = countDownLatch.createCountDownLatch(count, function () {
228
+ cb(null, {
229
+ msg: serverInfo
230
+ });
231
+ });
232
+
233
+ for (sid in agent.idMap) {
234
+ record = agent.idMap[sid];
235
+ if ((record.info as ServerInfo).frontend) {
236
+ agent.request(record.id, WatchServerModule.moduleId, {
237
+ comd: comd,
238
+ context: context
239
+ }, function (err , msg) {
240
+ serverInfo[msg.serverId] = msg.body;
241
+ latch.done();
242
+ });
243
+ }
244
+ }
245
+ } else {
246
+ let record = agent.idMap[context];
247
+ if (!record) {
248
+ cb('the server ' + context + ' not exist');
249
+ }
250
+ if ((record.info as ServerInfo).frontend) {
251
+ agent.request(record.id, WatchServerModule.moduleId, {
252
+ comd: comd,
253
+ context: context
254
+ }, function (err , msg) {
255
+ let serverInfo: any = {};
256
+ serverInfo[msg.serverId] = msg.body;
257
+ cb(null, {
258
+ msg: serverInfo
259
+ });
260
+ });
261
+ } else {
262
+ cb('\nthis command should be applied to frontend server\n');
263
+ }
264
+ }
265
+ } else if (handle === 'monitor') {
266
+ let agent = _agent as MonitorAgent;
267
+ let connection = app.components.__connection__;
268
+ if (!connection) {
269
+ cb(null , {
270
+ serverId: agent.id,
271
+ body: 'error'
272
+ });
273
+ return;
274
+ }
275
+
276
+ cb(null , {
277
+ serverId: agent.id,
278
+ body: connection.getStatisticsInfo()
279
+ });
280
+ }
281
+ }
282
+
283
+
284
+ function showLogins(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , cb: (err?: Error | string , data?: any) => void) {
285
+ showConnections(handle, _agent, app, comd, context, cb);
286
+ }
287
+
288
+ function showModules(handle: HandleType, _agent: MonitorAgent | MasterAgent, comd: string, context: string , cb: (err?: Error | string , data?: any) => void) {
289
+ let modules = _agent.consoleService.modules;
290
+ let result = [];
291
+ for (let module in modules) {
292
+ result.push(module);
293
+ }
294
+ cb(null, {
295
+ msg: result
296
+ });
297
+ }
298
+
299
+ function showStatus(handle: HandleType, _agent: MonitorAgent | MasterAgent, comd: string, context: string , cb: (err?: Error | string , data?: any) => void) {
300
+ if (handle === 'client') {
301
+ let agent = _agent as MasterAgent;
302
+ agent.request(context, WatchServerModule.moduleId, {
303
+ comd: comd,
304
+ context: context
305
+ }, function (err, msg) {
306
+ cb(null, {
307
+ msg: msg
308
+ });
309
+ });
310
+ } else if (handle === 'monitor') {
311
+ let agent = _agent as MonitorAgent;
312
+ let serverId = agent.id;
313
+ let pid = process.pid;
314
+ let params = {
315
+ serverId: serverId,
316
+ pid: String(pid)
317
+ };
318
+ monitor.psmonitor.getPsInfo(params, function (err: Error, data: any) {
319
+ cb(null, {
320
+ serverId: agent.id,
321
+ body: data
322
+ });
323
+ });
324
+ }
325
+ }
326
+
327
+ function showConfig(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string, cb: (err?: Error | string , data?: any) => void) {
328
+ if (handle === 'client') {
329
+ let agent = _agent as MasterAgent;
330
+ if (param === 'master') {
331
+ cb(null, {
332
+ masterConfig: app.get('masterConfig') || 'no config to master in app.js',
333
+ masterInfo: app.get('master')
334
+ });
335
+ return;
336
+ }
337
+
338
+ agent.request(context, WatchServerModule.moduleId, {
339
+ comd: comd,
340
+ param: param,
341
+ context: context
342
+ }, cb);
343
+ } else if (handle === 'monitor') {
344
+ let key = param + 'Config';
345
+ cb(null, clone(param, app.get(key)));
346
+ }
347
+ }
348
+
349
+ function showProxy(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string, param: string , cb: (err?: Error | string , data?: any) => void) {
350
+ if (handle === 'client') {
351
+ let agent = _agent as MasterAgent;
352
+ if (context === 'all') {
353
+ cb('context error');
354
+ return;
355
+ }
356
+
357
+ agent.request(context, WatchServerModule.moduleId, {
358
+ comd: comd,
359
+ param: param,
360
+ context: context
361
+ }, function (err, msg) {
362
+ cb(null, msg);
363
+ });
364
+ } else if (handle === 'monitor') {
365
+ let agent = _agent as MonitorAgent;
366
+ proxyCb(app, context, cb);
367
+ }
368
+ }
369
+
370
+ function showHandler(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string , cb: (err?: Error | string , data?: any) => void) {
371
+ if (handle === 'client') {
372
+ let agent = _agent as MasterAgent;
373
+ if (context === 'all') {
374
+ cb('context error');
375
+ return;
376
+ }
377
+
378
+ agent.request(context, WatchServerModule.moduleId, {
379
+ comd: comd,
380
+ param: param,
381
+ context: context
382
+ }, function (err, msg) {
383
+ cb(null, msg);
384
+ });
385
+ } else if (handle === 'monitor') {
386
+ let agent = _agent as MonitorAgent;
387
+ handlerCb(app, context, cb);
388
+ }
389
+ }
390
+
391
+ function showComponents(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string, cb: (err?: Error | string , data?: any) => void) {
392
+ if (handle === 'client') {
393
+ let agent = _agent as MasterAgent;
394
+ if (context === 'all') {
395
+ cb('context error');
396
+ return;
397
+ }
398
+
399
+ agent.request(context, WatchServerModule.moduleId, {
400
+ comd: comd,
401
+ param: param,
402
+ context: context
403
+ }, function (err, msg) {
404
+ cb(null, msg);
405
+ });
406
+ } else if (handle === 'monitor') {
407
+ let agent = _agent as MonitorAgent;
408
+ let _components = app.components;
409
+ let res: any = {};
410
+ for (let key in _components) {
411
+ let name = getComponentName(key);
412
+ res[name] = clone(name, app.get(name + 'Config'));
413
+ }
414
+ cb(null, res);
415
+ }
416
+ }
417
+
418
+ function showSettings(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string , cb: (err?: Error | string , data?: any) => void) {
419
+ if (handle === 'client') {
420
+ let agent = _agent as MasterAgent;
421
+ if (context === 'all') {
422
+ cb('context error');
423
+ return;
424
+ }
425
+
426
+ agent.request(context, WatchServerModule.moduleId, {
427
+ comd: comd,
428
+ param: param,
429
+ context: context
430
+ }, function (err, msg) {
431
+ cb(null, msg);
432
+ });
433
+ } else if (handle === 'monitor') {
434
+ let agent = _agent as MonitorAgent;
435
+ let _settings = app.settings;
436
+ let res: any = {};
437
+ for (let key in _settings) {
438
+ if (key.match(/^__\w+__$/) || key.match(/\w+Config$/)) {
439
+ continue;
440
+ }
441
+ if (!checkJSON(_settings[key])) {
442
+ res[key] = 'Object';
443
+ continue;
444
+ }
445
+ res[key] = _settings[key];
446
+ }
447
+ cb(null, res);
448
+ }
449
+ }
450
+
451
+ function dumpCPU(handle: HandleType, _agent: MonitorAgent | MasterAgent, comd: string, context: string , param: {times: number , filepath: string, force: boolean} , cb: (err?: Error | string , data?: any) => void) {
452
+ if (handle === 'client') {
453
+ let agent = _agent as MasterAgent;
454
+ if (context === 'all') {
455
+ cb('context error');
456
+ return;
457
+ }
458
+
459
+ agent.request(context, WatchServerModule.moduleId, {
460
+ comd: comd,
461
+ param: param,
462
+ context: context
463
+ }, function (err, msg) {
464
+ cb(err, msg);
465
+ });
466
+ } else if (handle === 'monitor') {
467
+ let agent = _agent as MonitorAgent;
468
+ let times = param['times'];
469
+ let filepath = param['filepath'];
470
+ let force = param['force'];
471
+ cb(null, 'cpu dump is unused in 1.0 of melo');
472
+ /**
473
+ if (!/\.cpuprofile$/.test(filepath)) {
474
+ filepath = filepath + '.cpuprofile';
475
+ }
476
+ if (!times || !/^[0-9]*[1-9][0-9]*$/.test(times)) {
477
+ cb('no times or times invalid error');
478
+ return;
479
+ }
480
+ checkFilePath(filepath, force, function(err) {
481
+ if (err) {
482
+ cb(err);
483
+ return;
484
+ }
485
+ //ndump.cpu(filepath, times);
486
+ cb(null, filepath + ' cpu dump ok');
487
+ });
488
+ */
489
+
490
+ }
491
+ }
492
+
493
+
494
+ function dumpMemory(handle: HandleType, _agent: MonitorAgent | MasterAgent, comd: string, context: string , param: {filepath: string, force: boolean} , cb: (err?: Error | string , data?: any) => void) {
495
+ if (handle === 'client') {
496
+ let agent = _agent as MasterAgent;
497
+ if (context === 'all') {
498
+ cb('context error');
499
+ return;
500
+ }
501
+
502
+ agent.request(context, WatchServerModule.moduleId, {
503
+ comd: comd,
504
+ param: param,
505
+ context: context
506
+ }, function (err, msg) {
507
+ cb(err, msg);
508
+ });
509
+ } else if (handle === 'monitor') {
510
+ let agent = _agent as MonitorAgent;
511
+ let filepath = param['filepath'];
512
+ let force = param['force'];
513
+ if (!/\.heapsnapshot$/.test(filepath)) {
514
+ filepath = filepath + '.heapsnapshot';
515
+ }
516
+ checkFilePath(filepath, force, function (err: string) {
517
+ if (err) {
518
+ cb(err);
519
+ return;
520
+ }
521
+ let heapdump = null;
522
+ try {
523
+ heapdump = require('heapdump');
524
+ heapdump.writeSnapshot(filepath);
525
+ cb(null, filepath + ' memory dump ok');
526
+ } catch (e) {
527
+ cb('melo-admin require heapdump');
528
+ }
529
+ });
530
+ }
531
+ }
532
+
533
+ function getApp(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string , cb: (err?: Error | string , data?: any) => void) {
534
+ if (handle === 'client') {
535
+ let agent = _agent as MasterAgent;
536
+ if (context === 'all') {
537
+ cb('context error');
538
+ return;
539
+ }
540
+
541
+ agent.request(context, WatchServerModule.moduleId, {
542
+ comd: comd,
543
+ param: param,
544
+ context: context
545
+ }, function (err, msg) {
546
+ cb(null, msg);
547
+ });
548
+ } else if (handle === 'monitor') {
549
+ let agent = _agent as MonitorAgent;
550
+ let res = app.get(param);
551
+ if (!checkJSON(res)) {
552
+ res = 'object';
553
+ }
554
+ cb(null, res || null);
555
+ }
556
+ }
557
+
558
+ function setApp(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: {key: string, value: any}, cb: (err?: Error | string , data?: any) => void) {
559
+ if (handle === 'client') {
560
+ let agent = _agent as MasterAgent;
561
+ if (context === 'all') {
562
+ cb('context error');
563
+ return;
564
+ }
565
+
566
+ agent.request(context, WatchServerModule.moduleId, {
567
+ comd: comd,
568
+ param: param,
569
+ context: context
570
+ }, function (err, msg) {
571
+ cb(null, msg);
572
+ });
573
+ } else if (handle === 'monitor') {
574
+ let agent = _agent as MonitorAgent;
575
+ let key = param['key'];
576
+ let value = param['value'];
577
+ app.set(key, value);
578
+ cb(null, 'set ' + key + ':' + value + ' ok');
579
+ }
580
+ }
581
+
582
+ function enableApp(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string, cb: (err?: Error | string , data?: any) => void) {
583
+ if (handle === 'client') {
584
+ let agent = _agent as MasterAgent;
585
+ if (context === 'all') {
586
+ cb('context error');
587
+ return;
588
+ }
589
+
590
+ agent.request(context, WatchServerModule.moduleId, {
591
+ comd: comd,
592
+ param: param,
593
+ context: context
594
+ }, function (err, msg) {
595
+ cb(null, msg);
596
+ });
597
+ } else if (handle === 'monitor') {
598
+ let agent = _agent as MonitorAgent;
599
+ app.enable(param);
600
+ cb(null, 'enable ' + param + ' ok');
601
+ }
602
+ }
603
+
604
+ function disableApp(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string, cb: (err?: Error | string , data?: any) => void) {
605
+ if (handle === 'client') {
606
+ let agent = _agent as MasterAgent;
607
+ if (context === 'all') {
608
+ cb('context error');
609
+ return;
610
+ }
611
+
612
+ agent.request(context, WatchServerModule.moduleId, {
613
+ comd: comd,
614
+ param: param,
615
+ context: context
616
+ }, function (err, msg) {
617
+ cb(null, msg);
618
+ });
619
+ } else if (handle === 'monitor') {
620
+ let agent = _agent as MonitorAgent;
621
+ app.disable(param);
622
+ cb(null, 'disable ' + param + ' ok');
623
+ }
624
+ }
625
+
626
+ function runScript(handle: HandleType, _agent: MonitorAgent | MasterAgent, app: Application, comd: string, context: string , param: string , cb: (err?: Error | string , data?: any) => void) {
627
+ if (handle === 'client') {
628
+ let agent = _agent as MasterAgent;
629
+ if (context === 'all') {
630
+ cb('context error');
631
+ return;
632
+ }
633
+
634
+ agent.request(context, WatchServerModule.moduleId, {
635
+ comd: comd,
636
+ param: param,
637
+ context: context
638
+ }, function (err, msg) {
639
+ cb(null, msg);
640
+ });
641
+ } else if (handle === 'monitor') {
642
+ let agent = _agent as MonitorAgent;
643
+ let ctx = {
644
+ app: app,
645
+ result: null as any
646
+ };
647
+ try {
648
+ vm.createContext(ctx);
649
+ vm.runInNewContext('result = ' + param, ctx);
650
+ cb(null, util.inspect(ctx.result));
651
+ } catch (e) {
652
+ cb(null, e.stack);
653
+ }
654
+ }
655
+ }
656
+
657
+ function showError(handle: HandleType, _agent: MonitorAgent | MasterAgent, comd: string, context: string , cb: (err?: Error | string , data?: any) => void) {
658
+
659
+ }
660
+
661
+ function clone(param: any, obj: any) {
662
+ let result: any = {};
663
+ let flag = 1;
664
+ for (let key in obj) {
665
+ if (typeof obj[key] === 'function' || typeof obj[key] === 'object') {
666
+ continue;
667
+ }
668
+ flag = 0;
669
+ result[key] = obj[key];
670
+ }
671
+ if (flag) {
672
+ // return 'no ' + param + 'Config info';
673
+ }
674
+ return result;
675
+ }
676
+
677
+ function checkFilePath(filepath: string, force: boolean, cb: (result: string) => void) {
678
+ if (!force && fs.existsSync(filepath)) {
679
+ cb('filepath file exist');
680
+ return;
681
+ }
682
+ fs.writeFile(filepath, 'test', function (err) {
683
+ if (err) {
684
+ cb('filepath invalid error');
685
+ return;
686
+ }
687
+ fs.unlinkSync(filepath);
688
+ cb(null);
689
+ });
690
+ }
691
+
692
+ function proxyCb(app: Application, context: string, cb: MasterCallback) {
693
+ let msg: any = {};
694
+ let __proxy__ = app.components.__proxy__;
695
+ if (__proxy__ && __proxy__.client && __proxy__.client.proxies.user) {
696
+ let proxies = __proxy__.client.proxies.user;
697
+ let server = app.getServerById(context);
698
+ if (!server) {
699
+ cb('no server with this id ' + context);
700
+ } else {
701
+ let type = server['serverType'];
702
+ let tmp = proxies[type];
703
+ msg[type] = {};
704
+ for (let _proxy in tmp) {
705
+ let r = tmp[_proxy];
706
+ msg[type][_proxy] = {};
707
+ for (let _rpc in r) {
708
+ if (typeof r[_rpc] === 'function') {
709
+ msg[type][_proxy][_rpc] = 'function';
710
+ }
711
+ }
712
+ }
713
+ cb(null, msg);
714
+ }
715
+ } else {
716
+ cb('no proxy loaded');
717
+ }
718
+ }
719
+
720
+ function handlerCb(app: Application, context: string, cb: MasterCallback) {
721
+ let msg: any = {};
722
+ let __server__ = app.components.__server__;
723
+ if (__server__ && __server__.server && __server__.server.handlerService.handlerMap) {
724
+ let handles = __server__.server.handlerService.handlerMap;
725
+ let server = app.getServerById(context);
726
+ if (!server) {
727
+ cb('no server with this id ' + context);
728
+ } else {
729
+ let type = server['serverType'];
730
+ let tmp = handles as any;
731
+ msg[type] = {};
732
+ for (let _p in tmp) {
733
+ let r = tmp[_p];
734
+ msg[type][_p] = {};
735
+ for (let _r in r) {
736
+ if (typeof r[_r] === 'function') {
737
+ msg[type][_p][_r] = 'function';
738
+ }
739
+ }
740
+ }
741
+ cb(null, msg);
742
+ }
743
+ } else {
744
+ cb('no handler loaded');
745
+ }
746
+ }
747
+
748
+ function getComponentName(c: string): string {
749
+ let t = c.match(/^__(\w+)__$/);
750
+ if (t) {
751
+ t = t[1] as any;
752
+ }
753
+ return t as any;
754
+ }
755
+
756
+ function checkJSON(obj: any) {
757
+ if (!obj) {
758
+ return true;
759
+ }
760
+ try {
761
+ JSON.stringify(obj);
762
+ } catch (e) {
763
+ return false;
764
+ }
765
+ return true;
766
+ }