-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFxAClient.py
220 lines (190 loc) · 6.04 KB
/
FxAClient.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
__author__ = 'alier'
from RxpSocket import RxpSocket
from RxpServerSocket import RxpServerSocket
import sys
import threading
import time
import os
class fxa_client:
def __init__(self):
self.FXAPORT = 5000
self.EMUIP = 8080
self.EMUPORT = 'localhost'
self.STATE = 'welcome'
self.DEBUG = False
self.s = RxpSocket(self.DEBUG)
self.msg = ''
self.data =''
self.MAXTIMEOUT = 3
self.finished = True
# self.thread = threading.Thread(target=self.close, args=())
# self.thread.daemon = True
# self.thread.start()
def _receive(self, s):
timeout = 0
while 1:
try:
full_msg = ''
while 1:
msg = self.s.recv(2048)
# print(msg[-5:]+"loop msg")
if msg == None:
print("Can't get message from server")
self.STATE = 'welcome'
return None
else:
full_msg = full_msg+msg
if len(full_msg)>=5 and full_msg[-5:] == "/.END":
break
if(len(full_msg)>0):
# print("full message here: " + full_msg[0:-5])
return full_msg[0:-5]
else:
return None
except Exception, e:
print "failed to receive message. please try again later" + e
timeout = timeout+1
if(timeout > self.MAXTIMEOUT):
print "time out. please try again later"
return None
def main(self):
while(1):
#set up for connect
if(self.STATE == 'welcome'):
if(len(sys.argv)<4):
print "invalid command"
break
try:
self.FXAPORT = int(sys.argv[1])
except:
print "invalid command"
if self.FXAPORT%2 != 0:
print "Please enter a valid port number that the socket should bind to (must be even)"
print "set to default 8080"
self.EMUIP = sys.argv[2]
self.EMUPORT = int(sys.argv[3])
if len(sys.argv) > 4:
if ((sys.argv[4] == 'd') or (sys.argv[4] == 'D')):
self.DEBUG = True
else:
print "Invalid debug command"
command = raw_input("Welcome. Type connect to establish connection: ")
if command == 'connect':
self.s = RxpSocket(self.DEBUG)
self.s.bind(self.EMUIP, self.EMUPORT, self.FXAPORT)
if not self.s.connect():
#this is currently wrong. It should print this when connect fails
print "can't connect to the server"
break
self.STATE = 'connect'
else:
print('type connect to establish connection')
#establish connect
if(self.STATE == 'connect'):
self.finished = True
self.msg = ''
self.data = ''
command = raw_input("It is connected to the server. Please enter a command: ")
if command == 'disconnect':
self.STATE = 'disconnect'
else:
self.msg = command.split(' ')
if(len(self.msg) == 2):
if self.msg[0] == 'get':
self.STATE = 'get'
elif self.msg[0] == 'post':
self.STATE = 'post'
elif self.msg[0] == 'window':
try:
self.s.setWindowSize(int (self.msg[1]))
self.STATE = 'window'
except Exception,e:
print"can't change window size" + e
else:
print'Invalid command. (get F, post F, window W or disconnect)'
else:
print'Invalid command. (get F, post F, window W or disconnect)'
#if user wants to post
if(self.STATE == 'post'):
self.finished = False
print("post file: " + str(self.msg[1]) + " request")
if(not os.path.isfile(str(self.msg[1]))):
print "This file does not exist"
self.STATE = 'connect'
else:
self.data = "pr " + str(self.msg[1])+"/.END"
#TODO: delete, only for debug
# self.s.send(self.data)
if self.s.send(self.data) == None:
print "Can't send post request"
self.STATE = 'connect'
else:
self.STATE = 'post_request'
#send file to server
if(self.STATE == 'post_request'):
recvMsg = self._receive(self.s)
# recvMsg = self.s.recv(2000)
if len(recvMsg) == 0 or recvMsg == None:
print "Did not receive anything from server. please try again later"
self.STATE = 'connect'
if recvMsg[0] != 'p':
print "Can't process post request. please try again later"
self.STATE = 'connect'
else:
readFile = open(str(self.msg[1]), "rb")
self.data = 'pm '+ readFile.read()
self.data = self.data + '/.END'
readFile.close()
#TODO: send() needs to return boolean
if self.s.send(self.data) == None:
print "unable to post the file, please try again later"
self.STATE = 'connect'
else:
self.STATE = 'post_complete'
#see if post is successful
if(self.STATE == 'post_complete'):
confirm = self._receive(self.s)
if(confirm != 'pcompleted'):
print "Failed to post the file, please try again later"
print "Has posted the file succesfully"
self.STATE = 'connect'
#users want to get a file
if(self.STATE == 'get'):
self.finished = False
print('get file' + str(self.msg[1]))
self.data = 'gr ' + str(self.msg[1])+ '/.END'
# self.s.send(self.data)
if self.s.send(self.data) == None:
print "Can't send get file: " + str(self.msg[1]) +" request. please try again later"
self.STATE = 'connect'
else:
recvMsg = self._receive(self.s)
if len(recvMsg) == 0 or recvMsg == None:
print "Can't download the file from the server. please try again later"
self.STATE = 'connect'
elif recvMsg[0:7] == "gfailed":
print "unable to download the file. please try again later"
elif recvMsg[0:10] != "gcompleted":
print "unable to download the file. please try again later"
else:
try:
readFile = open("cli" + str(self.msg[1]), "w")
self.data = recvMsg[10:]
readFile.write(self.data)
readFile.close()
print("downloaded "+self.msg[1]+" successfully")
except Exception, e:
print "unable to write the file."+ e
self.STATE = 'connect'
#user wants to change window size
if(self.STATE == 'window'):
print('window size has been set to '+ str(self.s.windowSize))
self.STATE = 'connect'
#user wants to disconnect
if(self.STATE == 'disconnect'):
print('User wants to disconnect')
self.s.send("disconnect/.END")
self.s.close()
self.STATE = "welcome"
my_client = fxa_client()
my_client.main()