Skip to content

Instantly share code, notes, and snippets.

@indradhanush
Last active November 22, 2015 14:49
Show Gist options
  • Save indradhanush/d1676444a6ca18ff5018 to your computer and use it in GitHub Desktop.
Save indradhanush/d1676444a6ca18ff5018 to your computer and use it in GitHub Desktop.
Attempt to get zmq handshake working
type Conn interface {
net.Conn
Connect(endpoint string) error
CurrentIdx() int
Serve() error
handleConnection(conn net.Conn) error
}
type Sock struct {
conns []net.Conn
listener net.Listener
currentIdx int
sockType byte
}
func NewPullConn(endpoint string) (Conn, error) {
var err error
s := Sock{
currentIdx: 0,
sockType: Pull,
}
addrParts := strings.Split(endpoint, "://")
if len(addrParts) != 2 {
return s, fmt.Errorf("malformed address")
}
s.listener, err = net.Listen(addrParts[0], addrParts[1])
if err != nil {
return s, err
}
go s.Serve()
return s, err
}
func (s Sock) Serve() error {
for {
conn, err := s.listener.Accept()
if err != nil {
fmt.Println("error in accepting")
return err
}
fmt.Println("got incoming", conn)
zmtpGreetOutgoing := &greeter{
sockType: s.sockType,
}
err = zmtpGreetOutgoing.greet(conn)
if err != nil {
fmt.Println("error in sending greet")
return err
}
s.conns = append(s.conns, conn)
go s.handleConnection(conn)
}
}
func (s Sock) handleConnection(conn net.Conn) error {
msg := make([]byte, 256)
n, err := conn.Read(msg)
if err != nil {
fmt.Println("error in receiving")
return err
}
fmt.Println("read bytes:", n)
fmt.Println(msg[:n])
return nil
}
// minimal testcase to start off with
func TestPullSockSingleConnection(t *testing.T) {
endpoint := "tcp://127.0.0.1:9998"
t.Log("new pull")
_, err := NewPullConn(endpoint)
if err != nil {
t.Fatal(err)
}
t.Log("new push")
push, err := NewPushConn(endpoint)
if err != nil {
t.Fatal(err)
}
t.Log("push hello")
push.Write([]byte("Hello"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment