func main() { var iaddr string flag.StringVar(&iaddr, "iaddr", "tcp://localhost:5555", "input data port") var oaddr string flag.StringVar(&oaddr, "oaddr", "tcp://localhost:5556", "output data port") flag.Parse() isck, err := pull.NewSocket() if err != nil { log.Fatalf("error creating a nanomsg socket: %v\n", err) } defer isck.Close() isck.AddTransport(ipc.NewTransport()) isck.AddTransport(tcp.NewTransport()) osck, err := push.NewSocket() if err != nil { log.Fatalf("error creating output port: %v\n", err) } defer osck.Close() osck.AddTransport(ipc.NewTransport()) osck.AddTransport(tcp.NewTransport()) log.Printf("dialing %s ...\n", iaddr) err = isck.Dial(iaddr) if err != nil { log.Fatalf("error dialing: %v\n", err) } log.Printf("dialing %s ... [done]\n", iaddr) log.Printf("dialing %s ...\n", oaddr) err = osck.Dial(oaddr) if err != nil { log.Fatalf("error dialing: %v\n", err) } log.Printf("dialing %s ... [done]\n", oaddr) for { msg, err := isck.Recv() if err != nil { if err == io.EOF || err == mangos.ErrClosed { log.Printf("received EOF: %v\n", err) break } log.Fatalf("error receiving data: %v\n", err) } log.Printf("recv: %v\n", string(msg)) omsg := bytes.Repeat(msg, 2) err = osck.Send(omsg) if err != nil { log.Fatalf("error sending data: %v\n", err) } } }
func TestSimpleCorrect(t *testing.T) { tx, e := pair.NewSocket() if e != nil { t.Fatalf("NewSocket: %v", e) return } rx, e := pair.NewSocket() if e != nil { t.Fatalf("NewSocket: %v", e) return } tx.AddTransport(tcp.NewTransport()) rx.AddTransport(tcp.NewTransport()) if e = rx.Listen(AddrTestTCP); e != nil { t.Fatalf("Listen: %v", e) return } if e = tx.Dial(AddrTestTCP); e != nil { t.Fatalf("Dial: %v", e) return } wg := &sync.WaitGroup{} wg.Add(2) go simpleSend(t, tx, wg) go simpleRecv(t, rx, wg) wg.Wait() }
func node0(url string) { var sock mangos.Socket var err error var msg []byte if sock, err = rep.NewSocket(); err != nil { die("can't get new rep socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on rep socket: %s", err.Error()) } for { // Could also use sock.RecvMsg to get header msg, err = sock.Recv() if string(msg) == "DATE" { // no need to terminate fmt.Println("NODE0: RECEIVED DATE REQUEST") d := date() fmt.Printf("NODE0: SENDING DATE %s\n", d) err = sock.Send([]byte(d)) if err != nil { die("can't send reply: %s", err.Error()) } } } }
func testDevLoop(t *testing.T, addr string) { s1, err := pair.NewSocket() if err != nil { t.Errorf("Failed to open S1: %v", err) return } defer s1.Close() s1.AddTransport(tcp.NewTransport()) s1.AddTransport(inproc.NewTransport()) s1.AddTransport(ipc.NewTransport()) s1.AddTransport(tlstcp.NewTransport()) s1.AddTransport(ws.NewTransport()) s1.AddTransport(wss.NewTransport()) options := make(map[string]interface{}) if strings.HasPrefix(addr, "wss://") || strings.HasPrefix(addr, "tls+tcp://") { options[mangos.OptionTLSConfig] = srvCfg } if err := s1.ListenOptions(addr, options); err != nil { t.Errorf("Failed listening to %s: %v", addr, err) return } if err := mangos.Device(s1, s1); err != nil { t.Errorf("Device failed: %v", err) return } RunTests(t, addr, deviceCaseClient()) }
func server(url string, nworkers int) { var sock mangos.Socket var err error var wg sync.WaitGroup rand.Seed(time.Now().UnixNano()) if sock, err = rep.NewSocket(); err != nil { die("can't get new rep socket: %s", err) } if err = sock.SetOption(mangos.OptionRaw, true); err != nil { die("can't set raw mode: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on rep socket: %s", err.Error()) } wg.Add(nworkers) fmt.Printf("Starting %d workers\n", nworkers) for id := 0; id < nworkers; id++ { go func(id int) { defer wg.Done() serverWorker(sock, id) }(id) } wg.Wait() }
func newEvtChannel(addr string) (mangos.Socket, error) { socket, err := sub.NewSocket() if err != nil { return nil, err } if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*120); err != nil { socket.Close() return nil, err } //socket.AddTransport(ipc.NewTransport()) socket.AddTransport(tcp.NewTransport()) if err := socket.Dial(addr); err != nil { socket.Close() return nil, err } err = socket.SetOption(mangos.OptionSubscribe, []byte("")) if err != nil { return nil, err } return socket, nil }
func main() { var addr string flag.StringVar(&addr, "addr", "tcp://localhost:5555", "input data port") flag.Parse() sck, err := pull.NewSocket() if err != nil { log.Fatalf("error creating a nanomsg socket: %v\n", err) } defer sck.Close() sck.AddTransport(ipc.NewTransport()) sck.AddTransport(tcp.NewTransport()) log.Printf("dialing %s ...\n", addr) err = sck.Dial(addr) if err != nil { log.Fatalf("error dialing: %v\n", err) } log.Printf("dialing %s ... [done]\n", addr) for { msg, err := sck.Recv() if err != nil { if err == io.EOF || err == mangos.ErrClosed { log.Printf("received EOF: %v\n", err) break } log.Fatalf("error receiving from bus: %v\n", err) } log.Printf("recv: %v\n", string(msg)) } }
func newCmdClient(addr string) (mangos.Socket, error) { socket, err := req.NewSocket() if err != nil { return nil, err } if err := socket.SetOption(mangos.OptionSendDeadline, time.Second*3); err != nil { socket.Close() return nil, err } if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*3); err != nil { socket.Close() return nil, err } //socket.AddTransport(ipc.NewTransport()) socket.AddTransport(tcp.NewTransport()) if err := socket.Dial(addr); err != nil { socket.Close() return nil, err } return socket, nil }
func client(url string, name string) { var sock mangos.Socket var err error var msg []byte if sock, err = respondent.NewSocket(); err != nil { die("can't get new respondent socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on respondent socket: %s", err.Error()) } for { if msg, err = sock.Recv(); err != nil { die("Cannot recv: %s", err.Error()) } fmt.Printf("CLIENT(%s): RECEIVED \"%s\" SURVEY REQUEST\n", name, string(msg)) d := date() fmt.Printf("CLIENT(%s): SENDING DATE SURVEY RESPONSE\n", name) if err = sock.Send([]byte(d)); err != nil { die("Cannot send: %s", err.Error()) } } }
func newBusNode(addr string) (busNode, error) { var err error var node busNode sock, err := bus.NewSocket() if err != nil { return node, err } node.sock = sock node.sock.AddTransport(ipc.NewTransport()) node.sock.AddTransport(tcp.NewTransport()) err = node.sock.Listen("tcp://127.0.0.1:0") if err != nil { return node, err } err = node.sock.Dial(addr) if err != nil { return node, err } return node, err }
func server(url string) { var sock mangos.Socket var err error var msg []byte if sock, err = surveyor.NewSocket(); err != nil { die("can't get new surveyor socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on surveyor socket: %s", err.Error()) } err = sock.SetOption(mangos.OptionSurveyTime, time.Second/2) if err != nil { die("SetOption(): %s", err.Error()) } for { time.Sleep(time.Second) fmt.Println("SERVER: SENDING DATE SURVEY REQUEST") if err = sock.Send([]byte("DATE")); err != nil { die("Failed sending survey: %s", err.Error()) } for { if msg, err = sock.Recv(); err != nil { break } fmt.Printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n", string(msg)) } fmt.Println("SERVER: SURVEY OVER") } }
func client(url string, name string) { var sock mangos.Socket var err error var msg []byte if sock, err = sub.NewSocket(); err != nil { die("can't get new sub socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on sub socket: %s", err.Error()) } // Empty byte array effectively subscribes to everything err = sock.SetOption(mangos.OptionSubscribe, []byte("")) if err != nil { die("cannot subscribe: %s", err.Error()) } for { if msg, err = sock.Recv(); err != nil { die("Cannot recv: %s", err.Error()) } fmt.Printf("CLIENT(%s): RECEIVED %s\n", name, string(msg)) } }
// NewDaemon creates and returns a new Daemon from the provided Config. An // error is returned if the Daemon cannot be created. func NewDaemon(config *Config) (*Daemon, error) { rep, err := rep.NewSocket() if err != nil { return nil, err } rep.AddTransport(tcp.NewTransport()) return &Daemon{rep, nil, []*publisher{}, []*subscriber{}, config}, nil }
// AddTransports adds all known transports to the given socket. func AddTransports(sock mangos.Socket) { sock.AddTransport(tcp.NewTransport()) sock.AddTransport(inproc.NewTransport()) sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tlstcp.NewTransport()) sock.AddTransport(ws.NewTransport()) sock.AddTransport(wss.NewTransport()) }
// NewClient creates and returns a new Client from the provided Benchmark // configuration. It returns an error if the Benchmark is not valid or it // can't communicate with any of the specified peers. func NewClient(b *Benchmark) (*Client, error) { if err := b.validate(); err != nil { return nil, err } brokerd, err := req.NewSocket() if err != nil { return nil, err } brokerd.AddTransport(tcp.NewTransport()) brokerd.SetOption(mangos.OptionSendDeadline, time.Duration(b.DaemonTimeout)*time.Second) brokerd.SetOption(mangos.OptionRecvDeadline, time.Duration(b.DaemonTimeout)*time.Second) if err := brokerd.Dial(fmt.Sprintf("tcp://%s", b.BrokerdHost)); err != nil { return nil, err } peerd := make(map[string]mangos.Socket, len(b.PeerHosts)) for _, peer := range b.PeerHosts { s, err := req.NewSocket() if err != nil { return nil, err } s.AddTransport(tcp.NewTransport()) s.SetOption(mangos.OptionSendDeadline, time.Duration(b.DaemonTimeout)*time.Second) s.SetOption(mangos.OptionRecvDeadline, time.Duration(b.DaemonTimeout)*time.Second) if err := s.Dial(fmt.Sprintf("tcp://%s", peer)); err != nil { return nil, err } peerd[peer] = s } return &Client{ brokerd: brokerd, peerd: peerd, Benchmark: b, }, nil }
func newSocket() mangos.Socket { socket, err := pair.NewSocket() if err != nil { log.Fatalf("Node %s: Cannot create socket: %s\n", node, err.Error()) } socket.AddTransport(ipc.NewTransport()) socket.AddTransport(tcp.NewTransport()) socket.SetOption(mangos.OptionRecvDeadline, 10*time.Second) return socket }
func testDevChain(t *testing.T, addr1 string, addr2 string, addr3 string) { // This tests using multiple devices across a few transports. // It looks like this: addr1->addr2->addr3 <==> addr3->addr2->addr1 var err error s := make([]mangos.Socket, 5) for i := 0; i < 5; i++ { if s[i], err = pair.NewSocket(); err != nil { t.Errorf("Failed to open S1_1: %v", err) return } defer s[i].Close() s[i].AddTransport(tcp.NewTransport()) s[i].AddTransport(inproc.NewTransport()) s[i].AddTransport(tlstcp.NewTransport()) s[i].AddTransport(ipc.NewTransport()) s[i].AddTransport(ws.NewTransport()) } if err = s[0].Listen(addr1); err != nil { t.Errorf("s[0] Listen: %v", err) return } if err = s[1].Dial(addr2); err != nil { t.Errorf("s[1] Dial: %v", err) return } if err = s[2].Listen(addr2); err != nil { t.Errorf("s[2] Listen: %v", err) return } if err = s[3].Dial(addr3); err != nil { t.Errorf("s[3] Dial: %v", err) return } if err = s[4].Listen(addr3); err != nil { t.Errorf("s[4] Listen: %v", err) return } if err = mangos.Device(s[0], s[1]); err != nil { t.Errorf("s[0],s[1] Device: %v", err) return } if err = mangos.Device(s[2], s[3]); err != nil { t.Errorf("s[2],s[3] Device: %v", err) return } if err = mangos.Device(s[4], nil); err != nil { t.Errorf("s[4] Device: %v", err) return } RunTests(t, addr1, deviceCaseClient()) }
func node0(url string) { var sock mangos.Socket var err error if sock, err = pair.NewSocket(); err != nil { die("can't get new pair socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on pair socket: %s", err.Error()) } sendRecv(sock, "node0") }
func starTestNewClient(t *testing.T, addr string, id int) *starTester { var err error bt := &starTester{id: id, rdoneq: make(chan bool), sdoneq: make(chan bool)} if bt.sock, err = star.NewSocket(); err != nil { t.Errorf("Failed getting client %d socket: %v", id, err) return nil } bt.sock.AddTransport(tcp.NewTransport()) if err = bt.sock.Dial(addr); err != nil { t.Errorf("Failed client %d dialing: %v", id, err) bt.sock.Close() return nil } return bt }
func newSocket(url, protocol string) (sock mangos.Socket, err error) { var str string switch protocol { case "bus": sock, err = bus.NewSocket() str = "bus.NewSocket: " case "pair": sock, err = pair.NewSocket() str = "pair.NewSocket: " case "push": sock, err = push.NewSocket() str = "push.NewSocket: " case "pull": sock, err = pull.NewSocket() str = "pull.NewSocket: " case "pub": sock, err = pub.NewSocket() str = "pub.NewSocket: " case "sub": sock, err = sub.NewSocket() str = "sub.NewSocket: " case "request": sock, err = req.NewSocket() str = "request.NewSocket: " case "reply": sock, err = rep.NewSocket() str = "reply.NewSocket: " case "surveyor": sock, err = surveyor.NewSocket() str = "surveyor.NewSocket: " case "respondent": sock, err = respondent.NewSocket() str = "respondent.NewSocket: " } if err != nil { s := fmt.Sprintf(str+" %s", err) return nil, errors.New(s) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) return sock, nil }
func (b *sysbus) init() error { sck, err := bus.NewSocket() if err != nil { b.Errorf("error creating a nanomsg socket: %v\n", err) return err } b.sck = sck b.sck.AddTransport(ipc.NewTransport()) b.sck.AddTransport(tcp.NewTransport()) err = b.sck.Listen(BusAddr) if err != nil { return err } return err }
func node1(url string, msg string) { var sock mangos.Socket var err error if sock, err = push.NewSocket(); err != nil { die("can't get new push socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on push socket: %s", err.Error()) } fmt.Printf("NODE1: SENDING \"%s\"\n", msg) if err = sock.Send([]byte(msg)); err != nil { die("can't send message on push socket: %s", err.Error()) } sock.Close() }
func node0(url string) { var sock mangos.Socket var err error var msg []byte if sock, err = pull.NewSocket(); err != nil { die("can't get new pull socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on pull socket: %s", err.Error()) } for { // Could also use sock.RecvMsg to get header msg, err = sock.Recv() fmt.Printf("NODE0: RECEIVED \"%s\"\n", msg) } }
func newCmdServer(addr string) (mangos.Socket, error) { log.Info("sensor: creating cmd server...") socket, err := rep.NewSocket() if err != nil { return nil, err } if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*3); err != nil { socket.Close() return nil, err } //socket.AddTransport(ipc.NewTransport()) socket.AddTransport(tcp.NewTransport()) if err := socket.Listen(addr); err != nil { socket.Close() return nil, err } return socket, nil }
func newEvtPublisher(addr string) (mangos.Socket, error) { log.Info("sensor: creating event publisher...") socket, err := pub.NewSocket() if err != nil { return nil, err } if err := socket.SetOption(mangos.OptionSendDeadline, time.Second*3); err != nil { socket.Close() return nil, err } //socket.AddTransport(ipc.NewTransport()) socket.AddTransport(tcp.NewTransport()) if err = socket.Listen(addr); err != nil { socket.Close() return nil, err } return socket, nil }
func server(url string) { var sock mangos.Socket var err error if sock, err = pub.NewSocket(); err != nil { die("can't get new pub socket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(url); err != nil { die("can't listen on pub socket: %s", err.Error()) } for { // Could also use sock.RecvMsg to get header d := date() fmt.Printf("SERVER: PUBLISHING DATE %s\n", d) if err = sock.Send([]byte(d)); err != nil { die("Failed publishing: %s", err.Error()) } time.Sleep(time.Second) } }
func node(args []string) { var sock mangos.Socket var err error var msg []byte var x int if sock, err = bus.NewSocket(); err != nil { die("bus.NewSocket: %s", err) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Listen(args[2]); err != nil { die("sock.Listen: %s", err.Error()) } // wait for everyone to start listening time.Sleep(time.Second) for x = 3; x < len(args); x++ { if err = sock.Dial(args[x]); err != nil { die("socket.Dial: %s", err.Error()) } } // wait for everyone to join time.Sleep(time.Second) fmt.Printf("%s: SENDING '%s' ONTO BUS\n", args[1], args[1]) if err = sock.Send([]byte(args[1])); err != nil { die("sock.Send: %s", err.Error()) } for { if msg, err = sock.Recv(); err != nil { die("sock.Recv: %s", err.Error()) } fmt.Printf("%s: RECEIVED \"%s\" FROM BUS\n", args[1], string(msg)) } }
func clientWorker(url string, id int) { var sock mangos.Socket var m *mangos.Message var err error if sock, err = req.NewSocket(); err != nil { die("can't get new req socket: %s", err.Error()) } // Leave this in Cooked mode! sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on req socket: %s", err.Error()) } // send an empty messsage m = mangos.NewMessage(1) if err = sock.SendMsg(m); err != nil { die("can't send request: %s", err.Error()) } if m, err = sock.RecvMsg(); err != nil { die("can't recv reply: %s", err.Error()) } sock.Close() if len(m.Body) != 4 { die("bad response len: %d", len(m.Body)) } worker := binary.BigEndian.Uint32(m.Body[0:]) L.Lock() fmt.Printf("Client: %4d Server: %4d\n", id, worker) L.Unlock() }
func node1(url string) { var sock mangos.Socket var err error var msg []byte if sock, err = req.NewSocket(); err != nil { die("can't get new req socket: %s", err.Error()) } sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tcp.NewTransport()) if err = sock.Dial(url); err != nil { die("can't dial on req socket: %s", err.Error()) } fmt.Printf("NODE1: SENDING DATE REQUEST %s\n", "DATE") if err = sock.Send([]byte("DATE")); err != nil { die("can't send message on push socket: %s", err.Error()) } if msg, err = sock.Recv(); err != nil { die("can't receive date: %s", err.Error()) } fmt.Printf("NODE1: RECEIVED DATE %s\n", string(msg)) sock.Close() }
// Connect creates a connection to the jobqueue server, specific to a single // queue. Timeout determines how long to wait for a response from the server, // not only while connecting, but for all subsequent interactions with it using // the returned Client. func Connect(addr string, queue string, timeout time.Duration) (c *Client, err error) { sock, err := req.NewSocket() if err != nil { return } err = sock.SetOption(mangos.OptionRecvDeadline, timeout) if err != nil { return } sock.AddTransport(tcp.NewTransport()) err = sock.Dial("tcp://" + addr) if err != nil { return } // clients identify themselves (only for the purpose of calling methods that // require the client has previously used Require()) with a UUID; v4 is used // since speed doesn't matter: a typical client executable will only // Connect() once; on the other hand, we avoid any possible problem with // running on machines with low time resolution c = &Client{sock: sock, queue: queue, ch: new(codec.BincHandle), clientid: uuid.NewV4()} // Dial succeeds even when there's no server up, so we test the connection // works with a Ping() ok := c.Ping(timeout) if !ok { sock.Close() c = nil err = Error{queue, "Connect", "", ErrNoServer} } return }