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

/tutorial/net/select.e

http://github.com/tybor/Liberty
Specman e | 91 lines | 72 code | 10 blank | 9 comment | 3 complexity | 95343e6b6be7e637092f33b6c1db9f09 MD5 | raw file
 1-- This example will show how you may wait some data in INPUT_STREAM
 2class SELECT
 3
 4create {ANY}
 5   make
 6
 7feature {ANY}
 8   events: EVENTS_SET
 9
10   make
11         -- Connect to the server started by the SOCKETS class in the same cluster. You must compile and start
12         -- it first.
13      do
14         connect_to(once "localhost", 2001)
15         create events.make
16         io.put_line(once "Here we go...")
17         read(1000)
18         read(1000)
19         ios.put_string(once "|...<* HELLO 1 *>...|%N|...<* HELLO 2 *>...|%N")
20         ios.put_string(once "|...<* HELLO 3 *>...|%N|...<* HELLO 4 *>...|%N")
21         ios.put_string(once "|...<* HELLO 5 *>...|%N|...<* HELLO 6 *>...|%N")
22         ios.put_string(once "|...<* HELLO 7 *>...|%N|...<* HELLO 8 *>...|%N")
23         ios.flush
24         read(1000)
25         read(1000)
26         disconnect
27      end
28
29   read (ms: INTEGER)
30      local
31         time_events: TIME_EVENTS
32      do
33         events.expect(time_events.timeout(ms))
34         events.expect(ios.event_can_read)
35         events.wait
36         if events.event_occurred(ios.event_can_read) then
37            check
38               ios.can_read_line
39            end
40            ios.read_line
41            from
42            until
43               ios.end_of_input
44            loop
45               io.put_string(once "INPUT> ")
46               io.put_line(ios.last_string.twin)
47               ios.read_line
48            end
49         else
50            io.put_line(once "INPUT> ...NONE...")
51         end
52
53         events.reset
54      end
55      -----------------------------------------------------------
56      -- standard features, nothing new here :)
57      -----------------------------------------------------------
58
59   ios: SOCKET_INPUT_OUTPUT_STREAM
60
61   connect_to (host: STRING; port: INTEGER)
62         -- connect TCP stream to destination host and port
63      local
64         tcp: TCP_ACCESS; tcp_host: HOST
65      do
66         create tcp_host.make(host)
67         create tcp.make(tcp_host, port, True)
68         ios := tcp.stream
69         if ios = Void or else not is_connected then
70            std_output.put_line(tcp.error)
71            die_with_code(1)
72         end
73      end
74
75   is_connected: BOOLEAN
76         -- is the stream connected
77      do
78         Result := ios.is_connected
79      end
80
81   disconnect
82         -- disconnect the stream
83      require
84         is_connected
85      do
86         ios.disconnect
87      ensure
88         not is_connected
89      end
90
91end -- class SELECT