2020-04-18 19:44:55 +00:00
|
|
|
package client
|
|
|
|
|
2020-04-18 22:17:27 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
|
|
|
|
)
|
2020-04-18 19:44:55 +00:00
|
|
|
|
|
|
|
type Publisher struct {
|
2020-04-18 22:17:27 +00:00
|
|
|
publishClient messaging_pb.SeaweedMessaging_PublishClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MessagingClient) NewPublisher(namespace, topic string) (*Publisher, error) {
|
|
|
|
|
|
|
|
stream, err := messaging_pb.NewSeaweedMessagingClient(mc.grpcConnection).Publish(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send init message
|
|
|
|
err = stream.Send(&messaging_pb.PublishRequest{
|
|
|
|
Init: &messaging_pb.PublishRequest_InitMessage{
|
|
|
|
Namespace: namespace,
|
|
|
|
Topic: topic,
|
|
|
|
Partition: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// process init response
|
|
|
|
initResponse, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if initResponse.Redirect != nil {
|
|
|
|
// TODO follow redirection
|
|
|
|
}
|
|
|
|
if initResponse.Config != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup looks for control messages
|
|
|
|
doneChan := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
in, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
doneChan <- err
|
|
|
|
return
|
|
|
|
}
|
2020-04-21 00:48:06 +00:00
|
|
|
if in.Redirect != nil {
|
2020-04-18 22:17:27 +00:00
|
|
|
}
|
2020-04-21 00:48:06 +00:00
|
|
|
if in.Config != nil {
|
2020-04-18 22:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return &Publisher{
|
|
|
|
publishClient: stream,
|
|
|
|
}, nil
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 06:37:04 +00:00
|
|
|
func (p *Publisher) Publish(m *messaging_pb.Message) error {
|
2020-04-18 22:17:27 +00:00
|
|
|
|
|
|
|
return p.publishClient.Send(&messaging_pb.PublishRequest{
|
|
|
|
Data: m,
|
|
|
|
})
|
|
|
|
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 10:03:40 +00:00
|
|
|
func (p *Publisher) Close() error {
|
2020-04-18 22:17:27 +00:00
|
|
|
|
2020-04-19 10:03:40 +00:00
|
|
|
return p.publishClient.CloseSend()
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|