本文整理汇总了Golang中github.com/pebbe/zmq4.NewContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewContext函数的具体用法?Golang NewContext怎么用?Golang NewContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: InitSocket
func (this *Spout) InitSocket(pull, push string) (err error) {
var reader, writer *zmq.Context
reader, err = zmq.NewContext()
if err != nil {
return
}
this.Component.Reader, err = reader.NewSocket(zmq.PULL)
if err != nil {
return
}
err = this.Component.Reader.Connect("tcp://127.0.0.1:" + pull)
if err != nil {
return
}
writer, err = zmq.NewContext()
if err != nil {
return
}
this.Component.Writer, err = writer.NewSocket(zmq.PUSH)
if err != nil {
return
}
err = this.Component.Writer.Bind("tcp://127.0.0.1:" + push)
return
}
示例2: BindPublisher
func BindPublisher(endpoint string) *GlcZmq {
context, err := zmq.NewContext()
if err != nil {
log.Fatal(err)
}
socket, err := context.NewSocket(zmq.PUB)
if err != nil {
log.Fatal(err)
}
go func(socket *zmq.Socket, endpoint string) {
defer socket.Close()
err = socket.Connect(endpoint)
if err != nil {
log.Fatal(err)
}
for {
time.Sleep(1 * time.Second)
}
}(socket, endpoint)
return &GlcZmq{socket}
}
示例3: BindSubscriber
func BindSubscriber(endpoint string, filter string, callback func(message string)) *GlcZmq {
context, err := zmq.NewContext()
socket, err := context.NewSocket(zmq.PUB)
defer socket.Close()
if err != nil {
log.Fatal(err)
}
err = socket.Connect(endpoint)
if err != nil {
log.Fatal(err)
}
for {
msg, _ := socket.Recv(zmq.DONTWAIT)
if msg != "" {
callback(msg)
}
}
return &GlcZmq{socket}
}
示例4: NewCommunicator
// Initiate connections to all replicas
func NewCommunicator(name string, replicas map[string]*ReplicaInfo) (*Communicator, error) {
communicator := new(Communicator)
context, err := zmq.NewContext()
if err != nil {
return nil, err
}
communicator.context = context
communicator.pubSocket, err = context.NewSocket(zmq.PUB)
if err != nil {
return nil, err
}
communicator.subSocket, err = context.NewSocket(zmq.SUB)
communicator.chunkSocket, err = context.NewSocket(zmq.REP)
for key, val := range replicas {
if replicas[key].Name == name {
communicator.pubSocket.Bind("tcp://*:" + val.Port)
communicator.chunkSocket.Bind("tcp://*:" + val.DemandPort)
communicator.tag = val.Pid
} else {
communicator.subSocket.Connect("tcp://" + val.IpAddr + ":" + val.Port)
communicator.subSocket.SetSubscribe(val.Pid)
}
}
communicator.replicas = replicas
return communicator, nil
}
示例5: BindProxy
func BindProxy(frontend string, backend string) {
context, err := zmq.NewContext()
if err != nil {
log.Fatal(err)
}
// create XSUB for publishers to connect to
xSub, _ := context.NewSocket(zmq.XSUB)
defer xSub.Close()
err = xSub.Bind(frontend)
if err != nil {
log.Fatal(err)
}
// create XPUB for subscribers to connect to
xPub, _ := context.NewSocket(zmq.XPUB)
defer xPub.Close()
err = xPub.Bind(backend)
if err != nil {
log.Fatal(err)
}
err = zmq.Proxy(xSub, xPub, nil)
log.Fatalln("Proxy interrupted:", err)
}
示例6: createSockets
// createSockets sets up the 0MQ sockets through which the kernel will
// communicate.
func createSockets(connInfo *ConnectionInfo) (*zmq.Context, *sockets, error) {
context, err := zmq.NewContext()
if err != nil {
return nil, nil, err
}
bindSocket := func(t zmq.Type, port int) (*zmq.Socket, error) {
addr := fmt.Sprintf(
"%s://%s:%v", connInfo.Transport, connInfo.IP, port,
)
socket, err := context.NewSocket(t)
if err != nil {
return nil, err
}
if err := socket.Bind(addr); err != nil {
socket.Close()
return nil, err
}
return socket, nil
}
var sockets sockets
var heartbeatSocket *zmq.Socket
socketPorts := []struct {
Name string
Port int
Type zmq.Type
Socket **zmq.Socket
}{
{"heartbeat", connInfo.HeartbeatPort, zmq.REP, &heartbeatSocket},
{"shell", connInfo.ShellPort, zmq.ROUTER, &sockets.Shell},
{"control", connInfo.ControlPort, zmq.ROUTER, &sockets.Control},
{"stdin", connInfo.StdinPort, zmq.ROUTER, &sockets.Stdin},
{"iopub", connInfo.IOPubPort, zmq.PUB, &sockets.IOPub},
}
for _, socketPort := range socketPorts {
socket, err := bindSocket(socketPort.Type, socketPort.Port)
if err != nil {
// TODO(axw) do we need to close all sockets if one
// fails? Is terminating the context good enough?
if err := context.Term(); err != nil {
log.Printf("terminating context: %v", err)
}
return nil, nil, fmt.Errorf(
"creating %v socket: %v", socketPort.Name, err,
)
}
*socketPort.Socket = socket
}
go zmq.Proxy(heartbeatSocket, heartbeatSocket, nil)
return context, &sockets, nil
}
示例7: Init
/******************************************************************************
* 概述: Zmq初始化
* 函数名: Init
* 返回值: error
* 参数列表: 参数名 参数类型 取值范围 描述
*
*******************************************************************************/
func (this *Zmq) Init(dataCache *DataCache, event *Event) error {
this.MdataCache = dataCache
this.Mevent = event
if this.Mcontext != nil {
return nil
}
context, err := zmq4.NewContext()
if err != nil {
return err
}
this.Mcontext = context
return nil
}
示例8: New
func New(addr string) (*Router, error) {
ctx, err := zmq.NewContext()
if err != nil {
return nil, err
}
router := &Router{
ctx: ctx,
receivers: make(map[*zmq.Socket]*receiver),
poller: zmq.NewPoller(),
addr: addr,
}
return router, nil
}
示例9: setSock
func (this *sock) setSock(pattern zmq.Type) {
var err error
this.ctx, err = zmq.NewContext()
if err != nil {
panic(err)
}
this.zmqSock, err = zmq.NewSocket(pattern)
if err != nil {
panic(err)
}
this.initialiser.init(this.zmqSock)
}
示例10: main
func main() {
context, _ := zmq.NewContext()
server, _ := context.NewSocket(zmq.REP)
defer server.Close()
server.Bind(SERVER_END)
for cycles := 1; ; cycles++ {
request, _ := server.Recv(0)
fmt.Printf("Request (%d) : (%s)\n", cycles, request)
server.Send(request, 0)
}
}
示例11: createSockets
// createSockets sets up the 0MQ sockets through which the kernel will
// communicate.
func createSockets(connInfo *ConnectionInfo) (*zmq.Context, *sockets, error) {
context, err := zmq.NewContext()
if err != nil {
return nil, nil, err
}
bindSocket := func(t zmq.Type, port int) (*zmq.Socket, error) {
addr := fmt.Sprintf(
"%s://%s:%v", connInfo.Transport, connInfo.IP, port,
)
socket, err := context.NewSocket(t)
if err != nil {
return nil, err
}
if err := socket.Bind(addr); err != nil {
socket.Close()
return nil, err
}
return socket, nil
}
sockets := sockets{
Heartbeat: socket{Name: "heartbeat", Port: connInfo.HeartbeatPort, Type: zmq.REP},
Shell: socket{Name: "shell", Port: connInfo.ShellPort, Type: zmq.ROUTER},
Control: socket{Name: "control", Port: connInfo.ControlPort, Type: zmq.ROUTER},
Stdin: socket{Name: "stdin", Port: connInfo.StdinPort, Type: zmq.ROUTER},
IOPub: socket{Name: "iopub", Port: connInfo.IOPubPort, Type: zmq.PUB},
}
for _, socketPtr := range sockets.sockets() {
socket, err := bindSocket(socketPtr.Type, socketPtr.Port)
if err == nil {
socketPtr.Socket = socket
err = socket.SetLinger(0)
}
if err != nil {
sockets.tryClose()
if err := context.Term(); err != nil {
log.Printf("error terminating context: %v", err)
}
return nil, nil, fmt.Errorf(
"creating %v socket: %v", socketPtr.Name, err,
)
}
}
go zmq.Proxy(sockets.Heartbeat.Socket, sockets.Heartbeat.Socket, nil)
return context, &sockets, nil
}
示例12: NewQueue
// New acts as a queue constructor
func NewQueue(uri string) (Queue, error) {
u := "tcp://" + uri
c, _ := zmq.NewContext()
pullSoc, pullErr := c.NewSocket(zmq.PULL)
if pullErr != nil {
return Queue{}, fmt.Errorf("pull socket initialization failed: %v", pullErr)
}
pushSoc, pushErr := c.NewSocket(zmq.PUSH)
if pushErr != nil {
return Queue{}, fmt.Errorf("push socket initialization failed: %v", pushErr)
}
q := Queue{u, pushSoc, pullSoc}
return q, nil
}
示例13: createPushServer
func createPushServer(impl *server) error {
ctx, err := zmq.NewContext()
if err != nil {
return err
}
socket, err := ctx.NewSocket(zmq.PUB)
if err != nil {
return err
}
if err = socket.Bind(fmt.Sprintf("tcp://*:%d", *pushPort)); err != nil {
return err
}
impl.socket = socket
log.Printf("PUSH service on %d", *pushPort)
impl.start()
return nil
}
示例14: main
func main() {
context, _ := zmq.NewContext()
fmt.Println("Connecting to server .... ")
client, _ := context.NewSocket(zmq.REQ)
client.Connect(SERVER_ENDPOINT)
for i := 0; i < 10; i++ {
msg := fmt.Sprintf("msg %d", i)
client.Send((msg), 0)
fmt.Println("Sending Message ", msg)
client.Recv(0)
}
}
示例15: newDealer
func newDealer() *dealer {
ctx, _ := zmq.NewContext()
msgCh := make(chan string)
doneCh := make(chan bool)
errCh := make(chan error)
client, err := ctx.NewSocket(zmq.DEALER)
if err != nil {
logger.Error.Println("Error openinng DEALER socket", err)
os.Exit(1)
}
return &dealer{
ctx: ctx,
msgCh: msgCh,
doneCh: doneCh,
errCh: errCh,
client: client,
}
}