aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/awlsimclient.py
blob: ce28ef0e1b1de4ffa75d05e8d9c0cf3b9ec4a423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI simulator client access
#
# Copyright 2014-2016 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from awlsim.gui.util import *
from awlsim.gui.blocktreewidget import *

from awlsim.coreclient.client import *
from awlsim.coreclient.sshtunnel import *


def sleepWithEventLoop(seconds, excludeInput=True):
	end = monotonic_time() + seconds
	eventFlags = QEventLoop.AllEvents |\
		(QEventLoop.ExcludeUserInputEvents if excludeInput else 0)
	while monotonic_time() < end:
		QApplication.processEvents(eventFlags, 10)
		QThread.msleep(10)

class GuiSSHTunnel(SSHTunnel, QDialog):
	"""SSH tunnel helper with GUI.
	"""

	def __init__(self, parent, *args, **kwargs):
		QDialog.__init__(self, parent)
		SSHTunnel.__init__(self, *args, **kwargs)

		self.setLayout(QGridLayout())
		self.setWindowTitle("Establishing SSH tunnel...")

		self.log = QPlainTextEdit(self)
		self.log.setFont(getDefaultFixedFont())
		self.log.setReadOnly(True)
		self.layout().addWidget(self.log, 0, 0)

		self.resize(750, 180)

	def sleep(self, seconds):
		sleepWithEventLoop(seconds, excludeInput=True)

	def connect(self):
		self.hide()
		self.setWindowModality(Qt.ApplicationModal)
		self.show()
		try:
			result = SSHTunnel.connect(self, timeout=None)
		except AwlSimError as e:
			QMessageBox.critical(self,
				"Failed to establish SSH tunnel",
				"Failed to establish SSH tunnel:\n\n%s" % str(e))
			e.setSeenByUser()
			raise e
		finally:
			self.hide()
		return result

	def sshMessage(self, message, isDebug):
		"""Print a SSH log message.
		"""
		if not isDebug:
			self.log.setPlainText(self.log.toPlainText() + "\n" + message)
			sb = self.log.verticalScrollBar()
			sb.setSliderPosition(sb.maximum())

	def getPassphrase(self, prompt):
		"""Get a password from the user.
		"""
		pw, ok = QInputDialog.getText(self,
			"Please enter SSH password",
			"Please enter SSH password for '%s@%s':" %(
				self.sshUser, self.remoteHost),
			QLineEdit.Password)
		if not ok:
			return None
		try:
			return pw.encode("UTF-8", "ignore")
		except UnicodeError:
			return None

	def hostAuth(self, prompt):
		"""Get the user answer to the host authentication question.
		This function returns a boolean.
		"""
		res = QMessageBox.question(self,
			"Confirm host authenticity?",
			prompt,
			QMessageBox.Yes | QMessageBox.No,
			QMessageBox.No)
		return res == QMessageBox.Yes

class OnlineData(QObject):
	"""Container for data retrieved from the server.
	"""

	# Symbol table signal.
	# Contains only SymTabSources that can be parsed without errors.
	# Parameter: a list of tuples such as:
	#		[ (SymTabSource, SymbolTable), ... ]
	symTabsUpdate = Signal(list)

	def __init__(self, client):
		QObject.__init__(self)
		self.__client = client

		self.reset()

	def reset(self):
		self.__symTabCache = {}

	def __getSymTabByIdent(self, identHash):
		try:
			symTabSrc = self.__client.getSymTabSource(identHash)
			if symTabSrc:
				return SymTabParser.parseSource(symTabSrc)
		except AwlSimError as e:
			return None
		return None

	def handle_IDENTS(self, msg):
		# Parse the symbol table sources
		symTabCache = {}
		symTabList = []
		for symTabSrc in msg.symTabSources:
			identHash = symTabSrc.identHash
			symTab = self.__symTabCache.get(identHash, None)
			if symTab is None:
				symTab = self.__getSymTabByIdent(identHash)
			symTabCache[identHash] = symTab
			if symTab is not None:
				symTabList.append((symTabSrc, symTab))
		self.__symTabCache = symTabCache
		self.symTabsUpdate.emit(symTabList)

