本文整理匯總了Golang中github.com/pebbe/zmq4.Socket.Connect方法的典型用法代碼示例。如果您正苦於以下問題:Golang Socket.Connect方法的具體用法?Golang Socket.Connect怎麽用?Golang Socket.Connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pebbe/zmq4.Socket
的用法示例。
在下文中一共展示了Socket.Connect方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: StartZeromqProtobufTest
func StartZeromqProtobufTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) {
return func(wg *sync.WaitGroup) {
var socket *zmq.Socket
var err error
var request []byte
if socket, err = zmq.NewSocket(zmq.REQ); err != nil {
log.Fatal(err)
}
defer socket.Close()
defer wg.Done()
if err = socket.Connect("tcp://" + address); err != nil {
log.Fatal(err)
}
if request, err = proto.Marshal(&pb.Request{
Method: "TEST",
Payload: strings.Repeat("a", messageSize),
}); err != nil {
log.Fatal(err)
}
*requestSize = len(request)
for i := 0; i < requestsPerClient; i++ {
timer.Time(func() {
socket.SendBytes(request, 0)
socket.Recv(0)
})
}
}
}
示例2: New
func New(serverid int, filename string) Servernode {
var data serverlist
content, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
err = json.Unmarshal(content, &data)
if err != nil {
panic("Error parsing json File")
}
mapofservers = make(map[int]int)
for i := 0; i < len(data.RunningServer); i++ {
mapofservers[data.RunningServer[i].Serverid] = data.RunningServer[i].Portno
}
var s Servernode
s.peers = make(map[int]int)
s.peersocks = make(map[int]*zmq.Socket)
_, exist := mapofservers[serverid]
if exist != true {
panic("Error in creation of new server instance")
}
s.sid = serverid
s.pid = mapofservers[serverid]
s.outbox = make(chan *Envelope)
s.inbox = make(chan *Envelope)
var responder *zmq.Socket
var requester *zmq.Socket
for id, port := range mapofservers {
if id == serverid {
responder, err = zmq.NewSocket(zmq.PULL)
s.Ownsocks = responder
responder.Bind("tcp://*:" + strconv.Itoa(port))
if err != nil {
panic("Problem Binding Server")
}
} else {
requester, err = zmq.NewSocket(zmq.PUSH)
if err != nil {
panic("Problem Binding Server")
}
requester.Connect("tcp://localhost:" + strconv.Itoa(port))
s.peers[id] = port
s.peersocks[port] = requester
}
}
go s.SendMessage()
go s.RecieveMessage(responder)
return s
}
示例3: applyZMQConfig
func applyZMQConfig(socket *zmq.Socket, configs []ZMQConfig) {
for _, config := range configs {
switch config.Type {
case "bind":
socket.Bind(config.Uri)
case "connect":
socket.Connect(config.Uri)
}
}
}
示例4: main
// main entry point for data writer
func main() {
var err error
var writerSocket *zmq4.Socket
var eventSubSocket *zmq4.Socket
fog.Info("program starts")
if writerSocket, err = createWriterSocket(); err != nil {
fog.Critical("createWriterSocket %s", err)
}
defer writerSocket.Close()
fog.Info("binding writer socket to %s", dataWriterAddress)
if err = writerSocket.Bind(dataWriterAddress); err != nil {
fog.Critical("Bind(%s) %s", dataWriterAddress, err)
}
if eventSubSocket, err = createEventSubSocket(); err != nil {
fog.Critical("createEventSubSocket %s", err)
}
defer eventSubSocket.Close()
fog.Info("connecting event sub socket to %s", eventAggregatorPubAddress)
if err = eventSubSocket.Connect(eventAggregatorPubAddress); err != nil {
fog.Critical("Connect(%s) %s", eventAggregatorPubAddress, err)
}
messageChan := NewMessageHandler()
reactor := zmq4.NewReactor()
reactor.AddChannel(tools.NewSignalWatcher(), 1, tools.SigtermHandler)
reactor.AddSocket(writerSocket, zmq4.POLLIN,
NewWriterSocketHandler(writerSocket, messageChan))
reactor.AddSocket(eventSubSocket, zmq4.POLLIN,
NewEventSubSocketHandler(eventSubSocket))
fog.Debug("starting reactor.Run")
reactor.SetVerbose(true)
err = reactor.Run(reactorPollingInterval)
if err == tools.SigtermError {
fog.Info("program terminates normally due to SIGTERM")
} else if errno, ok := err.(syscall.Errno); ok {
// we can get 'interrupted system call' if we get SIGTERM while
// a socket is waiting on a read. That's not too bad.
if errno == syscall.EINTR {
fog.Warn("reactor.Run returns '%s' assuming SIGTERM", errno)
} else {
fog.Error("reactor.Run returns %T '%s'", errno, errno)
}
} else {
fog.Error("reactor.Run returns %T %s", err, err)
}
}
示例5: TestPairTcp
func TestPairTcp(t *testing.T) {
var sb, sc *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{sb, sc} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
sb, err := zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sb.Bind("tcp://127.0.0.1:9736")
if err != nil {
t.Fatal("sb.Bind:", err)
}
sc, err = zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sc.Connect("tcp://127.0.0.1:9736")
if err != nil {
t.Fatal("sc.Connect:", err)
}
msg, err := bounce(sb, sc)
if err != nil {
t.Error(msg, err)
}
err = sc.Close()
sc = nil
if err != nil {
t.Error("sc.Close:", err)
}
err = sb.Close()
sb = nil
if err != nil {
t.Error("sb.Close:", err)
}
}
示例6: NewReplyHandler
func NewReplyHandler() chan<- *ReplyMessage {
replyChan := make(chan *ReplyMessage, replyChanCapacity)
pushSockets := make(map[string]*zmq4.Socket)
go func() {
for replyMessage := range replyChan {
marshalledReply, err := json.Marshal(replyMessage.Content)
if err != nil {
fog.Error("unable to marshall reply %s %s", replyMessage, err)
continue
}
var pushSocket *zmq4.Socket
var ok bool
pushSocket, ok = pushSockets[replyMessage.ClientAddress]
if !ok {
fog.Info("creating PUSH socket to %s", replyMessage.ClientAddress)
if pushSocket, err = createPushSocket(); err != nil {
fog.Error("Unable to create PUSH socket for %s %s",
replyMessage.ClientAddress, err)
continue
}
if err = pushSocket.Connect(replyMessage.ClientAddress); err != nil {
fog.Error("Unable to Connect PUSH socket to %s %s",
replyMessage.ClientAddress, err)
pushSocket.Close()
continue
}
pushSockets[replyMessage.ClientAddress] = pushSocket
}
if _, err = pushSocket.SendMessage(marshalledReply); err != nil {
fog.Error("pushSocket SendMessage to %s failed %s",
replyMessage.ClientAddress, err)
pushSocket.Close()
delete(pushSockets, replyMessage.ClientAddress)
continue
}
}
}()
return replyChan
}
示例7: Add
// Add adds a new sink channel for messages of topic `topic`
func (r *Router) Add(topic string, sink chan<- []byte) error {
var err error
var socket *zmq.Socket
socket, err = r.ctx.NewSocket(zmq.SUB)
if err != nil {
return err
}
if err = socket.Connect(r.addr); err != nil {
return err
}
if err = socket.SetSubscribe(topic); err != nil {
return err
}
r.poller.Add(socket, zmq.POLLIN)
r.receivers[socket] = &receiver{sink: sink}
return nil
}
示例8: ApplyZeroMQConfigs
func ApplyZeroMQConfigs(socket *zmq.Socket, configs []*ZeroMQConfig) (err error) {
for _, config := range configs {
switch config.Mode {
case "connect":
err = socket.Connect(config.URI)
case "bind":
err = socket.Bind(config.URI)
default:
err = errUnknownZeroMQMode
}
if err != nil {
return
}
}
return
}
示例9: handleConnectionRequest
func (c *WorkerConnection) handleConnectionRequest(socket *zmq.Socket, request *connectionRequest) {
c.connectionLock.Lock()
defer c.connectionLock.Unlock()
if request.connect {
if err := socket.Connect(request.endpoint); err != nil {
log.Printf("Failed to connect to endpoint %s: %s\n", request.endpoint, err)
c.connectionResultChan <- err
} else {
c.endpoints[request.endpoint] = true
}
} else {
if err := socket.Connect(request.endpoint); err != nil {
log.Printf("Failed to disconnect from endpoint %s: %s\n", request.endpoint, err)
c.connectionResultChan <- err
} else {
delete(c.endpoints, request.endpoint)
}
}
c.connectionResultChan <- nil
}
示例10: TestSecurityCurve
//.........這裏部分代碼省略.........
t.Fatal("server.SetCurveSecretkey:", err)
}
err = server.SetIdentity("IDENT")
if err != nil {
t.Fatal("server.SetIdentity:", err)
}
server.Bind("tcp://127.0.0.1:9998")
if err != nil {
t.Fatal("server.Bind:", err)
}
err = server.SetRcvtimeo(time.Second)
if err != nil {
t.Fatal("server.SetRcvtimeo:", err)
}
// Check CURVE security with valid credentials
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = client.SetCurveServerkey(server_public)
if err != nil {
t.Fatal("client.SetCurveServerkey:", err)
}
err = client.SetCurvePublickey(client_public)
if err != nil {
t.Fatal("client.SetCurvePublickey:", err)
}
err = client.SetCurveSecretkey(client_secret)
if err != nil {
t.Fatal("client.SetCurveSecretkey:", err)
}
err = client.Connect("tcp://127.0.0.1:9998")
if err != nil {
t.Fatal("client.Connect:", err)
}
msg, err := bounce(server, client)
if err != nil {
t.Error(msg, err)
}
err = client.Close()
client = nil
if err != nil {
t.Fatal("client.Close:", err)
}
time.Sleep(100 * time.Millisecond)
// Check CURVE security with a garbage server key
// This will be caught by the curve_server class, not passed to ZAP
garbage_key := "0000111122223333444455556666777788889999"
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = client.SetCurveServerkey(garbage_key)
if err != nil {
t.Fatal("client.SetCurveServerkey:", err)
}
err = client.SetCurvePublickey(client_public)
if err != nil {
t.Fatal("client.SetCurvePublickey:", err)
}
err = client.SetCurveSecretkey(client_secret)
if err != nil {
示例11: TestPoller
func TestPoller(t *testing.T) {
var sb, sc *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{sb, sc} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
sb, err := zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sb.Bind("tcp://127.0.0.1:9737")
if err != nil {
t.Fatal("sb.Bind:", err)
}
sc, err = zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sc.Connect("tcp://127.0.0.1:9737")
if err != nil {
t.Fatal("sc.Connect:", err)
}
poller := zmq.NewPoller()
idxb := poller.Add(sb, 0)
idxc := poller.Add(sc, 0)
if idxb != 0 || idxc != 1 {
t.Errorf("idxb=%d idxc=%d", idxb, idxc)
}
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 1:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 1 len = %d", len(pa))
} else if pa[0].Events != 0 || pa[1].Events != 0 {
t.Errorf("PollAll 1 events = %v, %v", pa[0], pa[1])
}
poller.Update(idxb, zmq.POLLOUT)
poller.UpdateBySocket(sc, zmq.POLLIN)
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 2:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 2 len = %d", len(pa))
} else if pa[0].Events != zmq.POLLOUT || pa[1].Events != 0 {
t.Errorf("PollAll 2 events = %v, %v", pa[0], pa[1])
}
poller.UpdateBySocket(sb, 0)
content := "12345678ABCDEFGH12345678ABCDEFGH"
// Send message from client to server
if rc, err := sb.Send(content, zmq.DONTWAIT); err != nil {
t.Error("sb.Send DONTWAIT:", err)
} else if rc != 32 {
t.Error("sb.Send DONTWAIT:", err32)
}
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 3:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 3 len = %d", len(pa))
} else if pa[0].Events != 0 || pa[1].Events != zmq.POLLIN {
t.Errorf("PollAll 3 events = %v, %v", pa[0], pa[1])
}
// Receive message
if msg, err := sc.Recv(zmq.DONTWAIT); err != nil {
t.Error("sb.Recv DONTWAIT:", err)
} else if msg != content {
t.Error("sb.Recv msg != content")
}
poller.UpdateBySocket(sb, zmq.POLLOUT)
poller.Update(idxc, zmq.POLLIN)
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 4:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 4 len = %d", len(pa))
} else if pa[0].Events != zmq.POLLOUT || pa[1].Events != 0 {
t.Errorf("PollAll 4 events = %v, %v", pa[0], pa[1])
}
err = sc.Close()
sc = nil
if err != nil {
t.Error("sc.Close:", err)
//.........這裏部分代碼省略.........
示例12: TestHwm
func TestHwm(t *testing.T) {
MAX_SENDS := 10000
BIND_FIRST := 1
CONNECT_FIRST := 2
test_defaults := func() (result int) {
result = -1
// Set up bind socket
bind_socket, err := zmq.NewSocket(zmq.PULL)
if err != nil {
t.Error("NewSocket:", err)
return
}
defer func() {
err := bind_socket.Close()
if err != nil {
t.Error("bind_socket.Close:", err)
}
}()
err = bind_socket.Bind("inproc://a")
if err != nil {
t.Error("bind_socket.Bind:", err)
return
}
// Set up connect socket
connect_socket, err := zmq.NewSocket(zmq.PUSH)
if err != nil {
t.Error("NewSocket:", err)
return
}
defer func() {
err := connect_socket.Close()
if err != nil {
t.Error("connect_socket.Close:", err)
}
}()
err = connect_socket.Connect("inproc://a")
if err != nil {
t.Error("connect_socket.Connect:", err)
return
}
// Send until we block
send_count := 0
for send_count < MAX_SENDS {
_, err := connect_socket.Send("", zmq.DONTWAIT)
if err != nil {
break
}
send_count++
}
// Now receive all sent messages
recv_count := 0
for {
_, err := bind_socket.Recv(zmq.DONTWAIT)
if err != nil {
break
}
recv_count++
}
if send_count != recv_count {
t.Error("test_defaults: send_count == recv_count")
}
return send_count
}
count_msg := func(send_hwm, recv_hwm, testType int) (result int) {
result = -1
var bind_socket, connect_socket *zmq.Socket
var err error
if testType == BIND_FIRST {
// Set up bind socket
bind_socket, err = zmq.NewSocket(zmq.PULL)
if err != nil {
t.Error("NewSocket:", err)
return
}
defer func() {
err := bind_socket.Close()
if err != nil {
t.Error("bind_socket.Close:", err)
}
}()
err = bind_socket.SetRcvhwm(recv_hwm)
if err != nil {
t.Error("bind_socket.SetRcvhwm:", err)
return
}
//.........這裏部分代碼省略.........
示例13: TestConflate
func TestConflate(t *testing.T) {
var s_in, s_out *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{s_in, s_out} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
bind_to := "tcp://127.0.0.1:5555"
err := zmq.SetIoThreads(1)
if err != nil {
t.Fatal("SetIoThreads(1):", err)
}
s_in, err = zmq.NewSocket(zmq.PULL)
if err != nil {
t.Fatal("NewSocket 1:", err)
}
err = s_in.SetConflate(true)
if err != nil {
t.Fatal("SetConflate(true):", err)
}
err = s_in.Bind(bind_to)
if err != nil {
t.Fatal("s_in.Bind:", err)
}
s_out, err = zmq.NewSocket(zmq.PUSH)
if err != nil {
t.Fatal("NewSocket 2:", err)
}
err = s_out.Connect(bind_to)
if err != nil {
t.Fatal("s_out.Connect:", err)
}
message_count := 20
for j := 0; j < message_count; j++ {
_, err = s_out.Send(fmt.Sprint(j), 0)
if err != nil {
t.Fatalf("s_out.Send %d: %v", j, err)
}
}
time.Sleep(time.Second)
payload_recved, err := s_in.Recv(0)
if err != nil {
t.Error("s_in.Recv:", err)
} else {
i, err := strconv.Atoi(payload_recved)
if err != nil {
t.Error("strconv.Atoi:", err)
}
if i != message_count-1 {
t.Error("payload_recved != message_count - 1")
}
}
err = s_in.Close()
s_in = nil
if err != nil {
t.Error("s_in.Close:", err)
}
err = s_out.Close()
s_out = nil
if err != nil {
t.Error("s_out.Close:", err)
}
}
示例14: TestAbstractIpc
func TestAbstractIpc(t *testing.T) {
var sb, sc *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{sb, sc} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
addr := "ipc://@/tmp/tester"
// This is Linux only
if runtime.GOOS != "linux" {
t.Skip("Only on Linux")
}
sb, err := zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sb.Bind(addr)
if err != nil {
t.Fatal("sb.Bind:", err)
}
endpoint, err := sb.GetLastEndpoint()
expected := "ipc://@/tmp/tester"
if endpoint != expected || err != nil {
t.Fatalf("sb.GetLastEndpoint: expected 'nil' %q, got '%v' %q", expected, err, endpoint)
return
}
sc, err = zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sc.Connect(addr)
if err != nil {
t.Fatal("sc.Bind:", err)
}
resp, err := bounce(sb, sc)
if err != nil {
t.Error(resp, err)
}
err = sc.Close()
sc = nil
if err != nil {
t.Fatal("sc.Close:", err)
}
err = sb.Close()
sb = nil
if err != nil {
t.Fatal("sb.Close:", err)
}
}
示例15: main
func main() {
flag.Parse(nil)
address_list := flag.Args
if len(address_list) == 0 {
fmt.Println("No Addresses submitted")
fmt.Println(flag.Help())
return
}
var send, recv bool
skip := false
var socket *zmq.Socket
switch *socket_type {
case "PUSH":
socket, _ = zmq.NewSocket(zmq.PUSH)
send = true
case "PULL":
socket, _ = zmq.NewSocket(zmq.PULL)
recv = true
case "PUB":
socket, _ = zmq.NewSocket(zmq.PUB)
send = true
case "SUB":
socket, _ = zmq.NewSocket(zmq.SUB)
recv = true
if len(*subscriptions) == 0 {
socket.SetSubscribe("")
}
for _, subscription := range *subscriptions {
socket.SetSubscribe(subscription)
}
case "REQ":
socket, _ = zmq.NewSocket(zmq.REQ)
send = true
recv = true
case "REP":
socket, _ = zmq.NewSocket(zmq.REP)
send = true
recv = true
skip = true
}
defer socket.Close()
// connect or bind
if *mode {
for _, address := range address_list {
socket.Connect(address)
}
} else {
for _, address := range address_list {
socket.Bind(address)
}
}
delim := byte('\n')
if *null {
fmt.Println("Setting delim to null")
delim = byte(0x00)
}
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
for i := 0; i < *number || *number == -1; i++ {
if send && !skip {
line, _ := reader.ReadBytes(delim)
socket.SendBytes([]byte(line), 0)
}
if recv {
data, _ := socket.RecvBytes(0)
writer.Write(data)
writer.Flush()
}
if skip {
skip = false
}
}
fmt.Println("finished", *number)
}