blob: 9a3c9213ad5e2c3aa961b7c5b3cb0ab27c708791 [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 Moolenaar608a8912016-02-03 22:39:51 +010038 print("received: {}".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:
52 print("using: {}".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"
65 elif decoded[1] == 'make change':
Bram Moolenaar66624ff2016-02-03 23:59:43 +010066 # Send two ex commands at the same time, before
67 # replying to the request.
Bram Moolenaare7bed622016-02-03 22:20:29 +010068 cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
69 cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
70 print("sending: {}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +010071 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +010072 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +010073 elif decoded[1] == 'do normal':
74 # Send a normal command.
75 cmd = '["normal","G$s more\u001b"]'
76 print("sending: {}".format(cmd))
77 self.request.sendall(cmd.encode('utf-8'))
78 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +010079 elif decoded[1] == 'eval-works':
80 # Send an eval request. We ignore the response.
Bram Moolenaarece61b02016-02-20 21:39:05 +010081 cmd = '["expr","\\"foo\\" . 123", -1]'
Bram Moolenaare7bed622016-02-03 22:20:29 +010082 print("sending: {}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +010083 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +010084 response = "ok"
85 elif decoded[1] == 'eval-fails':
86 # Send an eval request that will fail.
Bram Moolenaarece61b02016-02-20 21:39:05 +010087 cmd = '["expr","xxx", -2]'
Bram Moolenaare7bed622016-02-03 22:20:29 +010088 print("sending: {}".format(cmd))
Bram Moolenaar3b05b132016-02-03 23:25:07 +010089 self.request.sendall(cmd.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +010090 response = "ok"
Bram Moolenaar55fab432016-02-07 16:53:13 +010091 elif decoded[1] == 'eval-error':
92 # Send an eval request that works but the result can't
93 # be encoded.
Bram Moolenaarece61b02016-02-20 21:39:05 +010094 cmd = '["expr","function(\\"tr\\")", -3]'
Bram Moolenaar55fab432016-02-07 16:53:13 +010095 print("sending: {}".format(cmd))
96 self.request.sendall(cmd.encode('utf-8'))
97 response = "ok"
Bram Moolenaar66624ff2016-02-03 23:59:43 +010098 elif decoded[1] == 'eval-bad':
99 # Send an eval request missing the third argument.
Bram Moolenaarece61b02016-02-20 21:39:05 +0100100 cmd = '["expr","xxx"]'
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100101 print("sending: {}".format(cmd))
102 self.request.sendall(cmd.encode('utf-8'))
103 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100104 elif decoded[1] == 'an expr':
105 # Send an expr request.
106 cmd = '["expr","setline(\\"$\\", [\\"one\\",\\"two\\",\\"three\\"])"]'
107 print("sending: {}".format(cmd))
108 self.request.sendall(cmd.encode('utf-8'))
109 response = "ok"
Bram Moolenaarece61b02016-02-20 21:39:05 +0100110 elif decoded[1] == 'call-func':
111 cmd = '["call","MyFunction",[1,2,3], 0]'
112 print("sending: {}".format(cmd))
113 self.request.sendall(cmd.encode('utf-8'))
114 response = "ok"
Bram Moolenaarf4160862016-02-05 23:09:12 +0100115 elif decoded[1] == 'redraw':
116 cmd = '["redraw",""]'
117 print("sending: {}".format(cmd))
118 self.request.sendall(cmd.encode('utf-8'))
119 response = "ok"
120 elif decoded[1] == 'redraw!':
121 cmd = '["redraw","force"]'
122 print("sending: {}".format(cmd))
123 self.request.sendall(cmd.encode('utf-8'))
124 response = "ok"
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100125 elif decoded[1] == 'empty-request':
126 cmd = '[]'
127 print("sending: {}".format(cmd))
128 self.request.sendall(cmd.encode('utf-8'))
129 response = "ok"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100130 elif decoded[1] == 'eval-result':
131 # Send back the last received eval result.
132 response = last_eval
Bram Moolenaarf6157282016-02-10 21:07:14 +0100133 elif decoded[1] == 'call me':
134 cmd = '[0,"we called you"]'
135 print("sending: {}".format(cmd))
136 self.request.sendall(cmd.encode('utf-8'))
137 response = "ok"
138 elif decoded[1] == 'call me again':
139 cmd = '[0,"we did call you"]'
140 print("sending: {}".format(cmd))
141 self.request.sendall(cmd.encode('utf-8'))
142 response = ""
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100143 elif decoded[1] == 'close me':
144 print("closing")
145 self.request.close()
146 response = ""
Bram Moolenaarece61b02016-02-20 21:39:05 +0100147 elif decoded[1] == 'wait a bit':
148 time.sleep(0.2)
149 response = "waited"
Bram Moolenaare7bed622016-02-03 22:20:29 +0100150 elif decoded[1] == '!quit!':
151 # we're done
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100152 self.server.shutdown()
Bram Moolenaarb92abad2016-02-08 22:37:24 +0100153 return
Bram Moolenaare7bed622016-02-03 22:20:29 +0100154 elif decoded[1] == '!crash!':
155 # Crash!
156 42 / 0
157 else:
158 response = "what?"
159
Bram Moolenaarf6157282016-02-10 21:07:14 +0100160 if response == "":
161 print("no response")
162 else:
163 encoded = json.dumps([decoded[0], response])
164 print("sending: {}".format(encoded))
165 self.request.sendall(encoded.encode('utf-8'))
Bram Moolenaare7bed622016-02-03 22:20:29 +0100166
167 # Negative numbers are used for "eval" responses.
168 elif decoded[0] < 0:
169 last_eval = decoded
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100170
Bram Moolenaard7ece102016-02-02 23:23:02 +0100171class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
172 pass
173
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100174def writePortInFile(port):
175 # Write the port number in Xportnr, so that the test knows it.
176 f = open("Xportnr", "w")
177 f.write("{}".format(port))
178 f.close()
179
Bram Moolenaard7ece102016-02-02 23:23:02 +0100180if __name__ == "__main__":
181 HOST, PORT = "localhost", 0
182
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100183 # Wait half a second before opening the port to test waittime in ch_open().
184 # We do want to get the port number, get that first. We cannot open the
185 # socket, guess a port is free.
186 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
187 PORT = 13684
188 writePortInFile(PORT)
189
190 print("Wait for it...")
191 time.sleep(0.5)
192
Bram Moolenaard7ece102016-02-02 23:23:02 +0100193 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
194 ip, port = server.server_address
195
Bram Moolenaar6076fe12016-02-05 22:49:56 +0100196 # Start a thread with the server. That thread will then start a new thread
197 # for each connection.
Bram Moolenaard7ece102016-02-02 23:23:02 +0100198 server_thread = threading.Thread(target=server.serve_forever)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100199 server_thread.start()
200
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100201 writePortInFile(port)
Bram Moolenaard7ece102016-02-02 23:23:02 +0100202
Bram Moolenaard7ece102016-02-02 23:23:02 +0100203 print("Listening on port {}".format(port))
Bram Moolenaarb3e2f002016-02-04 00:11:37 +0100204
205 # Main thread terminates, but the server continues running
206 # until server.shutdown() is called.
Bram Moolenaarddbe7d22016-02-20 18:26:48 +0100207 try:
208 while server_thread.isAlive():
209 server_thread.join(1)
210 except (KeyboardInterrupt, SystemExit):
211 server.shutdown()