summaryrefslogtreecommitdiff
path: root/runtime/tools/demoserver.py
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-01-28 22:37:01 +0100
committerBram Moolenaar <Bram@vim.org>2016-01-28 22:37:01 +0100
commit3b5f929b18492fec291d1ec95a91f54e5912c03b (patch)
treefb095c24b14ab4d9470c7eccc966ee27f0953e21 /runtime/tools/demoserver.py
parentba59ddbd3642d02614acbe52694e3e8a78c0e9d3 (diff)
downloadvim-3b5f929b18492fec291d1ec95a91f54e5912c03b.zip
patch 7.4.1191
Problem: The channel feature isn't working yet. Solution: Add the connect(), disconnect(), sendexpr() and sendraw() functions. Add initial documentation. Add a demo server.
Diffstat (limited to 'runtime/tools/demoserver.py')
-rw-r--r--runtime/tools/demoserver.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/runtime/tools/demoserver.py b/runtime/tools/demoserver.py
new file mode 100644
index 000000000..c72a58b73
--- /dev/null
+++ b/runtime/tools/demoserver.py
@@ -0,0 +1,87 @@
+#!/usr/bin/python
+# Server that will accept connections from a Vim channel.
+# Run this server and then in Vim you can open the channel:
+# :let handle = connect('localhost:8765', 'json')
+#
+# Then Vim can send requests to the server:
+# :let response = sendexpr(handle, 'hello!')
+#
+# And you can control Vim by typing a JSON message here, e.g.:
+# ["ex","echo 'hi there'"]
+#
+# See ":help channel-demo" in Vim.
+
+import SocketServer
+import json
+import socket
+import sys
+import threading
+
+thesocket = None
+
+class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
+
+ def handle(self):
+ print "=== socket opened ==="
+ global thesocket
+ thesocket = self.request
+ while True:
+ try:
+ data = self.request.recv(4096)
+ except socket.error:
+ print "=== socket error ==="
+ break
+ except IOError:
+ print "=== socket closed ==="
+ break
+ if data == '':
+ print "=== socket closed ==="
+ break
+ print "received: {}".format(data)
+ try:
+ decoded = json.loads(data)
+ except ValueError:
+ print "json decoding failed"
+ decoded = [0, '']
+
+ if decoded[1] == 'hello!':
+ response = "got it"
+ else:
+ response = "what?"
+ encoded = json.dumps([decoded[0], response])
+ print "sending {}".format(encoded)
+ self.request.sendall(encoded)
+ thesocket = None
+
+class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
+ pass
+
+if __name__ == "__main__":
+ HOST, PORT = "localhost", 8765
+
+ server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
+ ip, port = server.server_address
+
+ # Start a thread with the server -- that thread will then start one
+ # more thread for each request
+ server_thread = threading.Thread(target=server.serve_forever)
+
+ # Exit the server thread when the main thread terminates
+ server_thread.daemon = True
+ server_thread.start()
+ print "Server loop running in thread: ", server_thread.name
+
+ print "Listening on port {}".format(PORT)
+ while True:
+ typed = sys.stdin.readline()
+ if "quit" in typed:
+ print "Goodbye!"
+ break
+ if thesocket is None:
+ print "No socket yet"
+ else:
+ print "sending {}".format(typed)
+ thesocket.sendall(typed)
+
+ server.shutdown()
+ server.server_close()