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