Skip to content
This repository was archived by the owner on Aug 28, 2020. It is now read-only.

Commit 5c2f98f

Browse files
committed
Merge pull request #136 from robertbasic/feature/better-support-for-multiple-requests
Feature/better support for multiple requests. Fixes #129
2 parents 95b1689 + 5d26821 commit 5c2f98f

File tree

4 files changed

+100
-61
lines changed

4 files changed

+100
-61
lines changed

pugdebug/debugger.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class PugdebugDebugger(QObject):
2828
current_file = ''
2929
current_line = 0
3030

31+
server_stopped_signal = pyqtSignal()
3132
debugging_started_signal = pyqtSignal()
3233
debugging_post_start_signal = pyqtSignal()
3334
debugging_stopped_signal = pyqtSignal()
@@ -58,8 +59,8 @@ def __init__(self):
5859
def connect_server_signals(self):
5960
"""Connect server signals to slots
6061
"""
61-
self.server.server_connected_signal.connect(
62-
self.handle_server_connected
62+
self.server.new_connection_established_signal.connect(
63+
self.handle_new_connection_established
6364
)
6465
self.server.server_stopped_signal.connect(
6566
self.handle_server_stopped
@@ -128,21 +129,15 @@ def connect_connection_signals(self, connection):
128129
self.handle_connection_error
129130
)
130131

131-
def cleanup(self):
132-
"""Cleanup debugger when it's done
132+
def cleanup_current_connection(self):
133+
"""Cleanup the current debugger connection
133134
134-
If there is an active connection, disconnect from it and clear
135-
all the remaining connections.
136-
137-
Stop the server from listening.
135+
If there is an active connection, disconnect from it.
138136
139137
Clean up attributes.
140138
"""
141139
if self.is_connected():
142140
self.current_connection.disconnect()
143-
self.connections.clear()
144-
145-
self.server.stop()
146141

147142
self.current_connection = None
148143
self.step_result = ''
@@ -159,14 +154,12 @@ def has_pending_connections(self):
159154
"""
160155
return len(self.connections) > 0
161156

162-
def start_debug(self):
163-
"""Start a debugging session
164-
165-
If the server is not connected, connect it.
157+
def start_listening(self):
158+
"""Start listening to new connections
166159
"""
167-
self.server.connect()
160+
self.server.start_listening()
168161

169-
def handle_server_connected(self, connection):
162+
def handle_new_connection_established(self, connection):
170163
"""Handle when the server establishes a new connection
171164
172165
Connect the signals for the new connection.
@@ -180,9 +173,9 @@ def handle_server_connected(self, connection):
180173
self.connections.append(connection)
181174

182175
if not self.is_connected():
183-
self.start_new_connection()
176+
self.start_debugging_new_connection()
184177

185-
def start_new_connection(self):
178+
def start_debugging_new_connection(self):
186179
"""Start a new connection
187180
188181
Get the first (oldest) connection from the queue, set it's init
@@ -208,15 +201,20 @@ def handle_post_start(self):
208201
"""
209202
self.debugging_post_start_signal.emit()
210203

211-
def handle_server_stopped(self):
212-
"""Handle when the server is stopped
204+
def stop_listening(self):
205+
"""Stop listening for new connections
213206
214-
If the current connection is terminated, cleanup the debugging
215-
session and emit the debugging stopped signal.
207+
Clear connections queue, stop debugging the current connection.
216208
"""
217-
if not self.is_connected():
218-
self.cleanup()
219-
self.debugging_stopped_signal.emit()
209+
self.connections.clear()
210+
self.stop_debug()
211+
212+
self.server.stop_listening()
213+
214+
def handle_server_stopped(self):
215+
"""Handle when the server stops listening to new connections
216+
"""
217+
self.server_stopped_signal.emit()
220218

221219
def stop_debug(self):
222220
"""Stop a debugging session
@@ -226,8 +224,6 @@ def stop_debug(self):
226224
"""
227225
if self.is_connected():
228226
self.current_connection.stop()
229-
else:
230-
self.server.stop()
231227

232228
def detach_debug(self):
233229
"""Detach the current connection
@@ -240,13 +236,12 @@ def handle_stopped(self):
240236
241237
If there are pending connections, start a new one.
242238
243-
Otherwise clean up and emit a server stopped signal.
239+
Otherwise emit a debugging stopped signal.
244240
"""
245241
if self.has_pending_connections():
246-
self.start_new_connection()
242+
self.start_debugging_new_connection()
247243
else:
248-
self.cleanup()
249-
244+
self.cleanup_current_connection()
250245
self.debugging_stopped_signal.emit()
251246

252247
def run_debug(self):

pugdebug/gui/main_window.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,38 @@ def setup_file_actions(self):
147147
self.quit_action.triggered.connect(self.close)
148148

149149
def setup_actions(self):
150-
self.start_debug_action = QAction("Start", self)
151-
self.start_debug_action.setToolTip("Start server (F1)")
152-
self.start_debug_action.setStatusTip(
153-
"Start listening to for connections. Shortcut: F1"
150+
self.start_listening_action = QAction("Start listening", self)
151+
self.start_listening_action.setToolTip(
152+
"Start listening for new connections (F1)"
154153
)
155-
self.start_debug_action.setShortcut(QKeySequence("F1"))
154+
self.start_listening_action.setStatusTip(
155+
"Start listening for incomming connections. Shortcut: F1"
156+
)
157+
self.start_listening_action.setShortcut(QKeySequence("F1"))
158+
159+
self.stop_listening_action = QAction("Stop listening", self)
160+
self.stop_listening_action.setToolTip(
161+
"Stop listening for new connections (F2)"
162+
)
163+
self.stop_listening_action.setStatusTip(
164+
"Stop listening to incomming connections. Shortcut: F2"
165+
)
166+
self.stop_listening_action.setShortcut(QKeySequence("F2"))
156167

157168
self.stop_debug_action = QAction("Stop", self)
158-
self.stop_debug_action.setToolTip("Stop server (F2)")
169+
self.stop_debug_action.setToolTip("Stop debugging (F3)")
159170
self.stop_debug_action.setStatusTip(
160-
"Stop listening to for connections. Shortcut: F2"
171+
"Stop debugging the current request. Shortcut: F3"
161172
)
162-
self.stop_debug_action.setShortcut(QKeySequence("F2"))
173+
self.stop_debug_action.setShortcut(QKeySequence("F3"))
163174

164175
self.detach_debug_action = QAction("Detach", self)
165-
self.detach_debug_action.setToolTip("Detach from server (F3)")
176+
self.detach_debug_action.setToolTip("Detach debugger (F4)")
166177
self.detach_debug_action.setStatusTip(
167-
"Stop the debugging session, but let the PHP process end normally."
168-
" Shortcut: F3"
178+
"Detach debugger from the current request."
179+
" Shortcut: F4"
169180
)
170-
self.detach_debug_action.setShortcut(QKeySequence("F3"))
181+
self.detach_debug_action.setShortcut(QKeySequence("F4"))
171182

172183
self.run_debug_action = QAction("Run", self)
173184
self.run_debug_action.setToolTip("Start/resume the script (F5)")
@@ -208,7 +219,9 @@ def setup_toolbar(self):
208219
toolbar = QToolBar("Main Toolbar")
209220
toolbar.setObjectName("main-toolbar")
210221

211-
toolbar.addAction(self.start_debug_action)
222+
toolbar.addAction(self.start_listening_action)
223+
toolbar.addAction(self.stop_listening_action)
224+
toolbar.addSeparator()
212225
toolbar.addAction(self.stop_debug_action)
213226
toolbar.addAction(self.detach_debug_action)
214227
toolbar.addSeparator()
@@ -235,7 +248,9 @@ def setup_menubar(self):
235248
view_menu.addAction(widget.toggleViewAction())
236249

237250
debug_menu = menu_bar.addMenu("&Debug")
238-
debug_menu.addAction(self.start_debug_action)
251+
debug_menu.addAction(self.start_listening_action)
252+
debug_menu.addAction(self.stop_listening_action)
253+
debug_menu.addSeparator()
239254
debug_menu.addAction(self.stop_debug_action)
240255
debug_menu.addAction(self.detach_debug_action)
241256
debug_menu.addSeparator()
@@ -247,13 +262,14 @@ def setup_menubar(self):
247262
self.setMenuBar(menu_bar)
248263

249264
def toggle_actions(self, enabled):
265+
self.stop_debug_action.setEnabled(enabled)
250266
self.detach_debug_action.setEnabled(enabled)
251267
self.run_debug_action.setEnabled(enabled)
252268
self.step_over_action.setEnabled(enabled)
253269
self.step_into_action.setEnabled(enabled)
254270
self.step_out_action.setEnabled(enabled)
255271

256-
self.start_debug_action.setEnabled(not enabled)
272+
self.start_listening_action.setEnabled(not enabled)
257273

258274
def get_file_browser(self):
259275
return self.file_browser

pugdebug/pugdebug.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ def connect_toolbar_action_signals(self):
155155
Connect signals when the run and step continuation commands are
156156
triggered are connected.
157157
"""
158-
self.main_window.start_debug_action.triggered.connect(self.start_debug)
158+
self.main_window.start_listening_action.triggered.connect(
159+
self.start_listening
160+
)
161+
self.main_window.stop_listening_action.triggered.connect(
162+
self.stop_listening
163+
)
159164
self.main_window.stop_debug_action.triggered.connect(self.stop_debug)
160165
self.main_window.detach_debug_action.triggered.connect(
161166
self.detach_debug
@@ -178,6 +183,9 @@ def connect_debugger_signals(self):
178183
"""
179184

180185
# Start/stop signals
186+
self.debugger.server_stopped_signal.connect(
187+
self.handle_server_stopped_listening
188+
)
181189
self.debugger.debugging_started_signal.connect(
182190
self.handle_debugging_started
183191
)
@@ -429,8 +437,8 @@ def handle_debugger_features_changed(self):
429437
if self.debugger.is_connected():
430438
self.debugger.set_debugger_features()
431439

432-
def start_debug(self):
433-
"""Start a new debugging session
440+
def start_listening(self):
441+
"""Start listening to new incomming connections
434442
435443
Clear the variable viewer.
436444
@@ -462,7 +470,7 @@ def start_debug(self):
462470

463471
self.document_viewer.remove_line_highlights()
464472

465-
self.debugger.start_debug()
473+
self.debugger.start_listening()
466474
self.main_window.set_debugging_status(1)
467475

468476
def handle_debugging_started(self):
@@ -512,6 +520,12 @@ def handle_debugging_post_start(self):
512520
else:
513521
self.step_into()
514522

523+
def stop_listening(self):
524+
self.debugger.stop_listening()
525+
526+
def handle_server_stopped_listening(self):
527+
self.main_window.set_debugging_status(0)
528+
515529
def stop_debug(self):
516530
"""Stop a debugging session
517531

pugdebug/server.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PugdebugServer(QThread):
2323

2424
wait_for_accept = True
2525

26-
server_connected_signal = pyqtSignal(object)
26+
new_connection_established_signal = pyqtSignal(object)
2727
server_stopped_signal = pyqtSignal()
2828

2929
server_error_signal = pyqtSignal(str)
@@ -36,29 +36,41 @@ def __init__(self):
3636
def run(self):
3737
self.mutex.lock()
3838

39-
self.__connect()
39+
socket_server = self.__connect()
40+
41+
if socket_server is not None:
42+
self.__listen(socket_server)
4043

4144
self.mutex.unlock()
4245

43-
def connect(self):
46+
def start_listening(self):
4447
self.wait_for_accept = True
4548
self.start()
4649

47-
def stop(self):
50+
def stop_listening(self):
4851
self.wait_for_accept = False
4952

5053
def __connect(self):
51-
"""Connect to the server and listen to new incomming connections
54+
"""Connect to the socket server
55+
"""
56+
try:
57+
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
58+
socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
59+
socket_server.settimeout(1)
60+
except OSError as e:
61+
socket_server = None
62+
self.server_error_signal.emit(e.strerror)
63+
64+
return socket_server
65+
66+
def __listen(self, socket_server):
67+
"""Listen to new incomming connections
5268
5369
For every accepted connection, see if it is valid and emit a signal
5470
with that new connection.
5571
5672
Otherwise silently disregard that connection.
5773
"""
58-
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
59-
socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
60-
socket_server.settimeout(1)
61-
6274
host = get_setting('debugger/host')
6375
port_number = int(get_setting('debugger/port_number'))
6476

@@ -76,7 +88,9 @@ def __connect(self):
7688
is_valid = connection.init_connection()
7789

7890
if is_valid:
79-
self.server_connected_signal.emit(connection)
91+
self.new_connection_established_signal.emit(
92+
connection
93+
)
8094
except socket.timeout:
8195
pass
8296

0 commit comments

Comments
 (0)