55import functools
66
77#GLOBALS
8- conn_array = []
9- secret_array = dict ()
10- username_array = dict ()
11- contact_array = dict ()
8+ conn_array = [] #stores open sockets
9+ secret_array = dict () #key: the open sockets in conn_array, value: integers for encryption
10+ username_array = dict () #key: the open sockets in conn_array, value: usernames for the connection
11+ contact_array = dict () #key: ip address as a string, value: [port, username]
1212
1313HOST = ''
1414PORT = 0
@@ -61,6 +61,8 @@ def refract(binary):
6161 master = master + chr (int (binary [x * 7 :(x + 1 )* 7 ],2 )+ 0 )
6262 return master
6363
64+ #I'm not actually sure why this is here. But it's used, so for now it stays.
65+ #Raises x to the power of y
6466def power (x , y ):
6567 if (y == 0 ):
6668 return 1
@@ -71,19 +73,21 @@ def power(x, y):
7173 final = final * x
7274 return final
7375
74-
76+ #Ensures that number is atleast length 4 by adding extra zeros to the front
7577def formatNumber (number ):
7678 temp = str (number )
7779 while (len (temp )< 4 ):
7880 temp = '0' + temp
7981 return temp
8082
81-
83+ #Sends message through the open socket conn with the encryption key secret
84+ #It first sends the length of the incoming message, then sends the actual message.
8285def netThrow (conn , secret , message ):
8386 conn .send (formatNumber (len (x_encode (message ,secret ))).encode ())
8487 conn .send (x_encode (message ,secret ).encode ())
8588
86-
89+ #Receive and return the message through open socket conn, decrypting using key secret.
90+ #If the message length begins with - instead of a number, process as a flag and return 1.
8791def netCatch (conn , secret ):
8892 data = conn .recv (4 )
8993 if (data .decode ()[0 ]== '-' ):
@@ -92,11 +96,13 @@ def netCatch(conn, secret):
9296 data = conn .recv (int (data .decode ()))
9397 return refract (xcrypt (data .decode (), bin (secret )[2 :]))
9498
95-
99+ #Process the flag corresponding to number, using open socket conn if necessary
96100def processFlag (number , conn = None ):
97101 global statusConnect
98102 global conn_array
99103 global secret_array
104+ global username_array
105+ global contact_array
100106 t = int (number [1 :])
101107 if (t == 1 ): #disconnect
102108 if (len (conn_array )== 1 ): #in the event of single connection being left or if we're just a client
@@ -111,13 +117,17 @@ def processFlag(number, conn=None):
111117 conn_array .remove (conn )
112118 conn .close ()
113119
114-
120+ if (t == 2 ): #username change
121+ name = netCatch (conn , secret_array [conn ])
122+ username_array [conn ] = name
123+ contact_array [conn .getpeername ()[0 ]]= [conn .getpeername ()[1 ], name ]
124+
115125
116126
117127
118128#--------------------------------------------------------------------------
119129
120-
130+ #Launches client options window for getting destination hostname and port
121131def client_options_window (master ):
122132 global top
123133 top = Toplevel (master )
@@ -133,7 +143,7 @@ def client_options_window(master):
133143 go = Button (top , text = "Connect" , command = client_options_go )
134144 go .grid (row = 2 , column = 1 )
135145
136-
146+ #Processes the options entered by the user in the client options window
137147def client_options_go ():
138148 global location
139149 global port
@@ -144,18 +154,12 @@ def client_options_go():
144154 global PORT
145155 HOST = location .get ()
146156 PORT = int (port .get ())
147- print ("We got here ok." ) #Start client connection, close previous window. top.destroy()
148157 top .destroy ()
149158 MyClient ().start ()
150-
151- else :
152- print ("Your ip or port were invalid." )
153- print (location .get ())
154- print (port .get ())
155- print (int (port .get ()))
156159
157160
158- def client_options_sanitation (loc , por ): # I should also have something to handle the possibilty of the server not being up.
161+ #Checks to make sure the port and the destination ip are both valid, launches error windows if there are any issues
162+ def client_options_sanitation (loc , por ):
159163 if (not por .isnumeric ()):
160164 error_window (root ,"Please input a port number." )
161165 return False
@@ -168,17 +172,11 @@ def client_options_sanitation(loc, por): # I should also have something to handl
168172 return True
169173
170174
171-
175+ #Converts a string with an ip into a list of numbers
172176def ip_breakdown (ip ):
173- spot = 0
174- array = []
175- for x in range (0 ,3 ):
176- t = ip .find ("." , spot )
177- array .append (ip [spot :t ])
178- spot = t + 1
179- array .append (ip [spot :])
180- return array
177+ return ip .split ("." )
181178
179+ #Checks to make sure every section of the ip is a valid number
182180def ip_process (ipArray ):
183181 for ip in ipArray :
184182 if (not ip .isnumeric ()):
@@ -191,6 +189,7 @@ def ip_process(ipArray):
191189
192190#----------------------------------------------------------------------------------------
193191
192+ #Launches server options window for getting port
194193def server_options_window (master ):
195194 global top
196195 top = Toplevel (master )
@@ -202,7 +201,7 @@ def server_options_window(master):
202201 go = Button (top , text = "Launch" , command = server_options_go )
203202 go .grid (row = 1 , column = 1 )
204203
205-
204+ #Processes the options entered by the user in the server options window
206205def server_options_go ():
207206 global port
208207 global top
@@ -214,14 +213,10 @@ def server_options_go():
214213 PORT = int (port .get ())
215214 top .destroy ()
216215 MyServer ().start ()
217-
218- else :
219- print ("Your ip or port were invalid." )
220- print (port .get ())
221- print (int (port .get ()))
222216
217+ #Checks to make sure the port is valid, launches error windows if there are any issues
223218def server_options_sanitation (por ):
224- if (not por .isnumeric ()): #if port is not pure number
219+ if (not por .isnumeric ()):
225220 error_window (root ,"Please input a number." )
226221 return False
227222 if (int (por )< 0 or 65555 < int (por )):
@@ -231,6 +226,7 @@ def server_options_sanitation(por):
231226
232227#----------------------------------------------------------------------------------------
233228
229+ #Launches a new window to display the message texty
234230def error_window (master , texty ):
235231 window = Toplevel (master )
236232 window .title ("ERROR" )
@@ -240,7 +236,7 @@ def error_window(master, texty):
240236
241237
242238#----------------------------------------------------------------------------------------
243- #Friends window
239+ #Contacts window
244240
245241def contacts_window (master ):
246242 global contact_array
@@ -421,7 +417,6 @@ def ClientRunner (conn, secret):
421417
422418
423419def ServerRunner (conn , secret ):
424- print ("Server runner entered" )
425420 global conn_array
426421 global secret_array
427422 global username_array
@@ -535,3 +530,5 @@ def dummer(push):
535530#------------------------------------------------------------#
536531
537532root .mainloop ()
533+
534+ #dump contacts
0 commit comments