commit f1a88348a765d7e78285a5c6dbd9041beb78e349
parent fdfec9cc2566b7ccdae28c6dc0ebe446c322b3c6
Author: MTRNord <mtrnord1@gmail.com>
Date: Sat, 16 Sep 2017 21:44:03 +0200
Add HTTP Protobuf Server and Client
Signed-off-by: MTRNord <mtrnord1@gmail.com>
Diffstat:
4 files changed, 54 insertions(+), 30 deletions(-)
diff --git a/src/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go b/src/github.com/Nordgedanken/MatrixProtoBuf/protobufS/protoServer.go
@@ -5,43 +5,29 @@ import (
pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos"
"github.com/golang/protobuf/proto"
"log"
- "net"
+ "net/http"
"os"
)
func StartProtoServer() {
log.Println("Started ProtoBuf Server")
- c := make(chan *pb.Version)
- //Listen to the TCP port
- listener, err := net.Listen("tcp", "127.0.0.1:2110")
- checkError(err)
- for {
- if conn, err := listener.Accept(); err == nil {
- //If err is nil then that means that data is available for us so we take up this data and pass it to a new goroutine
- go handleProtoClient(conn, c)
- } else {
- continue
+
+ http.HandleFunc("/versions", func(w http.ResponseWriter, r *http.Request) {
+ resp := &pb.VersionsResponse{
+ Versions: []*pb.Version{
+ {"r0.0.1"},
+ {"r0.1.0"},
+ {"r0.2.0"},
+ },
}
- }
-}
+ out, err := proto.Marshal(resp)
+ checkError(err)
-func handleProtoClient(conn net.Conn, c chan *pb.Version) {
- fmt.Println("Connection established")
- //Close the connection when the function exits
- defer conn.Close()
- //Create a data buffer of type byte slice with capacity of 4096
- data := make([]byte, 4096)
- //Read the data waiting on the connection and put it in the data buffer
- n, err := conn.Read(data)
- checkError(err)
- fmt.Println("Decoding Protobuf message")
- //Create an struct pointer of type ProtobufTest.TestMessage struct
- protodata := new(pb.Version)
- //Convert all the data retrieved into the ProtobufTest.TestMessage struct type
- err = proto.Unmarshal(data[0:n], protodata)
- checkError(err)
- //Push the protobuf message into a channel
- c <- protodata
+ w.WriteHeader(http.StatusOK)
+ _, err = w.Write(out)
+ checkError(err)
+ })
+ http.ListenAndServe(":3000", nil)
}
func checkError(err error) {
diff --git a/src/github.com/Nordgedanken/mpbProtoClient/client.go b/src/github.com/Nordgedanken/mpbProtoClient/client.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "fmt"
+ pb "github.com/Nordgedanken/MatrixProtoBuf/matrixProtos"
+ "github.com/golang/protobuf/proto"
+ "io/ioutil"
+ "net/http"
+)
+
+func main() {
+ getVersions()
+}
+
+func getVersions() {
+ var versions pb.VersionsResponse
+
+ resp, err := http.Get("http://localhost:3000/versions")
+ if err != nil {
+ panic(err)
+ }
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+
+ if err := proto.Unmarshal(body, &versions); err != nil {
+ fmt.Println(err)
+ }
+
+ for _, v := range versions.Versions {
+ fmt.Println(v.Version)
+ }
+}
diff --git a/src/github.com/Nordgedanken/mpbClient/client.go b/src/github.com/Nordgedanken/mpbRPCClient/client.go
diff --git a/vendor/manifest b/vendor/manifest
@@ -9,6 +9,12 @@
"path": "/compute/metadata"
},
{
+ "importpath": "github.com/StabbyCutyou/buffstreams",
+ "repository": "https://github.com/StabbyCutyou/buffstreams",
+ "revision": "20679e9ca31ad9ff33e5a1c3c4b90ec65e9a0b03",
+ "branch": "master"
+ },
+ {
"importpath": "github.com/golang/glog",
"repository": "https://github.com/golang/glog",
"revision": "23def4e6c14b4da8ac2ed8007337bc5eb5007998",