client.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright © 2019 Inria. All rights reserved. */
  2. var express = require('express')
  3. var path = require('path')
  4. var app = express()
  5. var net = require('net')
  6. var HOST = 'localhost'
  7. var HPPORT = 8888
  8. var NPORT = 3000
  9. var verbose = 0
  10. var open = 1
  11. var fs = require('fs')
  12. var open = require("open")
  13. var lastRequest = ''
  14. var client = new net.Socket()
  15. var svg
  16. var json
  17. function usage() {
  18. console.log(path.basename(process.argv[0]) + " " + path.basename(process.argv[1]) + " [options] <file.svg>")
  19. console.log(" SVG file <file.svg> must be generated with lstopo --of nativesvg <file.svg>")
  20. console.log("Options:")
  21. console.log(" --hp <port> Change hwloc-ps JSON server port (default is "+HPPORT+")")
  22. console.log(" --np <port> Change node port (default is "+NPORT+")")
  23. console.log(" -u Display the webpage URL instead of opening it")
  24. console.log(" -v --verbose Increase verbosity")
  25. console.log(" -h --help Show this help")
  26. }
  27. var currentarg = 2
  28. while(process.argv.length > currentarg){
  29. if(process.argv[currentarg] === '-h' || process.argv[currentarg] === '-help'){
  30. usage()
  31. return
  32. }else if (process.argv[currentarg] === '-v' || process.argv[currentarg] === '--verbose'){
  33. verbose++
  34. currentarg++
  35. }else if (process.argv[currentarg] === '-u'){
  36. open = 0
  37. currentarg++
  38. }else if (process.argv[currentarg] === '--hp'){
  39. if(currentarg+2 > process.argv.length){
  40. console.err("Missing argument after --hp")
  41. usage()
  42. return
  43. }
  44. HPPORT = process.argv[currentarg + 1]
  45. currentarg += 2
  46. }else if (process.argv[currentarg] === '--np'){
  47. if(currentarg+2 > process.argv.length){
  48. console.err("Missing argument after --np")
  49. usage()
  50. return
  51. }
  52. NPORT = process.argv[currentarg + 1]
  53. currentarg += 2
  54. }else{
  55. if(svg != undefined){
  56. console.err("Unexpected parameter after SVG filename.")
  57. usage()
  58. return
  59. }
  60. svg = process.argv[currentarg]
  61. currentarg++
  62. }
  63. }
  64. if(svg === undefined){
  65. console.err("Missing SVG filename.")
  66. usage()
  67. return
  68. }
  69. client.on('data', function(data) {
  70. if (verbose > 0)
  71. console.log('##########################\n'
  72. + 'Client received:\n'
  73. + data
  74. + '##########################\n')
  75. json += data
  76. })
  77. client.on('close', function() {
  78. console.log('Client closed')
  79. })
  80. client.on('error', function(err) {
  81. console.error(err)
  82. })
  83. app.get('/svg', function (req, res, next) {
  84. res.setHeader('Content-Type', 'image/svg+xml')
  85. res.sendFile(path.resolve(__dirname, svg))
  86. })
  87. app.get('/js', function (req, res, next) {
  88. res.setHeader('Content-Type', 'text/javascript')
  89. res.sendFile(path.join(__dirname, './assets/script.js'))
  90. })
  91. app.get('/json', function (req, res, next) {
  92. res.setHeader('Content-Type', 'application/json')
  93. res.send(json)
  94. })
  95. app.get('/stylecss', function (req, res, next) {
  96. res.setHeader('Content-Type', 'text/css')
  97. res.sendFile(path.join(__dirname, './assets/style.css'))
  98. })
  99. app.get('/maincss', function (req, res, next) {
  100. res.setHeader('Content-Type', 'text/css')
  101. res.sendFile(path.join(__dirname, './assets/main.css'))
  102. })
  103. app.get('/', function(req, res){
  104. res.sendFile(path.join(__dirname+'/assets/index.html'))
  105. })
  106. app.get('/bound', async (req, res) =>{
  107. json = ""
  108. lastRequest = 'bound'
  109. await client.write(lastRequest)
  110. setTimeout(function(){
  111. res.setHeader('Content-Type', 'application/json')
  112. res.send(JSON.parse(json))
  113. }, 500)
  114. })
  115. app.get('/all', async (req, res) =>{
  116. json = ""
  117. lastRequest = 'all'
  118. await client.write(lastRequest)
  119. setTimeout(function(){
  120. res.setHeader('Content-Type', 'application/json')
  121. res.send(json)
  122. }, 500)
  123. })
  124. app.get('/proc/:filter', async (req, res) =>{
  125. json = ""
  126. if (/^\d+$/.test(req.params.filter))
  127. lastRequest = 'threads pid=' + req.params.filter
  128. else
  129. lastRequest = 'name=' + req.params.filter
  130. await client.write(lastRequest)
  131. setTimeout(function(){
  132. res.setHeader('Content-Type', 'application/json')
  133. res.send(json)
  134. }, 500)
  135. })
  136. app.get('/threads', async (req, res) =>{
  137. json = ""
  138. if(!lastRequest.includes('threads'))
  139. lastRequest = 'threads ' + lastRequest
  140. else
  141. lastRequest = lastRequest.replace('threads ', '')
  142. await client.write(lastRequest)
  143. setTimeout(function(){
  144. res.setHeader('Content-Type', 'application/json')
  145. res.send(json)
  146. }, 500)
  147. })
  148. app.get('/lastcpulocation', async (req, res) =>{
  149. json = ""
  150. if(!lastRequest.includes('lastcpulocation'))
  151. lastRequest = 'lastcpulocation ' + lastRequest
  152. else
  153. lastRequest = lastRequest.replace('lastcpulocation ', '')
  154. await client.write(lastRequest)
  155. setTimeout(function(){
  156. res.setHeader('Content-Type', 'application/json')
  157. res.send(json)
  158. }, 500)
  159. })
  160. client.connect(HPPORT, HOST, function() {
  161. console.log('Client connected to hwloc-ps JSON server running on ' + HOST + ':' + HPPORT)
  162. let serv = app.listen(NPORT)
  163. if(open){
  164. open("http://localhost:" + NPORT)
  165. }else{
  166. console.log("Page is available from http://localhost:" + NPORT)
  167. }
  168. })