/tutorial/net/multiplex_server.e

http://github.com/tybor/Liberty · Specman e · 65 lines · 47 code · 9 blank · 9 comment · 2 complexity · a61c69c71fd1752f767e4e685d13eaec MD5 · raw file

  1. class MULTIPLEX_SERVER
  2. --
  3. -- If you try to make a real server using our technology, this class may prove useful. Be sure to have seen
  4. -- the class SOCKETS first.
  5. --
  6. insert
  7. SERVER[MULTIPLEX_CONNECTION]
  8. export {} start, start_in_stack
  9. end
  10. create {ANY}
  11. make
  12. feature {}
  13. make
  14. local
  15. host: HOST; tcp: TCP_ACCESS
  16. do
  17. -- Start a server on the local machine, listening on port 2001
  18. create host.make("localhost")
  19. create tcp.make(host, 2001, True)
  20. start(tcp)
  21. end
  22. new_connection: MULTIPLEX_CONNECTION
  23. do
  24. create Result.make(Current)
  25. end
  26. handle_error (error_message: STRING)
  27. do
  28. if error_message /= Void then
  29. std_error.put_string(error_message)
  30. std_error.put_new_line
  31. else
  32. std_error.put_string(once "Could not start the server%N")
  33. end
  34. die_with_code(1)
  35. end
  36. feature {MULTIPLEX_CONNECTION}
  37. shutdown
  38. -- A connection asked the server to shut down
  39. do
  40. server.shutdown
  41. end
  42. halt
  43. -- A connection asked the server to halt
  44. do
  45. server.halt
  46. end
  47. connection_done (a_connection: MULTIPLEX_CONNECTION)
  48. -- A connection is just finished.
  49. do
  50. connections := connections - 1
  51. if connections = 0 and then server.done then
  52. -- and then stack.current_loop /= Void
  53. handle_shutdown(server)
  54. end
  55. end
  56. end -- class MULTIPLEX_SERVER