Esempio n. 1
0
func (conn *GCMConnector) loadSubscriptions() {
	subscriptions := conn.kvStore.Iterate(registrationsSchema, "")
	count := 0
	for {
		select {
		case entry, ok := <-subscriptions:
			if !ok {
				log.WithFields(log.Fields{
					"module":                 "GCM",
					"gcm_subscription_count": count,
				}).Info("Renewed ")
				return
			}
			gcmID := entry[0]
			splitValue := strings.SplitN(entry[1], ":", 2)
			userID := splitValue[0]
			topic := splitValue[1]

			log.WithFields(log.Fields{
				"module": "GCM",
				"userID": userID,
				"gcmId":  gcmID,
				"topic":  string(topic),
			}).Debug("renewing GCM subscription")

			route := server.NewRoute(topic, gcmID, userID, conn.routerC)
			conn.router.Subscribe(route)
			count++
		}
	}
}
Esempio n. 2
0
func (gcmConnector *GCMConnector) subscribe(topic string, userid string, gcmid string) {
	guble.Info("gcm connector registration to userid=%q, gcmid=%q: %q", userid, gcmid, topic)

	route := server.NewRoute(topic, gcmConnector.channelFromRouter, gcmid, userid)

	gcmConnector.router.Subscribe(route)
	gcmConnector.saveSubscription(userid, topic, gcmid)
}
Esempio n. 3
0
func (rec *Receiver) subscribe() {
	routeC := make(chan *server.MessageForRoute, 3)
	rec.route = server.NewRoute(string(rec.path), rec.applicationId, rec.userId, routeC)
	_, err := rec.router.Subscribe(rec.route)
	if err != nil {
		rec.sendError(protocol.ERROR_SUBSCRIBED_TO, string(rec.path), err.Error())
	} else {
		rec.sendOK(protocol.SUCCESS_SUBSCRIBED_TO, string(rec.path))
	}
}
Esempio n. 4
0
func (conn *GCMConnector) subscribe(topic string, userID string, gcmID string) {

	log.WithFields(log.Fields{
		"module": "GCM",
		"userID": userID,
		"gcmId":  gcmID,
		"topic":  string(topic),
	}).Info("GCM connector registration to")

	route := server.NewRoute(topic, gcmID, userID, conn.routerC)

	conn.router.Subscribe(route)
	conn.saveSubscription(userID, topic, gcmID)
}
Esempio n. 5
0
// Start opens the connector, creates more goroutines / workers to handle messages coming from the router
func (conn *GCMConnector) Start() error {
	broadcastRoute := server.NewRoute(
		conn.broadcastPath,
		"gcm_connector",
		"gcm_connector",
		conn.routerC,
	)
	conn.router.Subscribe(broadcastRoute)
	go func() {
		//TODO Cosmin: should loadSubscriptions() be taken out of this goroutine, and executed before ?
		// (even if startup-time is longer, the routes are guaranteed to be there right after Start() returns)
		conn.loadSubscriptions()

		conn.wg.Add(conn.nWorkers)
		for id := 1; id <= conn.nWorkers; id++ {
			go conn.loopSendOrBroadcastMessage(id)
		}
	}()
	return nil
}
Esempio n. 6
0
func (gcmConnector *GCMConnector) Start() error {
	broadcastRoute := server.NewRoute(removeTrailingSlash(gcmConnector.prefix)+"/broadcast", gcmConnector.channelFromRouter, "gcm_connector", "gcm_connector")
	gcmConnector.router.Subscribe(broadcastRoute)
	go func() {
		gcmConnector.loadSubscriptions()

		for {
			select {
			case msg := <-gcmConnector.channelFromRouter:
				if string(msg.Message.Path) == removeTrailingSlash(gcmConnector.prefix)+"/broadcast" {
					go gcmConnector.broadcastMessage(msg)
				} else {
					go gcmConnector.sendMessageToGCM(msg)
				}
			case <-gcmConnector.stopChan:
				return
			}
		}
	}()
	return nil
}
Esempio n. 7
0
func (gcmConnector *GCMConnector) loadSubscriptions() {
	subscriptions := gcmConnector.kvStore.Iterate(GCM_REGISTRATIONS_SCHEMA, "")
	count := 0
	for {
		select {
		case entry, ok := <-subscriptions:
			if !ok {
				guble.Info("renewed %v gcm subscriptions", count)
				return
			}
			gcmId := entry[0]
			splitedValue := strings.SplitN(entry[1], ":", 2)
			userid := splitedValue[0]
			topic := splitedValue[1]

			guble.Debug("renew gcm subscription: user=%v, topic=%v, gcmid=%v", userid, topic, gcmId)
			route := server.NewRoute(topic, gcmConnector.channelFromRouter, gcmId, userid)
			gcmConnector.router.Subscribe(route)
			count++
		}
	}
}