Browse Source

timeout code to write to conns to find lost conns

Paul Klumpp 12 years ago
parent
commit
1e3a1c109c
1 changed files with 23 additions and 6 deletions
  1. 23 6
      socket_central.rb

+ 23 - 6
socket_central.rb

@@ -4,6 +4,9 @@
 require 'rubygems'
 require 'rubygems'
 require 'socket'
 require 'socket'
 require 'digest/md5'
 require 'digest/md5'
+require 'timeout'
+
+
 
 
 # .. allowed commands .. ROLES = CAPABILITIES
 # .. allowed commands .. ROLES = CAPABILITIES
 # normal users have ROLE broadcast. Roles are defined on a per-user basis .. in a config.
 # normal users have ROLE broadcast. Roles are defined on a per-user basis .. in a config.
@@ -312,17 +315,25 @@ def thinking(c, user, t_display)
 	end
 	end
 
 
 	
 	
-	write_user(user, "HELLO Hi user '#{user}'! How are you? I'm #{$version}")
+	write_user(user, "HELLO Hi user '#{user}'! How are you? I'm '#{$version}'")
 	write_user(user, "ROLES #{my_roles(user).join(", ")}")
 	write_user(user, "ROLES #{my_roles(user).join(", ")}")
 	write_user(user, "COMMANDS #{my_cmds(user).join(", ")}")
 	write_user(user, "COMMANDS #{my_cmds(user).join(", ")}")
-	write_role("everyone", "JOINED User '#{user}' just joined the party.", user)
+	write_role($default_role, "JOINED User '#{user}' just joined the party.", user)
 	
 	
 	# reading the client input ... to write it somewhere, possibly to the client himself.
 	# reading the client input ... to write it somewhere, possibly to the client himself.
 	begin
 	begin
-		input = ""
+		input = nil
 		while input != "PART" do
 		while input != "PART" do
-			input = c.gets.chomp
-			execution = inputting(user, input)
+			
+			begin
+				status = Timeout::timeout(90) do
+					input = c.gets.chomp # waits for user input
+					execution = inputting(user, input)
+				end
+			rescue Exception => e
+				write_user(user, "PING Still alive?") # if writing fails, then it may be a broken pipe .. so it will lose the connection and delete the user from the online user list.
+			end
+			           
 		end
 		end
 	rescue Exception => e  
 	rescue Exception => e  
 		puts e.message  
 		puts e.message  
@@ -334,6 +345,10 @@ def thinking(c, user, t_display)
 	
 	
 end
 end
 
 
+def pingtimer(c, user)
+	
+end
+
 def spawn_server
 def spawn_server
 	$display_queue = Hash.new
 	$display_queue = Hash.new
 	$broadcasts = Hash.new
 	$broadcasts = Hash.new
@@ -343,10 +358,11 @@ def spawn_server
 	
 	
 	loop do
 	loop do
 		Thread.start(server.accept) do |c|
 		Thread.start(server.accept) do |c|
-			user = c.gets.chomp
+			user = c.gets.chomp # waits for user name input
 			
 			
 			# only one connection from a user allowed - we don't handle multiple display_queues for one user!!!!!11111
 			# only one connection from a user allowed - we don't handle multiple display_queues for one user!!!!!11111
 			if online_users.user(user)
 			if online_users.user(user)
+				user = c.puts "Only one connection allowed per user."
 				c.close
 				c.close
 			else
 			else
 				$display_queue[user] = Array.new
 				$display_queue[user] = Array.new
@@ -354,6 +370,7 @@ def spawn_server
 
 
 				# this thread reads what others want you to read .. 
 				# this thread reads what others want you to read .. 
 				t_user_display = Thread.new{ displaying(c, user) }
 				t_user_display = Thread.new{ displaying(c, user) }
+				
 				begin
 				begin
 					execution = thinking(c, user, t_user_display)
 					execution = thinking(c, user, t_user_display)