forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
319 lines (293 loc) · 9.07 KB
/
pubsub.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/textproto"
"os"
"strings"
pubsub "google.golang.org/api/pubsub/v1beta2"
)
const USAGE = `Available arguments are:
<project_id> list_topics
<project_id> create_topic <topic>
<project_id> delete_topic <topic>
<project_id> list_subscriptions
<project_id> create_subscription <subscription> <linked topic>
<project_id> delete_subscription <subscription>
<project_id> connect_irc <topic> <server> <channel>
<project_id> pull_messages <subscription>
`
type IRCBot struct {
server string
port string
nick string
user string
channel string
conn net.Conn
tpReader *textproto.Reader
}
func NewIRCBot(server, channel, nick string) *IRCBot {
return &IRCBot{
server: server,
port: "6667",
nick: nick,
channel: channel,
conn: nil,
user: nick,
}
}
func (bot *IRCBot) Connect() {
conn, err := net.Dial("tcp", bot.server+":"+bot.port)
if err != nil {
log.Fatal("unable to connect to IRC server ", err)
}
bot.conn = conn
log.Printf("Connected to IRC server %s (%s)\n",
bot.server, bot.conn.RemoteAddr())
bot.tpReader = textproto.NewReader(bufio.NewReader(bot.conn))
bot.Sendf("USER %s 8 * :%s\r\n", bot.nick, bot.nick)
bot.Sendf("NICK %s\r\n", bot.nick)
bot.Sendf("JOIN %s\r\n", bot.channel)
}
func (bot *IRCBot) CheckConnection() {
for {
line, err := bot.ReadLine()
if err != nil {
log.Fatal("Unable to read a line during checking the connection.")
}
if parts := strings.Split(line, " "); len(parts) > 1 {
if parts[1] == "004" {
log.Println("The nick accepted.")
} else if parts[1] == "433" {
log.Fatalf("The nick is already in use: %s", line)
} else if parts[1] == "366" {
log.Println("Starting to publish messages.")
return
}
}
}
}
func (bot *IRCBot) Sendf(format string, args ...interface{}) {
fmt.Fprintf(bot.conn, format, args...)
}
func (bot *IRCBot) Close() {
bot.conn.Close()
}
func (bot *IRCBot) ReadLine() (line string, err error) {
return bot.tpReader.ReadLine()
}
func init() {
registerDemo("pubsub", pubsub.PubsubScope, pubsubMain)
}
func pubsubUsage() {
fmt.Fprint(os.Stderr, USAGE)
}
// Returns a fully qualified resource name for Cloud Pub/Sub.
func fqrn(res, proj, name string) string {
return fmt.Sprintf("projects/%s/%s/%s", proj, res, name)
}
func fullTopicName(proj, topic string) string {
return fqrn("topics", proj, topic)
}
func fullSubName(proj, topic string) string {
return fqrn("subscriptions", proj, topic)
}
// Check the length of the arguments.
func checkArgs(argv []string, min int) {
if len(argv) < min {
pubsubUsage()
os.Exit(2)
}
}
func listTopics(service *pubsub.Service, argv []string) {
next := ""
for {
topicsList, err := service.Projects.Topics.List(fmt.Sprintf("projects/%s", argv[0])).PageToken(next).Do()
if err != nil {
log.Fatalf("listTopics query.Do() failed: %v", err)
}
for _, topic := range topicsList.Topics {
fmt.Println(topic.Name)
}
next = topicsList.NextPageToken
if next == "" {
break
}
}
}
func createTopic(service *pubsub.Service, argv []string) {
checkArgs(argv, 3)
topic, err := service.Projects.Topics.Create(fullTopicName(argv[0], argv[2]), &pubsub.Topic{}).Do()
if err != nil {
log.Fatalf("createTopic Create().Do() failed: %v", err)
}
fmt.Printf("Topic %s was created.\n", topic.Name)
}
func deleteTopic(service *pubsub.Service, argv []string) {
checkArgs(argv, 3)
topicName := fullTopicName(argv[0], argv[2])
if _, err := service.Projects.Topics.Delete(topicName).Do(); err != nil {
log.Fatalf("deleteTopic Delete().Do() failed: %v", err)
}
fmt.Printf("Topic %s was deleted.\n", topicName)
}
func listSubscriptions(service *pubsub.Service, argv []string) {
next := ""
for {
subscriptionsList, err := service.Projects.Subscriptions.List(fmt.Sprintf("projects/%s", argv[0])).PageToken(next).Do()
if err != nil {
log.Fatalf("listSubscriptions query.Do() failed: %v", err)
}
for _, subscription := range subscriptionsList.Subscriptions {
sub_text, _ := json.MarshalIndent(subscription, "", " ")
fmt.Printf("%s\n", sub_text)
}
next = subscriptionsList.NextPageToken
if next == "" {
break
}
}
}
func createSubscription(service *pubsub.Service, argv []string) {
checkArgs(argv, 4)
name := fullSubName(argv[0], argv[2])
sub := &pubsub.Subscription{Topic: fullTopicName(argv[0], argv[3])}
subscription, err := service.Projects.Subscriptions.Create(name, sub).Do()
if err != nil {
log.Fatalf("createSubscription Create().Do() failed: %v", err)
}
fmt.Printf("Subscription %s was created.\n", subscription.Name)
}
func deleteSubscription(service *pubsub.Service, argv []string) {
checkArgs(argv, 3)
name := fullSubName(argv[0], argv[2])
if _, err := service.Projects.Subscriptions.Delete(name).Do(); err != nil {
log.Fatalf("deleteSubscription Delete().Do() failed: %v", err)
}
fmt.Printf("Subscription %s was deleted.\n", name)
}
func connectIRC(service *pubsub.Service, argv []string) {
checkArgs(argv, 5)
topicName := fullTopicName(argv[0], argv[2])
server := argv[3]
channel := argv[4]
nick := fmt.Sprintf("bot-%s", argv[2])
ircbot := NewIRCBot(server, channel, nick)
ircbot.Connect()
defer ircbot.Close()
ircbot.CheckConnection()
privMark := fmt.Sprintf("PRIVMSG %s :", ircbot.channel)
for {
line, err := ircbot.ReadLine()
if err != nil {
log.Fatal("Unable to read a line from the connection.")
}
parts := strings.Split(line, " ")
if len(parts) > 0 && parts[0] == "PING" {
ircbot.Sendf("PONG %s\r\n", parts[1])
} else {
pos := strings.Index(line, privMark)
if pos == -1 {
continue
}
privMsg := line[pos+len(privMark) : len(line)]
pubsubMessage := &pubsub.PubsubMessage{
Data: base64.StdEncoding.EncodeToString([]byte(privMsg)),
}
publishRequest := &pubsub.PublishRequest{
Messages: []*pubsub.PubsubMessage{pubsubMessage},
}
if _, err := service.Projects.Topics.Publish(topicName, publishRequest).Do(); err != nil {
log.Fatalf("connectIRC Publish().Do() failed: %v", err)
}
log.Println("Published a message to the topic.")
}
}
}
func pullMessages(service *pubsub.Service, argv []string) {
checkArgs(argv, 3)
subName := fullSubName(argv[0], argv[2])
pullRequest := &pubsub.PullRequest{
ReturnImmediately: false,
MaxMessages: 1,
}
for {
pullResponse, err := service.Projects.Subscriptions.Pull(subName, pullRequest).Do()
if err != nil {
log.Fatalf("pullMessages Pull().Do() failed: %v", err)
}
for _, receivedMessage := range pullResponse.ReceivedMessages {
data, err := base64.StdEncoding.DecodeString(receivedMessage.Message.Data)
if err != nil {
log.Fatalf("pullMessages DecodeString() failed: %v", err)
}
fmt.Printf("%s\n", data)
ackRequest := &pubsub.AcknowledgeRequest{
AckIds: []string{receivedMessage.AckId},
}
if _, err = service.Projects.Subscriptions.Acknowledge(subName, ackRequest).Do(); err != nil {
log.Printf("pullMessages Acknowledge().Do() failed: %v", err)
}
}
}
}
// This example demonstrates calling the Cloud Pub/Sub API. As of 20
// Aug 2014, the Cloud Pub/Sub API is only available if you're
// whitelisted. If you're interested in using it, please apply for the
// Limited Preview program at the following form:
// http://goo.gl/Wql9HL
//
// Also, before running this example, be sure to enable Cloud Pub/Sub
// service on your project in Developer Console at:
// https://console.developers.google.com/
//
// It has 8 subcommands as follows:
//
// <project_id> list_topics
// <project_id> create_topic <topic>
// <project_id> delete_topic <topic>
// <project_id> list_subscriptions
// <project_id> create_subscription <subscription> <linked topic>
// <project_id> delete_subscription <subscription>
// <project_id> connect_irc <topic> <server> <channel>
// <project_id> pull_messages <subscription>
//
// You can use either of your alphanumerical or numerial Cloud Project
// ID for project_id. You can choose any names for topic and
// subscription as long as they follow the naming rule described at:
// https://developers.google.com/pubsub/overview#names
//
// You can list/create/delete topics/subscriptions by self-explanatory
// subcommands, as well as connect to an IRC channel and publish
// messages from the IRC channel to a specified Cloud Pub/Sub topic by
// the "connect_irc" subcommand, or continuously pull messages from a
// specified Cloud Pub/Sub subscription and display the data by the
// "pull_messages" subcommand.
func pubsubMain(client *http.Client, argv []string) {
checkArgs(argv, 2)
service, err := pubsub.New(client)
if err != nil {
log.Fatalf("Unable to create PubSub service: %v", err)
}
m := map[string]func(service *pubsub.Service, argv []string){
"list_topics": listTopics,
"create_topic": createTopic,
"delete_topic": deleteTopic,
"list_subscriptions": listSubscriptions,
"create_subscription": createSubscription,
"delete_subscription": deleteSubscription,
"connect_irc": connectIRC,
"pull_messages": pullMessages,
}
f, ok := m[argv[1]]
if !ok {
pubsubUsage()
os.Exit(2)
}
f(service, argv)
}