aboutsummaryrefslogtreecommitdiff
path: root/weed/messaging/client/publisher.go
blob: d4c0f798a3ce9b637e4a21fb4f2589d16c4dfd7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package client

import (
	"context"

	"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
)

type Publisher struct {
	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
			}
			if in.Redirect != nil{
			}
			if in.Config != nil{
			}
		}
	}()

	return &Publisher{
		publishClient: stream,
	}, nil
}

func (p *Publisher) Publish(m *messaging_pb.RawData) error {

	return p.publishClient.Send(&messaging_pb.PublishRequest{
		Data: m,
	})

}

func (p *Publisher) Shutdown() {

	p.publishClient.CloseSend()

}