예제 #1
0
파일: main.go 프로젝트: mkrzewic/FairRoot
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)
		}
	}

}
예제 #2
0
파일: main.go 프로젝트: mkrzewic/FairRoot
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))
	}

}
예제 #3
0
// NewSocket allocates a new Socket.  The Socket is the handle used to
// access the underlying library.
func NewSocket(d Domain, p Protocol) (*Socket, error) {

	var s Socket
	var err error

	s.proto = p
	s.dom = d

	switch p {
	case PUB:
		s.sock, err = pub.NewSocket()
	case SUB:
		s.sock, err = sub.NewSocket()
	case PUSH:
		s.sock, err = push.NewSocket()
	case PULL:
		s.sock, err = pull.NewSocket()
	case REQ:
		s.sock, err = req.NewSocket()
	case REP:
		s.sock, err = rep.NewSocket()
	case SURVEYOR:
		s.sock, err = surveyor.NewSocket()
	case RESPONDENT:
		s.sock, err = respondent.NewSocket()
	case PAIR:
		s.sock, err = pair.NewSocket()
	case BUS:
		s.sock, err = bus.NewSocket()
	default:
		err = mangos.ErrBadProto
	}

	if err != nil {
		return nil, err
	}

	switch d {
	case AF_SP:
	case AF_SP_RAW:
		err = s.sock.SetOption(mangos.OptionRaw, true)
	default:
		err = errBadDomain
	}
	if err != nil {
		s.sock.Close()
		return nil, err
	}

	// Compat mode sockets should timeout on send if we don't have any pipes
	if err = s.sock.SetOption(mangos.OptionWriteQLen, 0); err != nil {
		s.sock.Close()
		return nil, err
	}

	s.rto = -1
	s.sto = -1
	all.AddTransports(s.sock)
	return &s, nil
}
예제 #4
0
func (pt *PullTest) Init(t *testing.T, addr string) bool {
	var err error
	if pt.Sock, err = pull.NewSocket(); err != nil {
		pt.Errorf("NewSocket(): %v", err)
		return false
	}
	return pt.T.Init(t, addr)
}
예제 #5
0
파일: msgq.go 프로젝트: ibmendoza/msgq
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
}
예제 #6
0
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)
	}
}