blob: 61e76ec4f860a631c85211426dd4eeb1fb60c3f9 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001#!/usr/bin/python
2#
3# Server that will accept connections from a Vim channel.
Bram Moolenaar77073442016-02-13 23:23:53 +01004# Used by test_channel.vim.
Bram Moolenaard7ece102016-02-02 23:23:02 +01005#
6# This requires Python 2.6 or later.
7
8from __future__ import print_function
9import json
10import socket
11import sys
Bram Moolenaar81661fb2016-02-18 22:23:34 +010012import time
Bram Moolenaard7ece102016-02-02 23:23:02 +010013import threading
14
15try:
16 # Python 3
17 import socketserver
18except ImportError:
19 # Python 2
20 import SocketServer as socketserver
21
Bram Moolenaard7ece102016-02-02 23:23:02 +010022class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
23
24 def handle(self):
25 print("=== socket opened ===")
Bram Moolenaard7ece102016-02-02 23:23:02 +010026 while True:
27 try:
Bram Moolenaar608a8912016-02-03 22:39:51 +010028 received = self.request.recv(4096).decode('utf-8')
Bram Moolenaard7ece102016-02-02 23:23:02 +010029 except socket.error:
30 print("=== socket error ===")
31 break
32 except IOError:
33 print("=== socket closed ===")
34 break
Bram Moolenaar608a8912016-02-03 22:39:51 +010035 if received == '':
Bram Moolenaard7ece102016-02-02 23:23:02 +010036 print("=== socket closed ===")
37 break
Bram Moolenaara63cdb52016-03-20 18:24:45 +010038 print("received: {0}".format(received))
Bram Moolenaard7ece102016-02-02 23:23:02 +010039
Bram Moolenaare7bed622016-02-03 22:20:29 +010040 # We may receive two messages at once. Take the part up to the
41 # matching "]" (recognized by finding "][").
Bram Moolenaar608a8912016-02-03 22:39:51 +010042 todo = received
43 while todo != '':
44 splitidx = todo.find('][')
Bram Moolenaare7bed622016-02-03 22:20:29 +010045 if splitidx < 0:
Bram Moolenaar608a8912016-02-03 22:39:51 +010046 used = todo
47 todo = ''
Bram Moolenaard7ece102016-02-02 23:23:02 +010048 else:
Bram Moolenaar608a8912016-02-03 22:39:51 +010049 used = todo[:splitidx + 1]
50 todo = todo[splitidx + 1:]
51 if used != received:
Bram Moolenaara63cdb52016-03-20 18:24:45 +010052 print("using: {0}".format(used))
Bram Moolenaard7ece102016-02-02 23:23:02 +010053
Bram Moolenaare7bed622016-02-03 22:20:29 +010054 try:
Bram Moolenaar608a8912016-02-03 22:39:51 +010055 decoded = json.loads(used)
Bram Moolenaare7bed622016-02-03 22:20:29 +010056 except ValueError:
57 print("json decoding failed")
58 decoded = [-1, '']
Bram Moolenaard7ece102016-02-02 23:23:02 +010059
Bram Moolenaare7bed622016-02-03 22:20:29 +010060 # Send a response if the sequence number is positive.
61 if decoded[0] >= 0:
62 if decoded[1] == 'hello!':
63 # simply send back a string
64 response = "got it"
Bram Moolenaard6547fc2016-03-03 19:35:02 +010065 elif decoded[1].startswith("echo "):
66 # send back the argument
67 response = decoded[1][5:]
Bram Moolenaare7bed622016-02-03 22:20:29 +010068 elif decoded[1] == 'make change':
Bram Moolenaar66624ff2016-02-03 23:59:43 +010069 # Send two ex commands at the same time, before
70 # replying to the request.
Bram Moolenaare7bed622016-02-03 22:20:29 +010071 cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
72 cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +010073 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +010074 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +010075 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +010076 elif decoded[1] == 'do normal':
77 # Send a normal command.
78 cmd = '["normal","G$s more\u001b"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +010079 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +010080 self.request.sendall(cmd.encode('utf-8'))
81 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +010082 elif decoded[1] == 'eval-works':
83 # Send an eval request. We ignore the response.
Bram Moolenaarece61b02016-02-20 21:39:05 +010084 cmd = '["expr","\\"foo\\" . 123", -1]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +010085 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +010086 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +010087 response = "ok"
88 elif decoded[1] == 'eval-fails':
89 # Send an eval request that will fail.
Bram Moolenaarece61b02016-02-20 21:39:05 +010090 cmd = '["expr","xxx", -2]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +010091 print("sending: {0}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +010092 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +010093 response = "ok"
Bram Moolenaar55fab432016-02-07 16:53:13 +010094 elif decoded[1] == 'eval-error':
95 # Send an eval request that works but the result can't
96 # be encoded.
Bram Moolenaarece61b02016-02-20 21:39:05 +010097 cmd = '["expr","function(\\"tr\\")", -3]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +010098 print("sending: {0}".format(cmd))
Bram Moolenaar55fab432016-02-07 16:53:13 +010099 self.request.sendall(cmd.encode('utf-8'))
100 response = "ok"
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100101 elif decoded[1] == 'eval-bad':
102 # Send an eval request missing the third argument.
Bram Moolenaarece61b02016-02-20 21:39:05 +0100103 cmd = '["expr","xxx"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100104 print("sending: {0}".format(cmd))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100105 self.request.sendall(cmd.encode('utf-8'))
106 response = "ok"
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100107 elif decoded[1] == 'malformed1':
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100108 cmd = '["ex",":"]wrong!["ex","smi"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100109 print("sending: {0}".format(cmd))
Bram Moolenaarac74d5e2016-03-20 14:31:00 +0100110 self.request.sendall(cmd.encode('utf-8'))
111 response = "ok"
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100112 elif decoded[1] == 'malformed2':
113 cmd = '"unterminated string'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100114 print("sending: {0}".format(cmd))
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100115 self.request.sendall(cmd.encode('utf-8'))
116 response = "ok"
117 # Need to wait for Vim to give up, otherwise the double
118 # quote in the "ok" response terminates the string.
119 time.sleep(0.2)
120 elif decoded[1] == 'malformed3':
121 cmd = '["ex","missing ]"'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100122 print("sending: {0}".format(cmd))
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100123 self.request.sendall(cmd.encode('utf-8'))
124 response = "ok"
125 # Need to wait for Vim to give up, otherwise the ]
126 # in the "ok" response terminates the list.
127 time.sleep(0.2)
128 elif decoded[1] == 'split':
129 cmd = '["ex","let '
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100130 print("sending: {0}".format(cmd))
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100131 self.request.sendall(cmd.encode('utf-8'))
132 time.sleep(0.01)
133 cmd = 'g:split = 123"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100134 print("sending: {0}".format(cmd))
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100135 self.request.sendall(cmd.encode('utf-8'))
136 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100137 elif decoded[1] == 'an expr':
138 # Send an expr request.
139 cmd = '["expr","setline(\\"$\\", [\\"one\\",\\"two\\",\\"three\\"])"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100140 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100141 self.request.sendall(cmd.encode('utf-8'))
142 response = "ok"
Bram Moolenaarece61b02016-02-20 21:39:05 +0100143 elif decoded[1] == 'call-func':
144 cmd = '["call","MyFunction",[1,2,3], 0]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100145 print("sending: {0}".format(cmd))
Bram Moolenaarece61b02016-02-20 21:39:05 +0100146 self.request.sendall(cmd.encode('utf-8'))
147 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100148 elif decoded[1] == 'redraw':
149 cmd = '["redraw",""]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100150 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100151 self.request.sendall(cmd.encode('utf-8'))
152 response = "ok"
153 elif decoded[1] == 'redraw!':
154 cmd = '["redraw","force"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100155 print("sending: {0}".format(cmd))
Bram Moolenaarf4160862016-02-05 23:09:12 +0100156 self.request.sendall(cmd.encode('utf-8'))
157 response = "ok"
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100158 elif decoded[1] == 'empty-request':
159 cmd = '[]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100160 print("sending: {0}".format(cmd))
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100161 self.request.sendall(cmd.encode('utf-8'))
162 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100163 elif decoded[1] == 'eval-result':
164 # Send back the last received eval result.
165 response = last_eval
Bram Moolenaarf6157282016-02-10 21:07:14 +0100166 elif decoded[1] == 'call me':
167 cmd = '[0,"we called you"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100168 print("sending: {0}".format(cmd))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100169 self.request.sendall(cmd.encode('utf-8'))
170 response = "ok"
171 elif decoded[1] == 'call me again':
172 cmd = '[0,"we did call you"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100173 print("sending: {0}".format(cmd))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100174 self.request.sendall(cmd.encode('utf-8'))
175 response = ""
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100176 elif decoded[1] == 'send zero':
177 cmd = '[0,"zero index"]'
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100178 print("sending: {0}".format(cmd))
Bram Moolenaar5983ad02016-03-05 20:54:36 +0100179 self.request.sendall(cmd.encode('utf-8'))
180 response = "sent zero"
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100181 elif decoded[1] == 'close me':
182 print("closing")
183 self.request.close()
184 response = ""
Bram Moolenaarece61b02016-02-20 21:39:05 +0100185 elif decoded[1] == 'wait a bit':
186 time.sleep(0.2)
187 response = "waited"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100188 elif decoded[1] == '!quit!':
189 # we're done
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100190 self.server.shutdown()
Bram Moolenaarb92abad2016-02-08 22:37:24 +0100191 return
Bram Moolenaare7bed622016-02-03 22:20:29 +0100192 elif decoded[1] == '!crash!':
193 # Crash!
194 42 / 0
195 else:
196 response = "what?"
197
Bram Moolenaarf6157282016-02-10 21:07:14 +0100198 if response == "":
199 print("no response")
200 else:
201 encoded = json.dumps([decoded[0], response])
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100202 print("sending: {0}".format(encoded))
Bram Moolenaarf6157282016-02-10 21:07:14 +0100203 self.request.sendall(encoded.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100204
205 # Negative numbers are used for "eval" responses.
206 elif decoded[0] < 0:
207 last_eval = decoded
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100208
Bram Moolenaard7ece102016-02-02 23:23:02 +0100209class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
210 pass
211
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100212def writePortInFile(port):
213 # Write the port number in Xportnr, so that the test knows it.
214 f = open("Xportnr", "w")
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100215 f.write("{0}".format(port))
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100216 f.close()
217
Bram Moolenaard7ece102016-02-02 23:23:02 +0100218if __name__ == "__main__":
219 HOST, PORT = "localhost", 0
220
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100221 # Wait half a second before opening the port to test waittime in ch_open().
222 # We do want to get the port number, get that first. We cannot open the
223 # socket, guess a port is free.
224 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
225 PORT = 13684
226 writePortInFile(PORT)
227
228 print("Wait for it...")
229 time.sleep(0.5)
230
Bram Moolenaard7ece102016-02-02 23:23:02 +0100231 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
232 ip, port = server.server_address
233
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100234 # Start a thread with the server. That thread will then start a new thread
235 # for each connection.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100236 server_thread = threading.Thread(target=server.serve_forever)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100237 server_thread.start()
238
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100239 writePortInFile(port)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100240
Bram Moolenaara63cdb52016-03-20 18:24:45 +0100241 print("Listening on port {0}".format(port))
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100242
243 # Main thread terminates, but the server continues running
244 # until server.shutdown() is called.
Bram Moolenaarddbe7d22016-02-20 18:26:48 +0100245 try:
246 while server_thread.isAlive():
247 server_thread.join(1)
248 except (KeyboardInterrupt, SystemExit):
249 server.shutdown()