annotate src/testdir/test_channel_lsp.py @ 28513:6a1e5b188374 v8.2.4781

patch 8.2.4781: Maxima files are not recognized Commit: https://github.com/vim/vim/commit/d0a20c9d111da75febb60ffee2e15f727ab6a5ad Author: Doron Behar <doron.behar@gmail.com> Date: Mon Apr 18 14:32:42 2022 +0100 patch 8.2.4781: Maxima files are not recognized Problem: Maxima files are not recognized. Solution: Add patterns to detect Maxima files. (Doron Behar, closes https://github.com/vim/vim/issues/10211)
author Bram Moolenaar <Bram@vim.org>
date Mon, 18 Apr 2022 15:45:02 +0200
parents d7ca583e5772
children 6b1da12297e5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 #!/usr/bin/env python
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 #
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 # Server that will accept connections from a Vim channel.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 # Used by test_channel.vim to test LSP functionality.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 #
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 # This requires Python 2.6 or later.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 from __future__ import print_function
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9 import json
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 import socket
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 import sys
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 import time
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 import threading
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 # Python 3
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 import socketserver
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 except ImportError:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 # Python 2
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 import SocketServer as socketserver
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 def setup(self):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 def send_lsp_msg(self, msgid, resp_dict):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 v = {'jsonrpc': '2.0', 'result': resp_dict}
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 if msgid != -1:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 v['id'] = msgid
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 resp = "Content-Length: " + str(len(s)) + "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 if self.debug:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 with open("Xlspdebug.log", "a") as myfile:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 myfile.write("\n=> send\n" + resp)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 def send_wrong_payload(self):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 v = 'wrong-payload'
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 resp = "Content-Length: " + str(len(s)) + "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 def send_empty_header(self, msgid, resp_dict):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 resp = "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 def send_empty_payload(self):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 resp = "Content-Length: 0\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 def send_extra_hdr_fields(self, msgid, resp_dict):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 # test for sending extra fields in the http header
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 resp = "Host: abc.vim.org\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 resp += "User-Agent: Python\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 resp += "Accept-Language: en-US,en\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 resp += "Content-Length: " + str(len(s)) + "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75
28511
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
76 def send_delayed_payload(self, msgid, resp_dict):
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
77 # test for sending the hdr first and then after some delay, send the
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
78 # payload
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
79 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
80 s = json.dumps(v)
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
81 resp = "Content-Length: " + str(len(s)) + "\r\n"
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
82 resp += "\r\n"
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
83 self.request.sendall(resp.encode('utf-8'))
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
84 time.sleep(0.05)
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
85 resp = s
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
86 self.request.sendall(resp.encode('utf-8'))
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
87
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 def send_hdr_without_len(self, msgid, resp_dict):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 # test for sending the http header without length
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 resp = "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 def send_hdr_with_wrong_len(self, msgid, resp_dict):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 # test for sending the http header with wrong length
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 resp = "Content-Length: 1000\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 def send_hdr_with_negative_len(self, msgid, resp_dict):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 # test for sending the http header with negative length
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 resp = "Content-Length: -1\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 self.request.sendall(resp.encode('utf-8'))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 def do_ping(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 time.sleep(0.2)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 self.send_lsp_msg(payload['id'], 'alive')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 def do_echo(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 self.send_lsp_msg(-1, payload)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 def do_simple_rpc(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 # test for a simple RPC request
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 self.send_lsp_msg(payload['id'], 'simple-rpc')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 def do_rpc_with_notif(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 # test for sending a notification before replying to a request message
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 self.send_lsp_msg(-1, 'rpc-with-notif-notif')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 # sleep for some time to make sure the notification is delivered
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 time.sleep(0.2)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 self.send_lsp_msg(payload['id'], 'rpc-with-notif-resp')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 def do_wrong_payload(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 # test for sending a non dict payload
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 self.send_wrong_payload()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 time.sleep(0.2)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 self.send_lsp_msg(-1, 'wrong-payload')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 def do_rpc_resp_incorrect_id(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-1')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-2')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 self.send_lsp_msg(1, 'rpc-resp-incorrect-id-3')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 time.sleep(0.2)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 self.send_lsp_msg(payload['id'], 'rpc-resp-incorrect-id-4')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 def do_simple_notif(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 # notification message test
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 self.send_lsp_msg(-1, 'simple-notif')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 def do_multi_notif(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 # send multiple notifications
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 self.send_lsp_msg(-1, 'multi-notif1')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 self.send_lsp_msg(-1, 'multi-notif2')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 def do_msg_with_id(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 self.send_lsp_msg(payload['id'], 'msg-with-id')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 def do_msg_specific_cb(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 self.send_lsp_msg(payload['id'], 'msg-specifc-cb')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 def do_server_req(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 self.send_lsp_msg(201, {'method': 'checkhealth', 'params': {'a': 20}})
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 def do_extra_hdr_fields(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 self.send_extra_hdr_fields(payload['id'], 'extra-hdr-fields')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166
28511
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
167 def do_delayad_payload(self, payload):
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
168 self.send_delayed_payload(payload['id'], 'delayed-payload')
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
169
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 def do_hdr_without_len(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 self.send_hdr_without_len(payload['id'], 'hdr-without-len')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 def do_hdr_with_wrong_len(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 self.send_hdr_with_wrong_len(payload['id'], 'hdr-with-wrong-len')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 def do_hdr_with_negative_len(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 self.send_hdr_with_negative_len(payload['id'], 'hdr-with-negative-len')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 def do_empty_header(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 self.send_empty_header(payload['id'], 'empty-header')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 def do_empty_payload(self, payload):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 self.send_empty_payload()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 def process_msg(self, msg):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 decoded = json.loads(msg)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 print("Decoded:")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 print(str(decoded))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 if 'method' in decoded:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 test_map = {
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 'ping': self.do_ping,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 'echo': self.do_echo,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 'simple-rpc': self.do_simple_rpc,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 'rpc-with-notif': self.do_rpc_with_notif,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 'wrong-payload': self.do_wrong_payload,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 'rpc-resp-incorrect-id': self.do_rpc_resp_incorrect_id,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 'simple-notif': self.do_simple_notif,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199 'multi-notif': self.do_multi_notif,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 'msg-with-id': self.do_msg_with_id,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 'msg-specifc-cb': self.do_msg_specific_cb,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202 'server-req': self.do_server_req,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 'extra-hdr-fields': self.do_extra_hdr_fields,
28511
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
204 'delayed-payload': self.do_delayad_payload,
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 'hdr-without-len': self.do_hdr_without_len,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 'hdr-with-wrong-len': self.do_hdr_with_wrong_len,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 'hdr-with-negative-len': self.do_hdr_with_negative_len,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 'empty-header': self.do_empty_header,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 'empty-payload': self.do_empty_payload
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 }
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 if decoded['method'] in test_map:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 test_map[decoded['method']](decoded)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 else:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 print("Error: Unsupported method: " + decoded['method'])
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 else:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 print("Error: 'method' field is not found")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218 except ValueError:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 print("json decoding failed")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 def process_msgs(self, msgbuf):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 while True:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 sidx = msgbuf.find('Content-Length: ')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 if sidx == -1:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 return msgbuf
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 sidx += 16
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 eidx = msgbuf.find('\r\n')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 if eidx == -1:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 return msgbuf
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 msglen = int(msgbuf[sidx:eidx])
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232 hdrend = msgbuf.find('\r\n\r\n')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 if hdrend == -1:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 return msgbuf
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236 # Remove the header
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 msgbuf = msgbuf[hdrend + 4:]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 payload = msgbuf[:msglen]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 self.process_msg(payload)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 # Remove the processed message
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 msgbuf = msgbuf[msglen:]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 def handle(self):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 print("=== socket opened ===")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 self.debug = False
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 msgbuf = ''
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 while True:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 received = self.request.recv(4096).decode('utf-8')
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 except socket.error:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 print("=== socket error ===")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 break
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 except IOError:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 print("=== socket closed ===")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 break
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 if received == '':
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 print("=== socket closed ===")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 break
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 print("\nReceived:\n{0}".format(received))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 # Write the received lines into the file for debugging
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 if self.debug:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265 with open("Xlspdebug.log", "a") as myfile:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 myfile.write("\n<= recv\n" + received)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 # Can receive more than one line in a response or a partial line.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
269 # Accumulate all the received characters and process one line at
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 # a time.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 msgbuf += received
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 msgbuf = self.process_msgs(msgbuf)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 pass
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277 def writePortInFile(port):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278 # Write the port number in Xportnr, so that the test knows it.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 f = open("Xportnr", "w")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 f.write("{0}".format(port))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 f.close()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 def main(host, port, server_class=ThreadedTCPServer):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 # Wait half a second before opening the port to test waittime in ch_open().
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 # We do want to get the port number, get that first. We cannot open the
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 # socket, guess a port is free.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288 port = 13684
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 writePortInFile(port)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 print("Wait for it...")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 time.sleep(0.5)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294 server = server_class((host, port), ThreadedTCPRequestHandler)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 ip, port = server.server_address[0:2]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 # Start a thread with the server. That thread will then start a new thread
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 # for each connection.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299 server_thread = threading.Thread(target=server.serve_forever)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 server_thread.start()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 writePortInFile(port)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
303
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 print("Listening on port {0}".format(port))
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306 # Main thread terminates, but the server continues running
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
307 # until server.shutdown() is called.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
309 while server_thread.is_alive():
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 server_thread.join(1)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 except (KeyboardInterrupt, SystemExit):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 server.shutdown()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 if __name__ == "__main__":
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 main("localhost", 0)