class GuiAwlSimClient(AwlSimClient, QObject):
	# CPU exception signal.
	haveException = Signal(AwlSimError)

	# CPU-dump signal.
	# Parameter: The dump text.
	haveCpuDump = Signal(str)

	# Instruction dump signal.
	# Parameter: AwlSimMessage_INSNSTATE instance.
	haveInsnDump = Signal(AwlSimMessage_INSNSTATE)

	# Memory update signal.
	# Parameter: A list of MemoryArea instances.
	haveMemoryUpdate = Signal(list)

	# Ident hashes signal.
	# Parameter: AwlSimMessage_IDENTS instance.
	haveIdentsMsg = Signal(AwlSimMessage_IDENTS)

	# Block info signal.
	# Parameter: AwlSimMessage_BLOCKINFO instance.
	haveBlockInfoMsg = Signal(AwlSimMessage_BLOCKINFO)

	# The client mode
	EnumGen.start
	MODE_OFFLINE	= EnumGen.item # Not connected
	MODE_ONLINE	= EnumGen.item # Connected to an existing core
	MODE_FORK	= EnumGen.item # Online to a newly forked core
	EnumGen.end

	def __init__(self):
		QObject.__init__(self)
		AwlSimClient.__init__(self)

		self.onlineData = OnlineData(self)

		self.__setMode(self.MODE_OFFLINE)

		self.__blockTreeModelManager = None
		self.__blockTreeModel = None

	# Override sleep handler
	def sleep(self, seconds):
		sleepWithEventLoop(seconds, excludeInput=True)

	# Override exception handler
	def handle_EXCEPTION(self, exception):
		# Emit the exception signal.
		self.haveException.emit(exception)
		# Call the default exception handler.
		AwlSimClient.handle_EXCEPTION(self, exception)

	# Override cpudump handler
	def handle_CPUDUMP(self, dumpText):
		self.haveCpuDump.emit(dumpText)

	# Override memory update handler
	def handle_MEMORY(self, memAreas):
		self.haveMemoryUpdate.emit(memAreas)

	# Override instruction state handler
	def handle_INSNSTATE(self, msg):
		self.haveInsnDump.emit(msg)

	# Override ident hashes handler
	def handle_IDENTS(self, msg):
		self.onlineData.handle_IDENTS(msg)
		self.haveIdentsMsg.emit(msg)

	# Override block info handler
	def handle_BLOCKINFO(self, msg):
		self.haveBlockInfoMsg.emit(msg)

	def getMode(self):
		return self.__mode

	def __setMode(self, mode, host = None, port = None, tunnel = None):
		self.__mode = mode
		self.__host = host
		self.__port = port
		self.__tunnel = tunnel
		self.onlineData.reset()

	def shutdown(self):
		# Shutdown the client.
		# If we are in FORK mode, this will also terminate
		# the forked core.
		# If we are in ONLINE mode, this will only
		# close the connection and possibly the tunnel.
		if self.__tunnel:
			self.__tunnel.shutdown()
			self.__tunnel = None
		AwlSimClient.shutdown(self)
		self.__setMode(self.MODE_OFFLINE)

	def setMode_OFFLINE(self):
		if self.__mode == self.MODE_OFFLINE:
			return
		if self.serverProcess:
			# Put the spawned core into STOP state.
			try:
				if not self.setRunState(False):
					raise RuntimeError
			except (AwlSimError, RuntimeError) as e:
				with suppressAllExc:
					self.killSpawnedServer()
		self.shutdownTransceiver()
		self.__setMode(self.MODE_OFFLINE)

	def setMode_ONLINE(self, parentWidget, linkSettings):
		host = linkSettings.getConnectHost()
		port = linkSettings.getConnectPort()
		timeout = linkSettings.getConnectTimeoutMs() / 1000.0
		wantTunnel = (linkSettings.getTunnel() == linkSettings.TUNNEL_SSH)
		sshUser = linkSettings.getSSHUser()
		sshPort = linkSettings.getSSHPort()
		sshExecutable = linkSettings.getSSHExecutable()

		if self.__mode == self.MODE_ONLINE:
			if wantTunnel and self.__tunnel:
				if host == self.__tunnel.remoteHost and\
				   port == self.__tunnel.remotePort and\
				   sshUser == self.__tunnel.sshUser and\
				   sshPort == self.__tunnel.sshPort and\
				   sshExecutable == self.__tunnel.sshExecutable:
					# We are already up and running.
					return
			elif not wantTunnel and not self.__tunnel:
				if self.__host == host and\
				   self.__port == port:
					# We are already up and running.
					return

		self.__interpreterList = None
		self.shutdown()

		tunnel = None
		try:
			if wantTunnel:
				localPort = linkSettings.getTunnelLocalPort()
				if localPort == linkSettings.TUNNEL_LOCPORT_AUTO:
					localPort = None
				tunnel = GuiSSHTunnel(parentWidget,
					remoteHost = host,
					remotePort = port,
					localPort = localPort,
					sshUser = sshUser,
					sshPort = sshPort,
					sshExecutable = sshExecutable
				)
				host, port = tunnel.connect()
				self.__tunnel = tunnel
			self.connectToServer(host = host,
					     port = port,
					     timeout = timeout)
		except AwlSimError as e:
			with suppressAllExc:
				self.shutdown()
			raise e
		self.__setMode(self.MODE_ONLINE, host = host,
			       port = port, tunnel = tunnel)

	def setMode_FORK(self, portRange,
			 interpreterList=None):
		host = "localhost"
		if self.__mode == self.MODE_FORK:
			if self.__port in portRange and\
			   self.__interpreterList == interpreterList:
				assert(self.__host == host)
				# We are already up and running.
				return
		try:
			if self.serverProcess:
				if self.serverProcessPort not in portRange or\
				   self.__interpreterList != interpreterList:
					self.killSpawnedServer()
			if not self.serverProcess:
				self.spawnServer(interpreter = interpreterList,
						 listenHost = host,
						 listenPort = portRange)
			self.shutdownTransceiver()
			self.connectToServer(host=host,
					     port=self.serverProcessPort,
					     timeout=10.0)
		except AwlSimError as e:
			with suppressAllExc:
				self.shutdown()
			raise e
		self.__setMode(self.MODE_FORK,
			       host=host,
			       port=self.serverProcessPort)
		self.__interpreterList = interpreterList

	def getBlockTreeModelRef(self):
		"""Get an ObjRef to the BlockTreeModel object."""

		if not self.__blockTreeModelManager:
			# Create a new block tree model
			self.__blockTreeModelManager = ObjRefManager("BlockTreeModel",
				allDestroyedCallback = self.__allBlockTreeModelRefsDestroyed)
			self.__blockTreeModel = BlockTreeModel(self)
			# Connect block tree message handlers
			self.haveIdentsMsg.connect(self.__blockTreeModel.handle_IDENTS)
			self.haveBlockInfoMsg.connect(self.__blockTreeModel.handle_BLOCKINFO)
			self.onlineData.symTabsUpdate.connect(self.__blockTreeModel.handle_symTabInfo)

		return ObjRef.make("BlockTreeModel", self.__blockTreeModelManager,
				   self.__blockTreeModel)

	def blockTreeModelActive(self):
		"""Returns True, if there is at least one active ref to
		the BlockTreeModel."""

		if self.__blockTreeModelManager:
			return self.__blockTreeModelManager.hasReferences
		return False

	def __allBlockTreeModelRefsDestroyed(self):
		# The last block tree model reference died. Destroy it.
		self.__blockTreeModelManager = None
		self.__blockTreeModel = None
bues.ch cgit interface