99import Foundation
1010
1111protocol ServerInterface : BroadcastDevice {
12- // Incoming TCP connections socket
13- var incoming_tcp_connections_socket : Int32 { get }
14- var tcpKQueue : Int32 { get }
15- var connectionSockets : [ Int32 : User ] { get }
16- var maxListeningConnections : Int32 { get }
17- var serverTCPCommunicationDelegate : ServerTCPCommunicationDelegate ? { get }
18-
1912 func enableTCPCommunication( )
2013 func handleNewConnection( )
21- func receivedClientTCPText( _ text: String , socket: Int32 , user : User )
22- func sendClientText( fullText: String , content: String , user : User , socket: Int32 )
14+ func receivedClientTCPText( _ text: String , socket: Int32 , senderIP : String )
15+ func sendClientText( fullText: String , content: String , senderAlias : String , socket: Int32 )
2316 func sendServerText( _ text: String )
24- func sendInformationText( _ text : String )
17+ func sendInformationText( fullText : String , content : String )
2518}
2619
2720final class Server : ServerInterface {
@@ -38,22 +31,19 @@ final class Server: ServerInterface {
3831 weak var udpCommunicationDelegate : UDPCommunicationDelegate ?
3932 weak var roleGrantDelegate : GrantRoleDelegate ? // to be removed
4033
41-
4234 // Incoming TCP connections socket
43- var incoming_tcp_connections_socket : Int32 = - 1
35+ private var incoming_tcp_connections_socket : Int32 = - 1
4436 // Kqueue to organise tcp events
45- let tcpKQueue : Int32 = kqueue ( )
37+ private let tcpKQueue : Int32 = kqueue ( )
4638
4739 // Map of the fd and their IPs
48- var connectionSockets : [ Int32 : User ] = [ : ]
40+ var connectionSockets : [ Int32 : String ] = [ : ]
4941 // Array of all the kevents
5042 private var kEvents : [ kevent ] = [ ]
5143
52- let maxListeningConnections : Int32 = 5
53-
5444 weak var serverTCPCommunicationDelegate : ServerTCPCommunicationDelegate ?
5545
56- private var nickname : String ?
46+ let maxListeningConnections : Int32 = 5
5747 var ip : String
5848
5949 init (
@@ -177,8 +167,8 @@ final class Server: ServerInterface {
177167 close ( Int32 ( fd) )
178168 } else if incoming_tcp_connections_socket == fd {
179169 handleNewConnection ( )
180- } else if let fd_and_user = connectionSockets. first ( where : ( { $0 . key == fd } ) ) {
181- incomingClientTCPText ( socket: fd_and_user . key , user : fd_and_user . value )
170+ } else if let ip = connectionSockets [ Int32 ( fd ) ] {
171+ incomingClientTCPText ( socket: Int32 ( fd ) , senderIP : ip )
182172 }
183173 }
184174 default :
@@ -205,7 +195,7 @@ final class Server: ServerInterface {
205195 }
206196
207197 // Save client ip with associated fd
208- connectionSockets [ connection_socket_and_client_ip. fd] = User ( IP : connection_socket_and_client_ip. IP)
198+ connectionSockets [ connection_socket_and_client_ip. fd] = connection_socket_and_client_ip. IP
209199
210200 // Create the kevent structure that sets up our kqueue to listen
211201 // for notifications
@@ -225,7 +215,7 @@ final class Server: ServerInterface {
225215 }
226216
227217 // A message from a client is about to come
228- private func incomingClientTCPText( socket: Int32 , user : User ) {
218+ private func incomingClientTCPText( socket: Int32 , senderIP : String ) {
229219 let receivedStringBuffer = UnsafeMutableBufferPointer< CChar> . allocate( capacity: 65536 )
230220 let rawPointer = UnsafeMutableRawPointer ( receivedStringBuffer. baseAddress)
231221
@@ -239,27 +229,26 @@ final class Server: ServerInterface {
239229 }
240230 let string = String ( cString: UnsafePointer ( baseAddress) )
241231 // Handle the message in order to understand whether is a standard message or a nick change request
242- receivedClientTCPText ( string, socket: socket, user : user )
232+ receivedClientTCPText ( string, socket: socket, senderIP : senderIP )
243233 }
244234
245235 // MARK: - Send message
246236 // Send the message of the client whose socket is clientSocket to all of the other clients
247- func receivedClientTCPText( _ text: String , socket: Int32 , user : User ) {
248- guard let messageType = serverTCPCommunicationDelegate? . serverDidReceiveClientTCPText ( text, senderIP: user . IP ) else {
237+ func receivedClientTCPText( _ text: String , socket: Int32 , senderIP : String ) {
238+ guard let messageType = serverTCPCommunicationDelegate? . serverDidReceiveClientTCPText ( text, senderIP: senderIP ) else {
249239 print ( " Nil communicationDelegate " )
250240 return
251241 }
252242
253243 switch messageType {
254- case . nicknameChangeRequest( let nickname) :
255- connectionSockets [ socket] ? . changeNickname ( nickname)
256- sendInformationText ( " \( user. IP) is now \( nickname) " )
244+ case . nicknameChangeRequest( let fullText, let content) :
245+ sendInformationText ( fullText: fullText, content: content)
257246 case . text( let fullText, let content, let senderAlias) :
258- sendClientText ( fullText: fullText, content: content, user : user , socket: socket)
247+ sendClientText ( fullText: fullText, content: content, senderAlias : senderAlias , socket: socket)
259248 }
260249 }
261250
262- func sendClientText( fullText: String , content: String , user : User , socket: Int32 ) {
251+ func sendClientText( fullText: String , content: String , senderAlias : String , socket: Int32 ) {
263252 for event in kEvents where event. ident != UInt ( socket) {
264253 let fd = event. ident
265254 fullText. withCString { cString in
@@ -272,7 +261,7 @@ final class Server: ServerInterface {
272261 }
273262 }
274263 }
275- serverTCPCommunicationDelegate? . serverDidSendClientText ( content, clientIP : user . IP )
264+ serverTCPCommunicationDelegate? . serverDidSendClientText ( content, senderAlias : senderAlias )
276265 }
277266
278267 // Send server own text
@@ -284,7 +273,7 @@ final class Server: ServerInterface {
284273 }
285274
286275 switch messageType {
287- case . text( let fullText, let content, let senderAlias ) :
276+ case . text( let fullText, let content, _ ) :
288277 for event in kEvents {
289278 let fd = event. ident
290279
@@ -299,25 +288,24 @@ final class Server: ServerInterface {
299288 }
300289 }
301290 }
302- case . nicknameChangeRequest( nickname: let nickname) :
303- self . nickname = nickname
304- sendInformationText ( " \( ip) is now \( nickname) " )
291+ case . nicknameChangeRequest( let fullText, let content) :
292+ sendInformationText ( fullText: fullText, content: content)
305293 }
306294 }
307295
308296 // Send information text such as nickname change
309- func sendInformationText( _ text : String ) {
297+ func sendInformationText( fullText : String , content : String ) {
310298 for event in kEvents {
311299 let fd = event. ident
312300
313- text . withCString { cString in
301+ fullText . withCString { cString in
314302 let messageLength = Int ( strlen ( cString) )
315303 let bytes = send ( Int32 ( fd) , cString, messageLength, 0 )
316304 if bytes < 0 {
317305 print ( " Error sending TCP Message " )
318306 } else {
319- print ( " Sent by server: \( text ) " )
320- serverTCPCommunicationDelegate? . serverDidSendInformationText ( text )
307+ print ( " Sent by server: \( content ) " )
308+ serverTCPCommunicationDelegate? . serverDidSendInformationText ( content )
321309 }
322310 }
323311 }
0 commit comments