Debian Codegraph @

modules/hwloc/contrib/hwloc-ps.www/client.js

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