add a working protobuf channel example
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
c34dc5f401
commit
b91b431ec6
|
@ -0,0 +1,5 @@
|
||||||
|
server:
|
||||||
|
go run server.go
|
||||||
|
|
||||||
|
client:
|
||||||
|
go run client.go
|
|
@ -0,0 +1,38 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "net"
|
||||||
|
import "os"
|
||||||
|
import "log"
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// import "io"
|
||||||
|
// import "encoding/csv"
|
||||||
|
// import "flag"
|
||||||
|
|
||||||
|
import "github.com/golang/protobuf/proto"
|
||||||
|
import pb "git.wit.com/jcarr/witProtobuf"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
msg := new(pb.Event)
|
||||||
|
msg.Name = "hello jeff"
|
||||||
|
data, err := proto.Marshal(msg)
|
||||||
|
if (err != nil) {
|
||||||
|
log.Printf("something fucked up happened")
|
||||||
|
}
|
||||||
|
sendDataToDest(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendDataToDest(data []byte) {
|
||||||
|
conn, err := net.Dial("tcp", "localhost:8080")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n, err := conn.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("Sent " + strconv.Itoa(n) + " bytes")
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "net"
|
||||||
|
import "os"
|
||||||
|
import "io"
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
import "github.com/golang/protobuf/proto"
|
||||||
|
import pb "git.wit.com/jcarr/witProtobuf"
|
||||||
|
|
||||||
|
func processEvents(mychannel chan *pb.Event) {
|
||||||
|
for {
|
||||||
|
message := <-mychannel
|
||||||
|
ReadReceivedData(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadReceivedData(data *pb.Event) {
|
||||||
|
msgItems := data.GetResults()
|
||||||
|
fmt.Println("Receiving data...")
|
||||||
|
for _, item := range msgItems {
|
||||||
|
fmt.Println(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Staring Server..")
|
||||||
|
c := make(chan *pb.Event)
|
||||||
|
go processEvents(c)
|
||||||
|
|
||||||
|
listener, err := net.Listen("tcp", ":8080")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if conn, err := listener.Accept(); err == nil {
|
||||||
|
go handleProtoClient(conn, c)
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleProtoClient(conn net.Conn, c chan *pb.Event) {
|
||||||
|
fmt.Println("Connected!")
|
||||||
|
defer conn.Close()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err := io.Copy(&buf, conn)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pdata := new(pb.Event)
|
||||||
|
err = proto.Unmarshal(buf.Bytes(), pdata)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c <- pdata
|
||||||
|
}
|
Loading…
Reference in New Issue