Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Server that will accept connections from a Vim channel. |
| 4 | # Run this server and then in Vim you can open the channel: |
| 5 | # :let handle = ch_open('localhost:8765', 'json') |
| 6 | # |
| 7 | # Then Vim can send requests to the server: |
| 8 | # :let response = ch_sendexpr(handle, 'hello!') |
| 9 | # |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 10 | # See ":help channel-demo" in Vim. |
| 11 | # |
| 12 | # This requires Python 2.6 or later. |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | import json |
| 16 | import socket |
| 17 | import sys |
| 18 | import threading |
| 19 | |
| 20 | try: |
| 21 | # Python 3 |
| 22 | import socketserver |
| 23 | except ImportError: |
| 24 | # Python 2 |
| 25 | import SocketServer as socketserver |
| 26 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 27 | class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): |
| 28 | |
| 29 | def handle(self): |
| 30 | print("=== socket opened ===") |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 31 | while True: |
| 32 | try: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 33 | received = self.request.recv(4096).decode('utf-8') |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 34 | except socket.error: |
| 35 | print("=== socket error ===") |
| 36 | break |
| 37 | except IOError: |
| 38 | print("=== socket closed ===") |
| 39 | break |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 40 | if received == '': |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 41 | print("=== socket closed ===") |
| 42 | break |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 43 | print("received: {}".format(received)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 44 | |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 45 | # We may receive two messages at once. Take the part up to the |
| 46 | # matching "]" (recognized by finding "]["). |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 47 | todo = received |
| 48 | while todo != '': |
| 49 | splitidx = todo.find('][') |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 50 | if splitidx < 0: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 51 | used = todo |
| 52 | todo = '' |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 53 | else: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 54 | used = todo[:splitidx + 1] |
| 55 | todo = todo[splitidx + 1:] |
| 56 | if used != received: |
| 57 | print("using: {}".format(used)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 58 | |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 59 | try: |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 60 | decoded = json.loads(used) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 61 | except ValueError: |
| 62 | print("json decoding failed") |
| 63 | decoded = [-1, ''] |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 64 | |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 65 | # Send a response if the sequence number is positive. |
| 66 | if decoded[0] >= 0: |
| 67 | if decoded[1] == 'hello!': |
| 68 | # simply send back a string |
| 69 | response = "got it" |
| 70 | elif decoded[1] == 'make change': |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame^] | 71 | # Send two ex commands at the same time, before |
| 72 | # replying to the request. |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 73 | cmd = '["ex","call append(\\"$\\",\\"added1\\")"]' |
| 74 | cmd += '["ex","call append(\\"$\\",\\"added2\\")"]' |
| 75 | print("sending: {}".format(cmd)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 76 | self.request.sendall(cmd.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 77 | response = "ok" |
| 78 | elif decoded[1] == 'eval-works': |
| 79 | # Send an eval request. We ignore the response. |
| 80 | cmd = '["eval","\\"foo\\" . 123", -1]' |
| 81 | print("sending: {}".format(cmd)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 82 | self.request.sendall(cmd.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 83 | response = "ok" |
| 84 | elif decoded[1] == 'eval-fails': |
| 85 | # Send an eval request that will fail. |
| 86 | cmd = '["eval","xxx", -2]' |
| 87 | print("sending: {}".format(cmd)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 88 | self.request.sendall(cmd.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 89 | response = "ok" |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame^] | 90 | elif decoded[1] == 'eval-bad': |
| 91 | # Send an eval request missing the third argument. |
| 92 | cmd = '["eval","xxx"]' |
| 93 | print("sending: {}".format(cmd)) |
| 94 | self.request.sendall(cmd.encode('utf-8')) |
| 95 | response = "ok" |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 96 | elif decoded[1] == 'eval-result': |
| 97 | # Send back the last received eval result. |
| 98 | response = last_eval |
| 99 | elif decoded[1] == '!quit!': |
| 100 | # we're done |
| 101 | sys.exit(0) |
| 102 | elif decoded[1] == '!crash!': |
| 103 | # Crash! |
| 104 | 42 / 0 |
| 105 | else: |
| 106 | response = "what?" |
| 107 | |
| 108 | encoded = json.dumps([decoded[0], response]) |
| 109 | print("sending: {}".format(encoded)) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 110 | self.request.sendall(encoded.encode('utf-8')) |
Bram Moolenaar | e7bed62 | 2016-02-03 22:20:29 +0100 | [diff] [blame] | 111 | |
| 112 | # Negative numbers are used for "eval" responses. |
| 113 | elif decoded[0] < 0: |
| 114 | last_eval = decoded |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 115 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 116 | class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
| 117 | pass |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | HOST, PORT = "localhost", 0 |
| 121 | |
| 122 | server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) |
| 123 | ip, port = server.server_address |
| 124 | |
| 125 | # Start a thread with the server -- that thread will then start one |
| 126 | # more thread for each request |
| 127 | server_thread = threading.Thread(target=server.serve_forever) |
| 128 | |
| 129 | # Exit the server thread when the main thread terminates |
| 130 | server_thread.daemon = True |
| 131 | server_thread.start() |
| 132 | |
| 133 | # Write the port number in Xportnr, so that the test knows it. |
| 134 | f = open("Xportnr", "w") |
| 135 | f.write("{}".format(port)) |
| 136 | f.close() |
| 137 | |
| 138 | # Block here |
| 139 | print("Listening on port {}".format(port)) |
| 140 | server.serve_forever() |