Skip to content
Snippets Groups Projects
Commit 9797bc54 authored by Pawan Rawal's avatar Pawan Rawal
Browse files

Making changes so that client gets back protocol buffer data

Client now gets back protocol buffer data and is able to parse it into protocol buffer Subgraph
parent d0eb73e2
No related branches found
No related tags found
No related merge requests found
......@@ -17,50 +17,69 @@
package main
import (
"bytes"
"flag"
"fmt"
"net"
"github.com/dgraph-io/dgraph/query/protocolbuffer"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/protobuf/proto"
)
var glog = x.Log("client")
var port = flag.String("port", "3000", "Port to communicate with server")
var port = flag.String("port", "8090", "Port to communicate with server")
func main() {
var q0 = `
{
user(_xid_:alice) {
follows {
_xid_
status
}
_xid_
status
var q0 = `{
me(_xid_: m.06pj8) {
type.object.name.en
film.director.film {
type.object.name.en
film.film.starring {
film.performance.character {
type.object.name.en
}
film.performance.actor {
type.object.name.en
film.director.film {
type.object.name.en
}
}
}
film.film.initial_release_date
film.film.country
film.film.genre {
type.object.name.en
}
}
}
}
`
}`
conn, err := net.Dial("tcp", "127.0.0.1:"+*port)
if err != nil {
glog.Fatalf("While running server: %v", err)
x.Err(glog, err).Fatal("DialTCPConnection")
}
fmt.Println("sending data", []byte(q0))
_, err = conn.Write([]byte(q0))
if err != nil {
x.Err(glog, err).Fatal("Error in writing to server")
}
reply := []byte{}
// TODO(pawan): Discuss and implement a better way of doing this.
reply := make([]byte, 4096)
_, err = conn.Read(reply)
if err != nil {
x.Err(glog, err).Fatal("Error in reading response from server")
}
fmt.Println(string(reply))
// Trimming null bytes
reply = bytes.Trim(reply, "\000")
conn.Close()
usg := &protocolbuffer.SubGraph{}
if err := proto.Unmarshal(reply, usg); err != nil {
x.Err(glog, err).Fatal("Error in umarshalling protocol buffer")
}
conn.Close()
}
......@@ -255,6 +255,9 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) {
func preTraverse(g *SubGraph) (sg *protocolbuffer.SubGraph, rerr error) {
sg = &protocolbuffer.SubGraph{}
if len(g.query) == 0 {
return sg, nil
}
sg.Attr = g.Attr
ro := flatbuffers.GetUOffsetT(g.result)
......
......@@ -24,6 +24,7 @@ import (
"net"
"net/http"
"runtime"
"strconv"
"strings"
"time"
......@@ -197,7 +198,10 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
}
func pbQueryHandler(q []byte) (pb []byte, rerr error) {
fmt.Println("in pbQueryHandler")
if len(q) == 0 {
return
}
glog.WithField("q", string(q)).Debug("Query received.")
gq, _, err := gql.Parse(string(q))
if err != nil {
......@@ -249,11 +253,11 @@ func runServerForClient(address string) error {
WithField("remote", cxn.RemoteAddr()).
Debug("Client Worker accepted connection")
q := make([]byte, 1024000)
// Move to separate function
// TODO(pawan) - Find a better way to do this, byte slice shouldn't be of fixed size
q := make([]byte, 4096)
// TODO(pawan) - Move to separate function
go func(c net.Conn) {
_, _ = c.Read(q)
fmt.Println("query received: ", string(q))
c.Read(q)
r, _ := pbQueryHandler(q)
c.Write(r)
}(cxn)
......@@ -306,7 +310,9 @@ func main() {
worker.Connect(addrs)
runServerForClient(":3000")
// TODO(pawan): Have a better way to do this, pick port for client from a flag
clientPort, _ := strconv.Atoi(*port)
runServerForClient(":" + strconv.Itoa(clientPort+10))
http.HandleFunc("/query", queryHandler)
glog.WithField("port", *port).Info("Listening for requests...")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment