aboutsummaryrefslogtreecommitdiff
blob: 07553337067a81d844328a116ad71aa2cd70358e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/env python
"""Graph server.

From the command-line it's easier to use sshgraphserver.py instead of this.
"""

from __future__ import print_function, absolute_import

import os, sys

PARENTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# make dotviewer importable
sys.path.insert(0, PARENTDIR)

from dotviewer import msgstruct
try:
    from cStringIO import StringIO
except ImportError:
    from io import StringIO

try:
    import thread
except ImportError:
    import _thread as thread

class Server(object):

    def __init__(self, io):
        self.io = io
        self.display = None

    def run(self, only_one_graph=False):
        # wait for the CMSG_INIT message
        msg = self.io.recvmsg()
        if msg[0] != msgstruct.CMSG_INIT or msg[1] != msgstruct.MAGIC:
            raise ValueError("bad MAGIC number")
        # process messages until we have a pygame display
        while self.display is None:
            self.process_next_message()
        # start a background thread to process further messages
        if not only_one_graph:
            thread.start_new_thread(self.process_all_messages, ())
        # give control to pygame
        self.display.run1()

    def process_all_messages(self):
        try:
            while True:
                self.process_next_message()
        except EOFError:
            from dotviewer.drawgraph import display_async_quit
            display_async_quit()

    def process_next_message(self):
        msg = self.io.recvmsg()
        fn = self.MESSAGES.get(msg[0])
        if fn:
            fn(self, *msg[1:])
        else:
            self.log("unknown message code %r" % (msg[0],))

    def log(self, info):
        print(info, file=sys.stderr)

    def setlayout(self, layout):
        if self.display is None:
            # make the initial display
            from dotviewer.graphdisplay import GraphDisplay
            self.display = GraphDisplay(layout)
        else:
            # send an async command to the display running the main thread
            from dotviewer.drawgraph import display_async_cmd
            display_async_cmd(layout=layout)

    def cmsg_start_graph(self, graph_id, scale, width, height, *rest):
        from dotviewer.drawgraph import GraphLayout
        self.newlayout = GraphLayout(float(scale), float(width), float(height))

        def request_reload():
            self.io.sendmsg(msgstruct.MSG_RELOAD, graph_id)
        def request_followlink(word):
            self.io.sendmsg(msgstruct.MSG_FOLLOW_LINK, graph_id, word)

        self.newlayout.request_reload = request_reload
        self.newlayout.request_followlink = request_followlink

    def cmsg_add_node(self, *args):
        self.newlayout.add_node(*args)

    def cmsg_add_edge(self, *args):
        self.newlayout.add_edge(*args)

    def cmsg_add_link(self, word, *info):
        if len(info) == 1:
            info = info[0]
        elif len(info) >= 4:
            info = (info[0], info[1:4])
        self.newlayout.links[word] = info

    def cmsg_fixed_font(self, *rest):
        self.newlayout.fixedfont = True

    def cmsg_stop_graph(self, *rest):
        self.setlayout(self.newlayout)
        del self.newlayout
        self.io.sendmsg(msgstruct.MSG_OK)

    def cmsg_missing_link(self, *rest):
        self.setlayout(None)

    def cmsg_say(self, errmsg, *rest):
        from drawgraph import display_async_cmd
        display_async_cmd(say=errmsg)

    MESSAGES = {
        msgstruct.CMSG_START_GRAPH: cmsg_start_graph,
        msgstruct.CMSG_ADD_NODE:    cmsg_add_node,
        msgstruct.CMSG_ADD_EDGE:    cmsg_add_edge,
        msgstruct.CMSG_ADD_LINK:    cmsg_add_link,
        msgstruct.CMSG_FIXED_FONT:  cmsg_fixed_font,
        msgstruct.CMSG_STOP_GRAPH:  cmsg_stop_graph,
        msgstruct.CMSG_MISSING_LINK:cmsg_missing_link,
        msgstruct.CMSG_SAY:         cmsg_say,
        }


def listen_server(local_address, s1=None):
    import socket, graphclient, thread
    if isinstance(local_address, str):
        if ':' in local_address:
            interface, port = local_address.split(':')
        else:
            interface, port = '', local_address
        local_address = interface, int(port)
    if s1 is None:
        s1 = socket.socket()
        s1.bind(local_address)
    s1.listen(5)
    print('listening on %r...' % (s1.getsockname(),))
    while True:
        conn, addr = s1.accept()
        print('accepted connection from %r' % (addr,))
        sock_io = msgstruct.SocketIO(conn)
        handler_io = graphclient.spawn_local_handler()
        thread.start_new_thread(copy_all, (sock_io, handler_io))
        thread.start_new_thread(copy_all, (handler_io, sock_io))
        del sock_io, handler_io, conn

def copy_all(io1, io2):
    try:
        while True:
            io2.sendall(io1.recv())
    except EOFError:
        io2.close_sending()


if __name__ == '__main__':
    if len(sys.argv) != 2:
        if len(sys.argv) == 1:
            # start locally
            import sshgraphserver
            sshgraphserver.ssh_graph_server(['LOCAL'])
            sys.exit(0)
        print(__doc__, file=sys.stderr)
        sys.exit(2)
    if sys.argv[1] == '--stdio':
        # a one-shot server running on stdin/stdout
        io = msgstruct.FileIO(getattr(sys.stdin, 'buffer', sys.stdin),
                              getattr(sys.stdout, 'buffer', sys.stdout))
        srv = Server(io)
        try:
            srv.run()
        except Exception as e:
            import traceback
            f = StringIO()
            traceback.print_exc(file=f)
            # try to add some explanations
            help = (" | if you want to debug on a remote machine, see\n"
                    " | instructions in dotviewer/sshgraphserver.py\n")
            try:
                os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
                import pygame
                if isinstance(e, pygame.error):
                    print(help, file=f)
            except Exception as e:
                f.seek(0)
                f.truncate()
                print("%s: %s" % (e.__class__.__name__, e), file=f)
                print(" | Pygame is not installed; either install it, or", file=f)
                print(help, file=f)
            io.sendmsg(msgstruct.MSG_ERROR, f.getvalue())
    else:
        listen_server(sys.argv[1])