PageRenderTime 21ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/net/multiplex_connection.e

http://github.com/tybor/Liberty
Specman e | 85 lines | 61 code | 8 blank | 16 comment | 1 complexity | 78e64489b201dd41132be1202385725f MD5 | raw file
 1class MULTIPLEX_CONNECTION
 2--
 3-- Handle one connection to the MULTIPLEX_SERVER.
 4--
 5-- Each connection is very simple: all the data is sent back (it's an "echo" thing), except when one of the
 6-- three commands below is issued. A command is a single word written alone on its line. The three commands
 7-- are:
 8--
 9-- bye       simply closes the current connection.
10--
11-- shutdown  will stop the server as soon as all the open connections are closed. The server does not
12--           accept any new connection.
13--
14-- halt      closes all the open connections and shut the server down.
15--
16inherit
17	CONNECTION
18		redefine
19			set_io
20		end
21
22create {MULTIPLEX_SERVER}
23	make
24
25feature {LOOP_ITEM}
26	continue is
27		local
28			s: STRING
29		do
30			ios.read_line
31			s := ios.last_string
32			if not s.is_empty then
33				inspect s
34				when "shutdown" then
35					ios.disconnect
36					server.shutdown
37				when "halt" then
38					server.halt
39				when "bye" then
40					ios.disconnect
41				else
42					std_output.put_integer(id)
43					std_output.put_string(once ": ")
44					std_output.put_line(s)
45				end
46			end
47		end
48
49feature {SERVER}
50	set_io (a_io: like ios) is
51			-- Called by the server when the connection is initiated. The stream is the socket into which the
52			-- connection is plugged.
53		do
54			Precursor(a_io)
55			a_io.unset_timeout
56			a_io.when_disconnect(agent handle_disconnect)
57			a_io.put_string(once "Hello! There are three commands available:%
58                              % bye, shutdown and halt.%N")
59		end
60
61feature {}
62	make (a_server: like server) is
63		do
64			counter.increment
65			id := counter.value
66			server := a_server
67		end
68
69	handle_disconnect (a_io: like ios) is
70		require
71			a_io = ios
72			done
73		do
74			server.connection_done(Current)
75		end
76
77	id: INTEGER
78	server: MULTIPLEX_SERVER
79
80	counter: COUNTER is
81		once
82			create Result
83		end
84
85end