Server executable program for ConnectFour project. Creates a ConnectK game server that listens on port 8080 and communicates with the client via oData.
Let's create a quick simple client program that checks the status of a particular game. First add the following dependencies:
[dependencies]
hyper = "0.10"
serde_json = "1.0"
serde_derive = "1.0.4"
serde = "1.0.4"
Now let's extern/use our dependencies. serde is used for json serialization/deserialization, and hyper will allow us to easily start up a client.
#[macro_use] extern crate serde_json;
extern crate hyper;
use hyper::Client;
use hyper::status::StatusCode;
use serde_json::{Value, from_reader};
Then we write a quick main() function that does a GET and returns the resulting json.
fn main() {
let client = Client::new();
// Assuming we are hosting the server locally:
let url = "http://localhost:8080/api/connect_four.svc/Games(0)";
let response = client.get(url).send().unwrap();
assert_eq!(response.status, StatusCode::Ok); // sanity check
// Parse JSON
let value: Value = from_reader(response).expect("Unable to parse response!");
println!("Body: {:?}", value);
println!("Got Board: id = {}, height = {}, width = {}",
value["id"], value["height"], value["width"]);
}
The example can be found here.
Additionally, the tests in main.rs may be useful to view. For instance, to play a move:
// Lets now play a move
let url = "http://localhost:8080/api/connect_four.svc/play_move";
let value = json!({
"id": id,
"move": 0
});
println!("{}", &value.to_string());
client.post(url).body(&value.to_string()).send().unwrap();
// Check that the move was actually played
println!("Checking Move Played");
let url = &format!("http://localhost:8080/api/connect_four.svc/Games({})", id);
println!("{}", url);
let res = client.get(url).send().unwrap();
assert_eq!(res.status, StatusCode::Ok);
let data: Value = from_reader(res).expect("Unable to parse response!");
let board = data["board"].as_str().expect("Unable to parse board!");
assert_eq!(board.chars().nth(0).unwrap(), '1');
- serde_json docs
- hyper client docs
GET <hostname>:8080/api/connect_four.svc/$metadata
{
"$schema": "http://docs.oasis-open.org/odata/odata-json-csdl/v4.0/edm.json#",
"actions": "",
"definitions": {
"Game": {
"keys": [
"id"
],
"properties": {
"board": {
"format": "",
"type": [
"string"
]
...
"entityContainer": {
"entitySets": {
"Games": {
"entityType": {
"$ref": "#/definitions/Game"
}
}
},
"name": "ServiceName"
},
"functions": "",
"odata-version": "4.0",
"references": "",
"schemas": "",
"terms": ""
}
GET <hostname>:8080/api/connect_four.svc/Games
[
{
"board": "00000000000000000000000000000000000",
"curr_player": 1,
"height": 7,
"id": 0,
"k": 4,
"status": "InProcess",
"width": 5
},
{
"board": "00000000000000000000000000000000000",
"curr_player": 1,
"height": 7,
"id": 1,
"k": 4,
"status": "InProcess",
"width": 5
},
{
"board": "00000000000000000000000000000000000",
"curr_player": 1,
"height": 7,
"id": 2,
"k": 4,
"status": "InProcess",
"width": 5
},
]
GET <hostname>:8080/api/connect_four.svc/Games(0)
{
"board": "00000000000000000000000000000000000",
"curr_player": 1,
"height": 7,
"id": 0,
"k": 4,
"status": "InProcess",
"width": 5
}
POST <hostname>:8080/api/connect_four.svc/Games
{
"height": 7,
"k": 4,
"width": 5
}
{
"board": "00000000000000000000000000000000000",
"curr_player": 1,
"height": 7,
"id": 1,
"k": 4,
"status": "InProcess",
"width": 5
}
Calls game.insert(move)
for designated game.
POST <hostname>:8080/api/connect_four.svc/play_move
{
"id": 1,
"move": 0
}
None