xref: /vim-8.2.3635/src/testdir/test_netbeans.py (revision fb094e14)
1#!/usr/bin/python
2#
3# Server that will communicate with Vim through the netbeans interface.
4# Used by test_netbeans.vim.
5#
6# This requires Python 2.6 or later.
7
8from __future__ import print_function
9import socket
10import sys
11import time
12import threading
13
14try:
15    # Python 3
16    import socketserver
17except ImportError:
18    # Python 2
19    import SocketServer as socketserver
20
21class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
22
23    def handle(self):
24        print("=== socket opened ===")
25        while True:
26            try:
27                received = self.request.recv(4096).decode('utf-8')
28            except socket.error:
29                print("=== socket error ===")
30                break
31            except IOError:
32                print("=== socket closed ===")
33                break
34            if received == '':
35                print("=== socket closed ===")
36                break
37            print("received: {0}".format(received))
38
39            # Write the received line into the file, so that the test can check
40            # what happened.
41            with open("Xnetbeans", "a") as myfile:
42                myfile.write(received)
43
44            response = ''
45            if received.find('README.txt') > 0:
46                name = received.split('"')[1]
47                response = '5:putBufferNumber!33 "' + name + '"\n'
48                response += '5:setDot!1 3/19\n'
49            elif received.find('disconnect') > 0:
50                # we're done
51                self.server.shutdown()
52                return
53
54            if len(response) > 0:
55                self.request.sendall(response.encode('utf-8'))
56                # Write the respoinse into the file, so that the test can knows
57                # the command was sent.
58                with open("Xnetbeans", "a") as myfile:
59                    myfile.write('send: ' + response)
60
61class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
62    pass
63
64def writePortInFile(port):
65    # Write the port number in Xportnr, so that the test knows it.
66    f = open("Xportnr", "w")
67    f.write("{0}".format(port))
68    f.close()
69
70if __name__ == "__main__":
71    HOST, PORT = "localhost", 0
72
73    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
74    ip, port = server.server_address
75
76    # Start a thread with the server.  That thread will then start a new thread
77    # for each connection.
78    server_thread = threading.Thread(target=server.serve_forever)
79    server_thread.start()
80
81    writePortInFile(port)
82
83    print("Listening on port {0}".format(port))
84
85    # Main thread terminates, but the server continues running
86    # until server.shutdown() is called.
87    try:
88        while server_thread.isAlive():
89            server_thread.join(1)
90    except (KeyboardInterrupt, SystemExit):
91        server.shutdown()
92