| 1 |
|
| 2 |
"""Graph server. |
| 3 |
|
| 4 |
From the command-line it's easier to use sshgraphserver.py instead of this. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import print_function, absolute_import |
| 8 |
|
| 9 |
import os, sys |
| 10 |
|
| 11 |
PARENTDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 12 |
|
| 13 |
|
| 14 |
sys.path.insert(0, PARENTDIR) |
| 15 |
|
| 16 |
from dotviewer import msgstruct |
| 17 |
try: |
| 18 |
from cStringIO import StringIO |
| 19 |
except ImportError: |
| 20 |
from io import StringIO |
| 21 |
|
| 22 |
try: |
| 23 |
import thread |
| 24 |
except ImportError: |
| 25 |
import _thread as thread |
| 26 |
|
| 27 |
class Server(object): |
| 28 |
|
| 29 |
def __init__(self, io): |
| 30 |
self.io = io |
| 31 |
self.display = None |
| 32 |
|
| 33 |
def run(self, only_one_graph=False): |
| 34 |
|
| 35 |
msg = self.io.recvmsg() |
| 36 |
if msg[0] != msgstruct.CMSG_INIT or msg[1] != msgstruct.MAGIC: |
| 37 |
raise ValueError("bad MAGIC number") |
| 38 |
|
| 39 |
while self.display is None: |
| 40 |
self.process_next_message() |
| 41 |
|
| 42 |
if not only_one_graph: |
| 43 |
thread.start_new_thread(self.process_all_messages, ()) |
| 44 |
|
| 45 |
self.display.run1() |
| 46 |
|
| 47 |
def process_all_messages(self): |
| 48 |
try: |
| 49 |
while True: |
| 50 |
self.process_next_message() |
| 51 |
except EOFError: |
| 52 |
from dotviewer.drawgraph import display_async_quit |
| 53 |
display_async_quit() |
| 54 |
|
| 55 |
def process_next_message(self): |
| 56 |
msg = self.io.recvmsg() |
| 57 |
fn = self.MESSAGES.get(msg[0]) |
| 58 |
if fn: |
| 59 |
fn(self, *msg[1:]) |
| 60 |
else: |
| 61 |
self.log("unknown message code %r" % (msg[0],)) |
| 62 |
|
| 63 |
def log(self, info): |
| 64 |
print(info, file=sys.stderr) |
| 65 |
|
| 66 |
def setlayout(self, layout): |
| 67 |
if self.display is None: |
| 68 |
|
| 69 |
from dotviewer.graphdisplay import GraphDisplay |
| 70 |
self.display = GraphDisplay(layout) |
| 71 |
else: |
| 72 |
|
| 73 |
from dotviewer.drawgraph import display_async_cmd |
| 74 |
display_async_cmd(layout=layout) |
| 75 |
|
| 76 |
def cmsg_start_graph(self, graph_id, scale, width, height, *rest): |
| 77 |
from dotviewer.drawgraph import GraphLayout |
| 78 |
self.newlayout = GraphLayout(float(scale), float(width), float(height)) |
| 79 |
|
| 80 |
def request_reload(): |
| 81 |
self.io.sendmsg(msgstruct.MSG_RELOAD, graph_id) |
| 82 |
def request_followlink(word): |
| 83 |
self.io.sendmsg(msgstruct.MSG_FOLLOW_LINK, graph_id, word) |
| 84 |
|
| 85 |
self.newlayout.request_reload = request_reload |
| 86 |
self.newlayout.request_followlink = request_followlink |
| 87 |
|
| 88 |
def cmsg_add_node(self, *args): |
| 89 |
self.newlayout.add_node(*args) |
| 90 |
|
| 91 |
def cmsg_add_edge(self, *args): |
| 92 |
self.newlayout.add_edge(*args) |
| 93 |
|
| 94 |
def cmsg_add_link(self, word, *info): |
| 95 |
if len(info) == 1: |
| 96 |
info = info[0] |
| 97 |
elif len(info) >= 4: |
| 98 |
info = (info[0], info[1:4]) |
| 99 |
self.newlayout.links[word] = info |
| 100 |
|
| 101 |
def cmsg_fixed_font(self, *rest): |
| 102 |
self.newlayout.fixedfont = True |
| 103 |
|
| 104 |
def cmsg_stop_graph(self, *rest): |
| 105 |
self.setlayout(self.newlayout) |
| 106 |
del self.newlayout |
| 107 |
self.io.sendmsg(msgstruct.MSG_OK) |
| 108 |
|
| 109 |
def cmsg_missing_link(self, *rest): |
| 110 |
self.setlayout(None) |
| 111 |
|
| 112 |
def cmsg_say(self, errmsg, *rest): |
| 113 |
from drawgraph import display_async_cmd |
| 114 |
display_async_cmd(say=errmsg) |
| 115 |
|
| 116 |
MESSAGES = { |
| 117 |
msgstruct.CMSG_START_GRAPH: cmsg_start_graph, |
| 118 |
msgstruct.CMSG_ADD_NODE: cmsg_add_node, |
| 119 |
msgstruct.CMSG_ADD_EDGE: cmsg_add_edge, |
| 120 |
msgstruct.CMSG_ADD_LINK: cmsg_add_link, |
| 121 |
msgstruct.CMSG_FIXED_FONT: cmsg_fixed_font, |
| 122 |
msgstruct.CMSG_STOP_GRAPH: cmsg_stop_graph, |
| 123 |
msgstruct.CMSG_MISSING_LINK:cmsg_missing_link, |
| 124 |
msgstruct.CMSG_SAY: cmsg_say, |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
def listen_server(local_address, s1=None): |
| 129 |
import socket, graphclient |
| 130 |
if isinstance(local_address, str): |
| 131 |
if ':' in local_address: |
| 132 |
interface, port = local_address.split(':') |
| 133 |
else: |
| 134 |
interface, port = '', local_address |
| 135 |
local_address = interface, int(port) |
| 136 |
if s1 is None: |
| 137 |
s1 = socket.socket() |
| 138 |
s1.bind(local_address) |
| 139 |
s1.listen(5) |
| 140 |
print('listening on %r...' % (s1.getsockname(),)) |
| 141 |
while True: |
| 142 |
conn, addr = s1.accept() |
| 143 |
print('accepted connection from %r' % (addr,)) |
| 144 |
sock_io = msgstruct.SocketIO(conn) |
| 145 |
handler_io = graphclient.spawn_local_handler() |
| 146 |
thread.start_new_thread(copy_all, (sock_io, handler_io)) |
| 147 |
thread.start_new_thread(copy_all, (handler_io, sock_io)) |
| 148 |
del sock_io, handler_io, conn |
| 149 |
|
| 150 |
def copy_all(io1, io2): |
| 151 |
try: |
| 152 |
while True: |
| 153 |
io2.sendall(io1.recv()) |
| 154 |
except EOFError: |
| 155 |
io2.close_sending() |
| 156 |
|
| 157 |
|
| 158 |
if __name__ == '__main__': |
| 159 |
if len(sys.argv) != 2: |
| 160 |
if len(sys.argv) == 1: |
| 161 |
|
| 162 |
import sshgraphserver |
| 163 |
sshgraphserver.ssh_graph_server(['LOCAL']) |
| 164 |
sys.exit(0) |
| 165 |
print(__doc__, file=sys.stderr) |
| 166 |
sys.exit(2) |
| 167 |
if sys.argv[1] == '--stdio': |
| 168 |
|
| 169 |
io = msgstruct.FileIO(getattr(sys.stdin, 'buffer', sys.stdin), |
| 170 |
getattr(sys.stdout, 'buffer', sys.stdout)) |
| 171 |
srv = Server(io) |
| 172 |
try: |
| 173 |
srv.run() |
| 174 |
except Exception as e: |
| 175 |
import traceback |
| 176 |
f = StringIO() |
| 177 |
traceback.print_exc(file=f) |
| 178 |
|
| 179 |
help = (" | if you want to debug on a remote machine, see\n" |
| 180 |
" | instructions in dotviewer/sshgraphserver.py\n") |
| 181 |
try: |
| 182 |
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" |
| 183 |
import pygame |
| 184 |
if isinstance(e, pygame.error): |
| 185 |
print(help, file=f) |
| 186 |
except Exception as e: |
| 187 |
f.seek(0) |
| 188 |
f.truncate() |
| 189 |
print("%s: %s" % (e.__class__.__name__, e), file=f) |
| 190 |
print(" | Pygame is not installed; either install it, or", file=f) |
| 191 |
print(help, file=f) |
| 192 |
io.sendmsg(msgstruct.MSG_ERROR, f.getvalue()) |
| 193 |
else: |
| 194 |
listen_server(sys.argv[1]) |