Debian Codegraph @

contrib/hwloc-ps.www/client.js

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