/tutorial/storable/example3/repository_client.e

http://github.com/tybor/Liberty · Specman e · 100 lines · 70 code · 13 blank · 17 comment · 2 complexity · 949b07cb708aca20612306c031214010 MD5 · raw file

  1. class REPOSITORY_CLIENT
  2. --
  3. -- A simple client that displays the contents of the repository, asks for a new variable and puts it back in
  4. -- the repository.
  5. --
  6. -- See REPOSITORY_SERVER: the server must be started
  7. --
  8. create {ANY}
  9. make
  10. feature {}
  11. repository: REPOSITORY[STRING]
  12. make
  13. do
  14. open_repository
  15. display_repository
  16. ask_new_variable
  17. display_repository
  18. repository.commit
  19. close_repository
  20. end
  21. feature {} -- Implementation
  22. ios: SOCKET_INPUT_OUTPUT_STREAM
  23. open_repository
  24. -- Open the repository
  25. local
  26. access: TCP_ACCESS
  27. do
  28. -- Create the client socket
  29. create access.make(create {LOCALHOST}.make, 2001, True)
  30. sedb_breakpoint
  31. -- Ensure that readings from the sockets at that access point are synchronous (because we don't use
  32. -- the sequencer mechanics, we need some low-level system to ensure that we have data to read)
  33. ios := access.stream
  34. if ios = Void then
  35. if access.error = Void then
  36. std_error.put_line("An error occurred. What?")
  37. else
  38. std_error.put_line(access.error)
  39. end
  40. die_with_code(1)
  41. end
  42. check
  43. ios.is_connected
  44. end
  45. -- Read the repository from that socket (the server must be started)
  46. create {XML_STREAM_REPOSITORY[STRING]} repository.connect_to(ios, ios)
  47. repository.update
  48. end
  49. close_repository
  50. do
  51. ios.disconnect
  52. end
  53. display_repository
  54. -- Display the contents of the repository
  55. local
  56. i: INTEGER
  57. do
  58. -- Display the repository
  59. from
  60. i := repository.lower
  61. until
  62. i > repository.upper
  63. loop
  64. std_output.put_string(repository.key(i))
  65. std_output.put_string(once ": ")
  66. std_output.put_string(repository.item(i))
  67. std_output.put_new_line
  68. i := i + 1
  69. end
  70. end
  71. ask_new_variable
  72. -- Ask for a new variable and put it in the repository
  73. local
  74. name, value: STRING
  75. do
  76. -- Ask the variable name
  77. std_output.put_string(once "new variable name: ")
  78. std_input.read_line
  79. name := std_input.last_string.twin
  80. -- Ask the variable value
  81. std_output.put_string(once "new variable value: ")
  82. std_input.read_line
  83. value := std_input.last_string.twin
  84. -- Modify the repository
  85. repository.put(value, name)
  86. end
  87. end -- class REPOSITORY_CLIENT