def run(self): # Echo server program try: import socket import _pydev_log log = _pydev_log.Log() dbg(SERVER_NAME + ' creating socket', INFO1) try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, self.thisPort)) except: sys.stderr.write( 'Error connecting with parameters: host: %s port: %s\n' % (HOST, self.serverPort)) raise s.listen(1) # socket to receive messages. # we stay here until we are connected. # we only accept 1 client. # the exit message for the server is @@KILL_SERVER_END@@ dbg( SERVER_NAME + ' waiting for connection on %s (%s)' % (HOST, self.thisPort), INFO1) conn, addr = s.accept() dbg( SERVER_NAME + ' connecting to java server on %s (%s)' % (HOST, self.serverPort), INFO1) # after being connected, create a socket as a client. self.connectToServer() dbg(SERVER_NAME + ' Connected by ' + str(addr), INFO1) while 1: data = '' returnMsg = '' keepAliveThread = KeepAliveThread(self.socket) while data.find(MSG_END) == -1: received = conn.recv(BUFFER_SIZE) if len(received) == 0: sys.exit(0) # ok, connection ended if IS_PYTHON3K: data = data + received.decode('utf-8') else: data = data + received try: try: if data.find(MSG_KILL_SERVER) != -1: dbg(SERVER_NAME + ' kill message received', INFO1) # break if we received kill message. self.ended = True sys.exit(0) dbg(SERVER_NAME + ' starting keep alive thread', INFO2) keepAliveThread.start() if data.find(MSG_PYTHONPATH) != -1: comps = [] for p in _sys_path: comps.append((p, ' ')) returnMsg = self.getCompletionsMessage(None, comps) else: data = data[:data.rfind(MSG_END)] if data.startswith(MSG_IMPORTS): data = data.replace(MSG_IMPORTS, '') data = unquote_plus(data) defFile, comps = _pydev_imports_tipper.GenerateTip( data, log) returnMsg = self.getCompletionsMessage( defFile, comps) elif data.startswith(MSG_CHANGE_PYTHONPATH): data = data.replace(MSG_CHANGE_PYTHONPATH, '') data = unquote_plus(data) ChangePythonPath(data) returnMsg = MSG_OK elif data.startswith(MSG_SEARCH): data = data.replace(MSG_SEARCH, '') data = unquote_plus(data) (f, line, col), foundAs = _pydev_imports_tipper.Search( data) returnMsg = self.getCompletionsMessage( f, [(line, col, foundAs)]) elif data.startswith(MSG_CHANGE_DIR): data = data.replace(MSG_CHANGE_DIR, '') data = unquote_plus(data) CompleteFromDir(data) returnMsg = MSG_OK elif data.startswith(MSG_BIKE): returnMsg = MSG_INVALID_REQUEST # No longer supported. else: returnMsg = MSG_INVALID_REQUEST except SystemExit: returnMsg = self.getCompletionsMessage( None, [('Exit:', 'SystemExit', '')]) keepAliveThread.lastMsg = returnMsg raise except: dbg(SERVER_NAME + ' exception occurred', ERROR) s = StringIO.StringIO() traceback.print_exc(file=s) err = s.getvalue() dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) returnMsg = self.getCompletionsMessage( None, [('ERROR:', '%s\nLog:%s' % (err, log.GetContents()), '')]) finally: log.Clear() keepAliveThread.lastMsg = returnMsg conn.close() self.ended = True sys.exit(0) # connection broken except SystemExit: raise # No need to log SystemExit error except: s = StringIO.StringIO() exc_info = sys.exc_info() traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file=s) err = s.getvalue() dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) raise
def run(self): # Echo server program try: import _pydev_log log = _pydev_log.Log() dbg( SERVER_NAME + ' connecting to java server on %s (%s)' % (HOST, self.port), INFO1) # after being connected, create a socket as a client. self.connectToServer() dbg(SERVER_NAME + ' Connected to java server', INFO1) while not self.ended: data = '' while data.find(MSG_END) == -1: received = self.socket.recv(BUFFER_SIZE) if len(received) == 0: raise Exit() # ok, connection ended if IS_PYTHON3K: data = data + received.decode('utf-8') else: data = data + received try: try: if data.find(MSG_KILL_SERVER) != -1: dbg(SERVER_NAME + ' kill message received', INFO1) # break if we received kill message. self.ended = True raise Exit() dbg(SERVER_NAME + ' starting keep alive thread', INFO2) if data.find(MSG_PYTHONPATH) != -1: comps = [] for p in _sys_path: comps.append((p, ' ')) self.send(self.getCompletionsMessage(None, comps)) else: data = data[:data.rfind(MSG_END)] if data.startswith(MSG_IMPORTS): data = data[len(MSG_IMPORTS):] data = unquote_plus(data) defFile, comps = _pydev_imports_tipper.GenerateTip( data, log) self.send( self.getCompletionsMessage(defFile, comps)) elif data.startswith(MSG_CHANGE_PYTHONPATH): data = data[len(MSG_CHANGE_PYTHONPATH):] data = unquote_plus(data) ChangePythonPath(data) self.send(MSG_OK) elif data.startswith(MSG_JEDI): data = data[len(MSG_JEDI):] data = unquote_plus(data) line, column, encoding, path, source = data.split( '|', 4) try: import jedi # @UnresolvedImport except: self.send( self.getCompletionsMessage( None, [('Error on import jedi', 'Error importing jedi', '')])) else: script = jedi.Script( # Line +1 because it expects lines 1-based (and col 0-based) source=source, line=int(line) + 1, column=int(column), source_encoding=encoding, path=path, ) lst = [] for completion in script.completions(): t = completion.type if t == 'class': t = '1' elif t == 'function': t = '2' elif t == 'import': t = '0' elif t == 'keyword': continue # Keywords are already handled in PyDev elif t == 'statement': t = '3' else: t = '-1' # gen list(tuple(name, doc, args, type)) lst.append( (completion.name, '', '', t)) self.send( self.getCompletionsMessage( 'empty', lst)) elif data.startswith(MSG_SEARCH): data = data[len(MSG_SEARCH):] data = unquote_plus(data) (f, line, col), foundAs = _pydev_imports_tipper.Search( data) self.send( self.getCompletionsMessage( f, [(line, col, foundAs)])) elif data.startswith(MSG_CHANGE_DIR): data = data[len(MSG_CHANGE_DIR):] data = unquote_plus(data) CompleteFromDir(data) self.send(MSG_OK) else: self.send(MSG_INVALID_REQUEST) except Exit: self.send( self.getCompletionsMessage( None, [('Exit:', 'SystemExit', '')])) raise except: dbg(SERVER_NAME + ' exception occurred', ERROR) s = StringIO.StringIO() traceback.print_exc(file=s) err = s.getvalue() dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) self.send( self.getCompletionsMessage( None, [('ERROR:', '%s\nLog:%s' % (err, log.GetContents()), '')])) finally: log.Clear() self.socket.close() self.ended = True raise Exit() # connection broken except Exit: if self.exit_process_on_kill: sys.exit(0) # No need to log SystemExit error except: s = StringIO.StringIO() exc_info = sys.exc_info() traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file=s) err = s.getvalue() dbg(SERVER_NAME + ' received error: ' + str(err), ERROR) raise