tinycore.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright 2018-2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var obj = { meshCoreInfo: 'TinyCore v1' };
  14. var mesh = null;
  15. // Replace a string with a number if the string is an exact number
  16. function toNumberIfNumber(x) { if ((typeof x == 'string') && (+parseInt(x) === x)) { x = parseInt(x); } return x; }
  17. // Split a string taking into account the quoats. Used for command line parsing
  18. function splitArgs(str)
  19. {
  20. var myArray = [], myRegexp = /[^\s"]+|"([^"]*)"/gi;
  21. do { var match = myRegexp.exec(str); if (match != null) { myArray.push(match[1] ? match[1] : match[0]); } } while (match != null);
  22. return myArray;
  23. }
  24. // Parse arguments string array into an object
  25. function parseArgs(argv)
  26. {
  27. var results = { '_': [] }, current = null;
  28. for (var i = 1, len = argv.length; i < len; i++)
  29. {
  30. var x = argv[i];
  31. if (x.length > 2 && x[0] == '-' && x[1] == '-')
  32. {
  33. if (current != null) { results[current] = true; }
  34. current = x.substring(2);
  35. } else
  36. {
  37. if (current != null) { results[current] = toNumberIfNumber(x); current = null; } else { results['_'].push(toNumberIfNumber(x)); }
  38. }
  39. }
  40. if (current != null) { results[current] = true; }
  41. return results;
  42. }
  43. function sendConsoleText(msg, sessionid)
  44. {
  45. try
  46. {
  47. if (sessionid != null)
  48. {
  49. require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg, sessionid: sessionid });
  50. }
  51. else
  52. {
  53. require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg });
  54. }
  55. }
  56. catch(e)
  57. {
  58. }
  59. }
  60. function processConsoleCommand(cmd, args, rights, sessionid)
  61. {
  62. try
  63. {
  64. var response = null;
  65. switch (cmd)
  66. {
  67. case 'help':
  68. response = "Available commands are: eval, osinfo, setdebug, versions.";
  69. break;
  70. case 'versions':
  71. response = JSON.stringify(process.versions, null, ' ');
  72. break;
  73. case 'eval':
  74. { // Eval JavaScript
  75. if (args['_'].length < 1)
  76. {
  77. response = 'Proper usage: eval "JavaScript code"'; // Display correct command usage
  78. } else
  79. {
  80. response = JSON.stringify(require('MeshAgent').eval(args['_'][0])); // This can only be run by trusted administrator.
  81. }
  82. break;
  83. }
  84. case 'setdebug':
  85. {
  86. if (args['_'].length < 1) { response = 'Proper usage: setdebug (target), 0 = Disabled, 1 = StdOut, 2 = This Console, * = All Consoles, 4 = WebLog, 8 = Logfile'; } // Display usage
  87. else { if (args['_'][0] == '*') { console.setDestination(2); } else { console.setDestination(parseInt(args['_'][0]), sessionid); } }
  88. break;
  89. }
  90. case 'osinfo': { // Return the operating system information
  91. var i = 1;
  92. if (args['_'].length > 0) { i = parseInt(args['_'][0]); if (i > 8) { i = 8; } response = 'Calling ' + i + ' times.'; }
  93. for (var j = 0; j < i; j++)
  94. {
  95. var pr = require('os').name();
  96. pr.sessionid = sessionid;
  97. pr.then(function (v)
  98. {
  99. sendConsoleText("OS: " + v, this.sessionid);
  100. });
  101. }
  102. break;
  103. }
  104. default: { // This is an unknown command, return an error message
  105. response = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.';
  106. break;
  107. }
  108. }
  109. } catch (e) { response = "Command returned an exception error: " + e; console.log(e); }
  110. if (response != null) { sendConsoleText(response, sessionid); }
  111. }
  112. // Handle a mesh agent command
  113. function handleServerCommand(data)
  114. {
  115. if ((typeof data == 'object') && (data.action == 'msg') && (data.type == 'console') && data.value && data.sessionid)
  116. {
  117. if (data.value && data.sessionid)
  118. {
  119. try
  120. {
  121. var args = splitArgs(data.value);
  122. processConsoleCommand(args[0].toLowerCase(), parseArgs(args), data.rights, data.sessionid);
  123. }
  124. catch(e)
  125. {
  126. sendConsoleText(e);
  127. }
  128. }
  129. }
  130. else
  131. {
  132. console.log(JSON.stringify(data, null, 1));
  133. }
  134. }
  135. // Called when the server connection state changes
  136. function handleServerConnection(state)
  137. {
  138. if (state == 1) { mesh.SendCommand({ "action": "coreinfo", "value": obj.meshCoreInfo }); } // Server connected, send mesh core information
  139. }
  140. obj.start = function ()
  141. {
  142. // Hook up mesh agent events
  143. mesh.AddCommandHandler(handleServerCommand);
  144. mesh.AddConnectHandler(handleServerConnection);
  145. mesh.SendCommand({ action: 'coreinfo', value: "TinyCore", caps: 0 });
  146. }
  147. obj.stop = function ()
  148. {
  149. mesh.AddCommandHandler(null);
  150. mesh.AddConnectHandler(null);
  151. }
  152. var xexports = null;
  153. try { xexports = module.exports; } catch (e) { }
  154. if (xexports != null)
  155. {
  156. // If we are running within NodeJS, export the core
  157. module.exports.createMeshCore = function (agent) { mesh = agent.getMeshApi(); return (obj); };
  158. }
  159. else
  160. {
  161. // If we are not running in NodeJS, launch the core
  162. sendConsoleText('TinyCore Started...');
  163. mesh = require('MeshAgent');
  164. obj.start();
  165. }