1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Lucas Bajolet <r4pass@hotmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Network debugger for a nit program, based on the original debugger
18 # Replaces access to stdin/stdout to send data on the network to the client concerned
19 module websocket_debugger
22 intrude import debugger
24 redef class ToolContext
26 var opt_debug_port
: OptionInt = new OptionInt("Sets the debug port (Defaults to 22125) - Must be contained between 0 and 65535", 22125, "--port")
31 self.option_context
.add_option
(self.opt_debug_port
)
36 redef class ModelBuilder
37 fun run_debugger_network_mode
(mainmodule
: MModule, arguments
: Array[String], port
: Int)
40 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
42 var interpreter
= new NetworkDebugger(self, mainmodule
, arguments
, port
)
44 init_naive_interpreter
(interpreter
, mainmodule
)
47 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
49 interpreter
.ns
.stop_server
53 # Extends the debugger by adding new network capabilities for remote debugging
57 # Represents the connexion with a particular client (Actually the only one accepted at the moment)
58 private var ns
: WebSocket
60 # Initializes the debugger, waits for a client to connect
61 # Then starts debugging as usual
62 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String], port
: Int)
64 print
"Listening on port {port}"
66 ns
= new WebSocket(port
, 1)
70 print
"Client connected"
74 if stdout
isa Stdout then
75 (stdout
.as(Stdout)).connection
= ns
84 # Checks on every call if the client has sent a command before continuing debugging
89 if command
== "stop" then
90 n
.debug
("Stopped by client")
91 while process_debug_command
(gets
) do end
93 process_debug_command
(command
)
104 # Breaks automatically when encountering an error
105 # Permits the injunction of commands before crashing
106 # Disconnects from the client before crashing
107 redef private fun fatal
(v
: NaiveInterpreter, message
: String)
109 if v
isa Debugger then
110 print
"An error was encountered, the program will stop now."
112 while v
.process_debug_command
(gets
) do end
115 if v
isa NetworkDebugger then
116 stdin
.connection
.stop_server
123 # Replaces Stdin to read on the network
126 # Represents the connection with a client
127 var connection
: nullable WebSocket = null
129 # Used to store data that has been read from the connection
130 var buf
: Buffer = new FlatBuffer
133 # Checks if data is available for reading
136 return connection
.can_read
(0)
139 # Reads the whole content of the buffer
140 # Blocking if the buffer is empty
143 var loc_buf
= new FlatBuffer
144 if connection
.can_read
(0) then buf
.append
(connection
.read
)
145 for i
in [buf_pos
.. buf
.length-1
] do loc_buf
.add
(buf
.chars
[i
])
151 # Reads a single char on the incoming buffer
152 # If the buffer is empty, the call is blocking
155 if connection
.can_read
(0) then buf
.append
(connection
.read
)
156 if buf_pos
>= buf
.length
then
160 buf
.append
(connection
.read
)
163 return buf
.chars
[buf_pos-1
].ascii
166 # Reads a line on the network if available
167 # Stops at the first encounter of a \n character
168 # If the buffer is empty, the read_line call is blocking
171 var line_buf
= new FlatBuffer
172 if connection
.can_read
(0) then
173 buf
.append
(connection
.read
)
175 var has_found_eol
: Bool = false
177 if buf_pos
>= buf
.length
then
181 buf
.append
(connection
.read
)
184 if buf
.chars
[buf_pos-1
] == '\n' then break
185 line_buf
.add
(buf
.chars
[buf_pos-1
])
191 # Replaces Stdout to write on the network
194 # Connection with the client object
195 var connection
: nullable WebSocket = null
197 # Writes a string on the network if available, else
198 # it is written in the standard output (Terminal)
201 if connection
!= null then
202 connection
.write
(s
.to_s
)