From dced9f08f5b0c532d57e889b2fd917721099f434 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sun, 6 Mar 2016 08:57:11 -0700 Subject: [PATCH] client examples --- README.md | 6 ++--- client/README.md | 66 +++++++++++++++++++++++++++++++++++++++++++++ client/conn_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 client/conn_test.go diff --git a/README.md b/README.md index ac171c6b..c0848e2e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Tile38 is an open source (MIT licensed), in-memory geolocation data store, spati - Spatial index with [search](#searching) methods such as Nearby, Within, and Intersects. - Realtime [geofencing](#geofencing). - Variety of client protocols, including [http](#http) (curl), [websockets](#websockets), [telnet](#telnet), and a [native interface](#native-interface). -- Multiple object types including [lat/lon point](#latlon-point), [bounding box](#bounding-box), [Geohash](#geohash), [GeoJSON](#geojson), [QuadKey](#quadkey), and [XYZ tile](#xyz-tile). +- Object types of [lat/lon](#latlon-point), [bbox](#bounding-box), [Geohash](#geohash), [GeoJSON](#geojson), [QuadKey](#quadkey), and [XYZ tile](#xyz-tile). - Server responses are in json. - Full [command line interface](#cli). - Leader / follower [replication](#replication). @@ -202,7 +202,7 @@ set city tempe object {"type":"Polygon","coordinates":[[[0,0],[10,10],[10,0],[0, An XYZ tile is rectangle bounding area on earth that is represented by an X, Y coordinate and a Z (zoom) level. Check out [maptiler.org](http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/) for an interactive example. -### QuadKey Tile +### QuadKey A QuadKey used the same coordinate system as an XYZ tile except that the string representation is a string characters composed of 0, 1, 2, or 3. For a detailed explanation checkout [The Bing Maps Tile System](https://msdn.microsoft.com/en-us/library/bb259689.aspx). @@ -247,7 +247,7 @@ Should be sent to the server as (without quotes): "$16 get fleet truck1\r\n" ``` -The server responds will always respond in JSON, and will include the top level member `ok`. When `ok` is `false` there will also be an accompanied `err` member describing the problem. In nearly every response there will also be an `elapsed` member that is the duration of time that it took to process the request on the server. For more information on this string please refer to the [time.Duration](https://golang.org/pkg/time/#Duration) Go documentation. +The server will always respond in JSON, and will include the top level member `ok`. When `ok` is `false` there will also be an accompanied `err` member describing the problem. In nearly every response there will also be an `elapsed` member that is the duration of time that it took to process the request on the server. For more information on this string please refer to the [time.Duration](https://golang.org/pkg/time/#Duration) Go documentation. So the response message: ```json diff --git a/client/README.md b/client/README.md index 664cece2..f0ec35a3 100644 --- a/client/README.md +++ b/client/README.md @@ -6,3 +6,69 @@ Tile38 Client Tile38 Client is a [Go](http://golang.org/) client for [Tile38](http://tile38.com/). +## Examples + +#### Connection +```go +package main + +import "github.com/tidwall/tile38/client" + +func main(){ + conn, err := client.Dial("localhost:9851") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + resp, err := conn.Do("set fleet truck1 point 33.5123 -112.2693") + if err != nil { + log.Fatal(err) + } + fmt.Println(string(resp)) +} + +``` + +#### Pool +```go +package main + +import "github.com/tidwall/tile38/client" + +func main(){ + pool, err := client.DialPool("localhost:9851") + if err != nil { + log.Fatal(err) + } + defer pool.Close() + + // We'll set a point in a background routine + go func() { + conn, err := pool.Get() // get a conn from the pool + if err != nil { + log.Fatal(err) + } + defer conn.Close() // return the conn to the pool + _, err = conn.Do("set fleet truck1 point 33.5123 -112.2693") + if err != nil { + log.Fatal(err) + } + }() + time.Sleep(time.Second / 2) // wait a moment + + // Retreive the point we just set. + go func() { + conn, err := pool.Get() // get a conn from the pool + if err != nil { + log.Fatal(err) + } + defer conn.Close() // return the conn to the pool + resp, err := conn.Do("get fleet truck1 point") + if err != nil { + log.Fatal(err) + } + fmt.Println(string(resp)) + }() + time.Sleep(time.Second / 2) // wait a moment +} +``` \ No newline at end of file diff --git a/client/conn_test.go b/client/conn_test.go new file mode 100644 index 00000000..ceefff13 --- /dev/null +++ b/client/conn_test.go @@ -0,0 +1,57 @@ +package client + +import ( + "fmt" + "log" + "time" +) + +func ExampleDial() { + conn, err := Dial("localhost:9851") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + resp, err := conn.Do("set fleet truck1 point 33.5123 -112.2693") + if err != nil { + log.Fatal(err) + } + fmt.Println(string(resp)) +} + +func ExampleDialPool() { + pool, err := DialPool("localhost:9851") + if err != nil { + log.Fatal(err) + } + defer pool.Close() + + // We'll set a point in a background routine + go func() { + conn, err := pool.Get() // get a conn from the pool + if err != nil { + log.Fatal(err) + } + defer conn.Close() // return the conn to the pool + _, err = conn.Do("set fleet truck1 point 33.5123 -112.2693") + if err != nil { + log.Fatal(err) + } + }() + time.Sleep(time.Second / 2) // wait a moment + + // Retreive the point we just set. + go func() { + conn, err := pool.Get() // get a conn from the pool + if err != nil { + log.Fatal(err) + } + defer conn.Close() // return the conn to the pool + resp, err := conn.Do("get fleet truck1 point") + if err != nil { + log.Fatal(err) + } + fmt.Println(string(resp)) + }() + time.Sleep(time.Second / 2) // wait a moment +}