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 Listen(sock mangos.Socket, url string) error { if err := sock.Listen(url); err != nil { s := fmt.Sprintf("socket.Listen: %s", err) return errors.New(s) } return nil }
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 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 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 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 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)) } }