PageRenderTime 12ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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