-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkManager.py
More file actions
129 lines (120 loc) · 5.14 KB
/
NetworkManager.py
File metadata and controls
129 lines (120 loc) · 5.14 KB
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
import os
import sys
os.chdir('tftpy')
sys.path.insert(0, os.getcwd())
from TftpClient import TftpClient
from TftpShared import TftpException
os.chdir('..')
sys.path.insert(0, os.getcwd())
import urllib
import urllib2
import cPickle as pickle
from datastructures import User
class NetworkManager(object):
def __init__(self):
try:
urllib.urlretrieve("http://students.olin.edu/2017/ihill/ServerAddress.txt", "ServerAddress.txt")
with open('ServerAddress.txt', 'r') as ip:
lines = ip.readlines()
self.ip = lines[0].rstrip()
self.port = int(lines[1])
os.remove('ServerAddress.txt')
self.client = TftpClient(self.ip,self.port)
self.started = True
except:
self.started = False
def check_internet(self):
if self.started == False:
try:
urllib.urlretrieve("http://students.olin.edu/2017/ihill/ServerAddress.txt", "ServerAddress.txt")
with open('ServerAddress.txt', 'r') as ip:
lines = ip.readlines()
self.ip = lines[0]
self.port = lines[1]
os.remove('ServerAddress.txt')
self.client = TftpClient(self.ip,self.port)
self.started = True
except:
self.started = False
return False
else:
try:
urllib2.urlopen('http://www.google.com',None,timeout=5)
print("Connected to the Internet")
return True
except urllib2.URLError:
print("Disconnected from the Internet")
return False
def create_user(self,username):
if self.check_internet():
try:
self.client.download( username + '.usr', 'temp.usr')
print("User already exists")
return -1
except TftpException:
print("User does not exist on server \nCreating file")
os.rename('user.usr',username + '.usr')
self.client.upload(username + '.usr', username + '.usr')
os.rename(username + '.usr','user.usr')
print("Done")
def update(self,username,password):
if self.check_internet():
try:
self.client.download(username + '.usr','temp.usr')
print("Downloaded user information")
except:
print("User does not exist on server")
return -1
if os.path.isfile('temp.usr') and os.path.isfile('user.usr'):
with open('temp.usr','rb') as temp:
online_user = pickle.load(temp)
with open('user.usr','rb') as local:
local_user = pickle.load(local)
else:
print("User does not exist on client")
return -2
print("Checking if user file is up-to-date")
if local_user.username != online_user.username or online_user.last_updated > local_user.last_updated:
""" server version of user is newer than local version """
os.remove('user.usr')
os.rename('temp.usr','user.usr')
elif local_user.username == online_user.username and local_user.last_updated >= online_user.last_updated:
""" local version of user is newer than server version """
os.remove('temp.usr')
with open('user.usr','rb') as userfile:
user = pickle.load(userfile)
userfile.close()
if password != user.password:
print("Incorrect password")
return -3
else:
print("Correct password")
try:
self.client.download('courses.csv', 'courses.csv')
print("Downloaded course information")
self.client.download('profs.csv','profs.csv')
print("Downloaded professor information")
self.client.download('stats.sts','stats.sts')
print("Downloaded user statistics")
except:
print("Network error when downloading new content")
return -4
else:
try:
os.rename('user.usr',username + '1.usr')
self.client.upload(username+'1.usr', username + '1.usr')
os.rename(username + '1.usr','user.usr')
print("User content uploaded")
except:
if os.path.isfile(username + '1.usr'):
os.rename(username + '1.usr','user.usr')
print("User content upload failed")
return -5
else:
print("Update complete.")
return True
else:
return 0
if __name__ == '__main__':
network = NetworkManager()
network.update('ihill','crashcourse')