A "tiny database" and accompanying protocol
This example shows the usage of shared state amongst all connected clients, namely a database of key/value pairs. Each connected client can send a series of GET/SET commands to query the current value of a key or set the value of a key.
This example has a simple protocol you can use to interact with the server. To run, first run this in one terminal window:
cargo run
and next in another windows run:
nc 127.0.0.1 6142
In the connect
window you can type in commands where when you hit enter
you'll get a response from the server for that command. An example session
is:
$ nc 127.0.0.1 6142
GET foo
foo = bar
GET FOOBAR
error: no key FOOBAR
SET FOOBAR my awesome string
set FOOBAR = my awesome string
, previous: None
SET foo tokio
set foo = tokio
, previous: Some("bar")
GET foo
foo = tokio
Namely you can issue two forms of commands:
GET $key
- this will fetch the value of$key
from the database and return it. The server's database is initially populated with the keyfoo
set to the valuebar
SET $key $value
- this will set the value of$key
to$value
, returning the previous value, if any.