/
ncrsc.py
executable file
·155 lines (139 loc) · 5.25 KB
/
ncrsc.py
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
#!/usr/bin/python2.7
import pyrs.comms
import pyrs.rpc
import pyrs.msgs
import curses
import sys
import time
#import ncrsc classes
import status
import nhelp
import download
import upload
import chat
# Message Definitions.
from pyrs.proto import core_pb2
from pyrs.proto import system_pb2
from pyrs.proto import chat_pb2
import pyrs.test.auth
auth = pyrs.test.auth.Auth()
class MAIN(object):
def __init__(self, stdscr, comms):
# Construct a Msg Parser.
self.TIMEOUT = 0.5
self.parser = pyrs.msgs.RpcMsgs()
self.rs = pyrs.rpc.RsRpc(comms)
self.stdscr = stdscr
self.comms = comms
self.t = time.time()
self.my,self.mx = self.stdscr.getmaxyx()
self.statuswin = curses.newwin(7, self.mx-4, 1, 2); self.statuswin.border(0)
self.statuswin.refresh()
self.chatEvent_msgid = pyrs.msgs.constructMsgId(core_pb2.CORE, core_pb2.CHAT, chat_pb2.MsgId_EventChatMessage, True);
#construct children
self.children = []
self.children.append([nhelp.Menu(self.stdscr, self), curses.KEY_F1]) #help menu
self.children.append([download.Menu(self.stdscr, self.rs, self.parser, self), curses.KEY_F2]) #download menu
self.children.append([upload.Menu(self.stdscr, self.rs, self.parser, self), curses.KEY_F3]) #upload menu
self.children.append([chat.Menu(self.stdscr, self.rs, self.parser, self), curses.KEY_F4]) #chat menu
#set active_child to help menu
self.active_child = self.children[0]
self.build_menu()
def tick(self):
loop = True
while loop:
t=time.time()
while t-time.time() > -0.2:
c = stdscr.getch()
if c == ord('q'):
loop = False
break #exit while
elif self.menu_key(c):
self.active_child[0].menu_key(c)
if self.t-time.time() < -0.5 :
status_req = status.request_status(self.rs)
# Now iterate through all the responses.
(msg_id, msg_body) = self.rs.response(status_req, self.TIMEOUT)
if msg_body :
resp = self.parser.construct(msg_id, msg_body)
if resp :
status.print_status(self.statuswin,resp)
self.active_child[0].tick(True)
self.t = time.time()
else:
self.active_child[0].tick()
#if c >= ord('0') and c <= ord('9'): self.chat.enter_leave_lobby(c)
#elif c == ord('n'): self.chat.change_name()
def rs_response(self, req, i=0):
resp = self.rs.response(req, self.TIMEOUT)
if resp == None:
time.sleep(5)
if i == 20: return resp # ther server did probably die
return self.rs_response(req, i+1)
return resp
def menu_key(self, c):
i = 0
for child in self.children:
if c == child[1]:
self.active_child[0].end()
self.active_child = child
self.build_menu()
return False
elif child[1] == self.active_child[1]:
if c == curses.KEY_LEFT and i > 0:
self.active_child[0].end()
self.active_child = self.children[i-1]
self.build_menu()
return False
elif c == curses.KEY_RIGHT and i+1 < len(self.children):
self.active_child[0].end()
self.active_child = self.children[i+1]
self.build_menu()
return False
i += 1
return True
def build_menu(self):
self.statuswin.addstr(4, 1, "") #set cursor position
for child in self.children:
atr = curses.A_NORMAL
if child[1] == self.active_child[1]: atr = curses.A_BOLD
self.statuswin.addstr(self.get_key_string(child[1])+": "+child[0].title+" ", atr)
self.statuswin.addstr("q: exit")
self.statuswin.refresh()
def get_key_string(self, c):
if c == curses.KEY_F1: return "F1"
elif c == curses.KEY_F2: return "F2"
elif c == curses.KEY_F3: return "F3"
elif c == curses.KEY_F4: return "F4"
elif c == curses.KEY_F5: return "F5"
elif c == curses.KEY_F6: return "F6"
elif c == curses.KEY_F7: return "F7"
elif c == curses.KEY_F8: return "F8"
elif c == curses.KEY_F9: return "F9"
else: return "unknown"
#init ncurses
stdscr = curses.initscr()
stdscr.border(0); stdscr.keypad(1); curses.cbreak(); curses.noecho()
#open connection
stdscr.addstr(2, 2, "connecting to server...")
stdscr.refresh()
comms = pyrs.comms.SSHcomms(auth.user, auth.pwd, auth.host, auth.port)
comms.connect()
stdscr.clear()
stdscr.border(0)
stdscr.nodelay(1)
main = MAIN(stdscr, comms)
try:
main.tick()
except:
e = sys.exc_info()
stdscr.addstr(20, 2, str(e))
time.sleep(1.5)
curses.nocbreak(); stdscr.keypad(0); curses.echo()
curses.endwin()
print e
import pdb; pdb.post_mortem(e[2])
comms.close()
curses.nocbreak(); stdscr.keypad(0); curses.echo() #leave terminal in "normal" mode
comms.close()
curses.endwin()