Example #1
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
}
Example #2
0
func (pt *PushTest) Init(t *testing.T, addr string) bool {
	var err error
	if pt.Sock, err = push.NewSocket(); err != nil {
		pt.Errorf("NewSocket(): %v", err)
		return false
	}
	return pt.T.Init(t, addr)
}
Example #3
0
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()
}
Example #4
0
File: gen.go Project: jfsmig/lbtk
func main() {
	how_rand := flag.Bool("rand", false, "")
	flag.Parse()
	if flag.NArg() < 1 {
		log.Fatal("Missing arguments: at least one endpoint to bind to")
	}

	var err error
	var out mangos.Socket
	if out, err = push.NewSocket(); err != nil {
		log.Fatal("Nanomsg socket creation failure: ", err)
	} else {
		defer out.Close()
		out.AddTransport(ipc.NewTransport())
		out.AddTransport(tcp.NewTransport())
		out.SetOption(mangos.OptionWriteQLen, 16)
		out.SetOption(mangos.OptionRetryTime, 0)
		for i := 0; i < flag.NArg(); i++ {
			if err := out.Listen(flag.Arg(i)); err != nil {
				log.Fatal("Nanomsg listen() error: ", err)
			}
		}
	}

	switch {
	case *how_rand:
		Gen(out, func(tab []string) string {
			if len(tab) <= 0 {
				return ""
			}
			return tab[rand.Intn(len(tab))]
		})
	default:
		i := 0
		Gen(out, func(tab []string) string {
			if len(tab) <= 0 {
				return ""
			}
			i = (i + 1) % len(tab)
			return tab[i]
		})
	}
}
Example #5
0
func (c *Context) Run(name string) {
	push, _ := push.NewSocket()
	push.AddTransport(tcp.NewTransport())
	push.Dial("tcp://127.0.0.1:5006")

	pull, _ := sub.NewSocket()
	pull.AddTransport(tcp.NewTransport())
	pull.Dial("tcp://127.0.0.1:5005")
	pull.SetOption(mangos.OptionSubscribe, []byte(""))

	for {
		var protocol [4][]byte
		bytes, _ := pull.Recv()
		msgpack.Unmarshal(bytes, &protocol)

		if string(protocol[1]) != "*" && string(protocol[1]) != name {
			continue
		}

		if string(protocol[2]) == "message" {
			var message [5][]byte
			msgpack.Unmarshal(protocol[3], &message)

			for _, f := range c.messages {
				result := f(Message{
					string(message[0]),
					string(message[1]),
					string(message[2]),
					string(message[3]),
					string(message[4]),
				})

				if result != "" {
					packed, _ := msgpack.Marshal([5]string{
						string(message[0]),
						string(message[2]),
						string(message[1]),
						result,
						string(message[4]),
					})

					output, _ := msgpack.Marshal([4]string{
						name,
						"protocol." + string(message[0]),
						"message",
						string(packed),
					})

					push.Send(output)
				}
			}
		}

		if string(protocol[2]) == "command" {
			var message [2]interface{}
			msgpack.Unmarshal(protocol[3], &message)

			var embed [5][]byte
			msgpack.Unmarshal([]byte(message[0].(string)), &embed)

			orig, _ := message[0].(string)
			line, _ := message[1].([]interface{})[0].(string)
			command := strings.SplitN(line, " ", 2)
			f := c.commands[strings.TrimPrefix(command[0], ".")]

			result := f(Command{
				strings.TrimPrefix(command[0], "."),
				command[1],
				Message{
					string(embed[0]),
					string(embed[1]),
					string(embed[2]),
					string(embed[3]),
					string(embed[4]),
				},
			})

			if result != "" {
				new_messages := message[1].([]interface{})
				new_messages = append([]interface{}{result}, new_messages[1:]...)

				packed, _ := msgpack.Marshal([2]interface{}{
					orig,
					new_messages,
				})

				output, _ := msgpack.Marshal([4]string{
					name,
					string(protocol[0]),
					"response",
					string(packed),
				})

				push.Send(output)
			}
		}
	}
}