func SetUpChannelGroup() {
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	done := make(chan bool)

	pubnub := messaging.NewPubnub(config.Keys.Pub, config.Keys.Sub, "", "",
		false, "")

	pubnub.SetAuthenticationKey(bootstrapAuth)

	// Remove Group
	go pubnub.ChannelGroupRemoveGroup(config.StocksChannelGroup,
		successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done

	// Create it from the scratch
	go pubnub.ChannelGroupAddChannel(config.StocksChannelGroup, stockNames,
		successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done
}
func GrantPermissions() {
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	done := make(chan bool)

	pubnub := messaging.NewPubnub(config.Keys.Pub, config.Keys.Sub,
		config.Keys.Secret, "", false, "")

	pubnub.SetAuthenticationKey(bootstrapAuth)

	// Allow current Pubnub instance to managet the channel group
	go pubnub.GrantChannelGroup(config.StocksChannelGroup,
		false, true, config.GrantTTL,
		bootstrapAuth, successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done

	// Allow unauthorized users to subscribe to stockblast channel group
	go pubnub.GrantChannelGroup(config.StocksChannelGroup,
		true, false, config.GrantTTL,
		"", successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done

	// Unauthorized users can both read and write on chat channel
	go pubnub.GrantSubscribe(config.ChatChannel, true, true, config.GrantTTL, "",
		successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done

	// Unauthorized users can only read history
	go pubnub.GrantSubscribe(config.HistoryChannel,
		true, false, config.GrantTTL, "", successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done

	// Allow stock tickers authorized by auths.Auth key to publish to stock
	// channels
	go pubnub.GrantSubscribe(stockNames, false, true, config.GrantTTL,
		config.Keys.Auth, successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	<-done
}
Exemple #3
0
// publishRoutine asks the user the message to send to the pubnub channel(s) and
// calls the Publish routine of the messaging package as a parallel
// process. If we have multiple pubnub channels then this method will spilt the
// channel by comma and send the message on all the pubnub channels.
func publishRoutine(channels string, message string) {
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	fmt.Println("Publishing message: ", message)
	go pub.Publish(channels, message, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Publish")
}
Exemple #4
0
// UnsubscribePresenceRoutine calls the PresenceUnsubscribe routine of the messaging package as a parallel
// process. All the channels in the _connectChannels string will be unsubscribed.
func unsubscribePresenceRoutine(channels string) {
	var errorChannel = make(chan []byte)
	channel := make(chan []byte)
	channelArray := strings.Split(channels, ",")
	go pub.PresenceUnsubscribe(channels, channel, errorChannel)
	go handleUnsubscribeResult(channel, errorChannel, messaging.GetNonSubscribeTimeout(), "UnsubscribePresence", len(channelArray)*2)
}
Exemple #5
0
func grantSubscribe(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	ch := q.Get("ch")
	read := q.Get("r")
	write := q.Get("w")
	ttl := q.Get("ttl")
	bRead := false
	if read == "1" {
		bRead = true
	}
	bWrite := false
	if write == "1" {
		bWrite = true
	}
	iTTL := 1440
	if ival, err := strconv.Atoi(ttl); err == nil {
		iTTL = ival
	}

	uuid := q.Get("uuid")
	pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	go pubInstance.GrantSubscribe(ch, bRead, bWrite, iTTL, "", successChannel, errorChannel)
	handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Revoke Subscribe")
}
Exemple #6
0
// WhereNowRoutine
func whereNowRoutine(uuid string) {
	fmt.Println("WhereNow ", uuid)
	var errorChannel = make(chan []byte)
	successChannel := make(chan []byte)
	go pub.WhereNow(uuid, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "WhereNow")
}
Exemple #7
0
func globalHereNowRoutine(showUuid bool, includeUserState bool) {
	fmt.Println("Global here now ", uuid)
	var errorChannel = make(chan []byte)
	successChannel := make(chan []byte)
	go pub.GlobalHereNow(showUuid, includeUserState, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Global Here Now")
}
Exemple #8
0
// detailedHistoryRoutine calls the History routine of the messaging package as a parallel
// process. If we have multiple pubnub channels then this method will spilt the _connectChannels
// by comma and send the message on all the pubnub channels.
func detailedHistoryRoutine(channels string, startTime int64) {
	errorChannel := make(chan []byte)
	channel := make(chan []byte)
	fmt.Println(fmt.Sprintf("Page Size :%d", pageSize))
	go pub.History(channels, pageSize, startTime, 0, false, channel, errorChannel)
	go handleDetailedHistoryResult(channel, errorChannel, messaging.GetNonSubscribeTimeout(), "Detailed History")
}
Exemple #9
0
func addChannelToChannelGroupRoutine(group, channels string) {
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	go pub.ChannelGroupAddChannel(group, channels, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), "Channel Group Add Channel")
}
Exemple #10
0
func removeChannelGroupRoutine(group string) {
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	go pub.ChannelGroupRemoveGroup(group, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), "Channel Group Remove")
}
Exemple #11
0
func pamAuditChannelGroupRoutine(groups, auth string) {
	var errorChannel = make(chan []byte)
	var pamChannel = make(chan []byte)

	go pub.AuditChannelGroup(groups, auth, pamChannel, errorChannel)
	go handleResult(pamChannel, errorChannel, messaging.GetNonSubscribeTimeout(),
		"Channel Group Audit")
}
Exemple #12
0
func pamGrantChannelGroupRoutine(groups, auth string,
	read, manage bool, ttl int) {
	var errorChannel = make(chan []byte)
	var pamChannel = make(chan []byte)

	go pub.GrantChannelGroup(groups, read, manage, ttl, auth,
		pamChannel, errorChannel)
	go handleResult(pamChannel, errorChannel, messaging.GetNonSubscribeTimeout(),
		"Channel Group Grant")
}
Exemple #13
0
// pamAuditRoutine calls the AuditPresence or AuditSubscribe routine of the messaging package
// as a parallel process.
func pamAuditRoutine(channels string, isPresence bool) {
	var errorChannel = make(chan []byte)
	var pamChannel = make(chan []byte)
	if isPresence {
		go pub.AuditPresence(channels, "", pamChannel, errorChannel)
	} else {
		go pub.AuditSubscribe(channels, "", pamChannel, errorChannel)
	}
	go handleResult(pamChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Audit")
}
Exemple #14
0
func revokeSubscribe(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	ch := q.Get("ch")
	uuid := q.Get("uuid")
	pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	go pubInstance.GrantSubscribe(ch, false, false, 0, "", successChannel, errorChannel)
	handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Revoke Subscribe")
}
Exemple #15
0
func getTime(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	uuid := q.Get("uuid")

	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
	go pubInstance.GetTime(successChannel, errorChannel)
	handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Time")
}
Exemple #16
0
// PublishRoutine asks the user the message to send to the pubnub channel(s) and
// calls the Publish routine of the messaging package as a parallel
// process. If we have multiple pubnub channels then this method will spilt the
// channel by comma and send the message on all the pubnub channels.
func publishRoutine(channels string, message string) {
	var errorChannel = make(chan []byte)
	channelArray := strings.Split(channels, ",")

	for i := 0; i < len(channelArray); i++ {
		ch := strings.TrimSpace(channelArray[i])
		fmt.Println("Publish to channel: ", ch)
		channel := make(chan []byte)
		go pub.Publish(ch, message, channel, errorChannel)
		go handleResult(channel, errorChannel, messaging.GetNonSubscribeTimeout(), "Publish")
	}
}
Exemple #17
0
// HereNowRoutine calls the HereNow routine of the messaging package as a parallel
// process. If we have multiple pubnub channels then this method will spilt the _connectChannels
// by comma and send the message on all the pubnub channels.
func hereNowRoutine(channels string, showUuid bool, includeUserState bool) {
	var errorChannel = make(chan []byte)
	channelArray := strings.Split(channels, ",")
	for i := 0; i < len(channelArray); i++ {
		channel := make(chan []byte)
		ch := strings.TrimSpace(channelArray[i])
		fmt.Println("HereNow for channel: ", ch)

		go pub.HereNow(ch, showUuid, includeUserState, channel, errorChannel)
		go handleResult(channel, errorChannel, messaging.GetNonSubscribeTimeout(), "HereNow")
	}
}
Exemple #18
0
func setUserStateJSON(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	ch := q.Get("ch")
	j := q.Get("j")
	uuid := q.Get("uuid")
	pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	go pubInstance.SetUserStateJSON(ch, j, successChannel, errorChannel)
	handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Set User State JSON")
}
Exemple #19
0
// DetailedHistoryRoutine calls the History routine of the messaging package as a parallel
// process. If we have multiple pubnub channels then this method will spilt the _connectChannels
// by comma and send the message on all the pubnub channels.
func detailedHistoryRoutine(channels string) {
	var errorChannel = make(chan []byte)
	channelArray := strings.Split(channels, ",")
	for i := 0; i < len(channelArray); i++ {
		ch := strings.TrimSpace(channelArray[i])
		fmt.Println("DetailedHistory for channel: ", ch)

		channel := make(chan []byte)

		//go _pub.History(ch, 100, 13662867154115803, 13662867243518473, false, channel)
		go pub.History(ch, 100, 0, 0, false, channel, errorChannel)
		go handleResult(channel, errorChannel, messaging.GetNonSubscribeTimeout(), "Detailed History")
	}
}
Exemple #20
0
func detailedHistory(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	ch := q.Get("ch")
	uuid := q.Get("uuid")
	start := q.Get("start")
	var iStart int64
	if strings.TrimSpace(start) != "" {
		bi := big.NewInt(0)
		if _, ok := bi.SetString(start, 10); !ok {
			iStart = 0
		} else {
			iStart = bi.Int64()
		}
	}

	end := q.Get("end")
	var iEnd int64
	if strings.TrimSpace(end) != "" {
		bi := big.NewInt(0)
		if _, ok := bi.SetString(end, 10); !ok {
			iEnd = 0
		} else {
			iEnd = bi.Int64()
		}
	}

	limit := q.Get("limit")
	reverse := q.Get("reverse")

	iLimit := 100
	if ival, err := strconv.Atoi(limit); err == nil {
		iLimit = ival
	}

	bReverse := false
	if reverse == "1" {
		bReverse = true
	}

	pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	go pubInstance.History(ch, iLimit, iStart, iEnd, bReverse, false, successChannel, errorChannel)
	handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Detailed History")
}
Exemple #21
0
func globalHereNow(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	uuid := q.Get("uuid")
	globalHereNowShowUUID := q.Get("showUUID")
	globalHereNowIncludeUserState := q.Get("includeUserState")
	disableUUID := false
	includeUserState := false
	if globalHereNowShowUUID == "1" {
		disableUUID = true
	}
	if globalHereNowIncludeUserState == "1" {
		includeUserState = true
	}
	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)

	pubInstance := messaging.NewPubnub(publishKey, subscribeKey, secretKey, "", true, uuid)
	go pubInstance.GlobalHereNow(disableUUID, includeUserState, successChannel, errorChannel)
	handleResult(w, r, uuid, successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Global Here Now")
}
// PublishMessages calls the publish method of messaging package numberOfMessages times
// and appends the count with the message to distinguish from the others.
//
// Parameters:
// pubnubInstance: a reference of *messaging.Pubnub,
// channel: the pubnub channel to publish the messages,
// t: a reference to *testing.T,
// startMessagesFrom: the message identifer,
// numberOfMessages: number of messages to send,
// message: message to send.
//
// returns a bool if the publish of all messages is successful.
func PublishMessages(pubnubInstance *messaging.Pubnub, channel string, t *testing.T, startMessagesFrom int, numberOfMessages int, message string) bool {
	messagesReceived := 0
	messageToSend := ""
	tOut := messaging.GetNonSubscribeTimeout()
	messaging.SetNonSubscribeTimeout(30)
	for i := startMessagesFrom; i < startMessagesFrom+numberOfMessages; i++ {
		messageToSend = message + strconv.Itoa(i)

		returnPublishChannel := make(chan []byte)
		errorChannel := make(chan []byte)
		go pubnubInstance.Publish(channel, messageToSend, returnPublishChannel, errorChannel)
		messagesReceived++
		//time.Sleep(500 * time.Millisecond)
		time.Sleep(1500 * time.Millisecond)
	}
	if messagesReceived == numberOfMessages {
		return true
	}
	messaging.SetNonSubscribeTimeout(tOut)
	return false
}
func (stock *Stock) UpdateValuesAndPublish(pubnub *messaging.Pubnub,
	cycle chan bool) {
	if stock.CurrentPrice == 0 {
		stock.CurrentPrice = stock.InitialPrice
	}

	rand.Seed(int64(time.Now().Nanosecond()))

	change := float64(rand.Intn(stock.Volatility)-stock.Volatility/2) / 100
	stock.CurrentPrice = stock.CurrentPrice + float64(change)
	delta := stock.CurrentPrice - stock.InitialPrice
	percentage := Roundn((1-stock.InitialPrice/stock.CurrentPrice)*100, 2)
	vol := Randn(stock.Volatility, 1000) * 10

	streamMessage := StreamMessage{
		Time:       time.Now().Format(TIME_FORMAT),
		Price:      fmt.Sprintf("%.2f", stock.CurrentPrice),
		Delta:      fmt.Sprintf("%.2f", delta),
		Percentage: fmt.Sprintf("%.2f", percentage),
		Vol:        vol}

	if math.Abs(percentage) > float64(stock.MaxDelta)/100 {
		stock.CurrentPrice = stock.InitialPrice
	}

	errorChannel := make(chan []byte)
	successChannel := make(chan []byte)
	done := make(chan bool)

	go pubnub.Publish(stock.Name, streamMessage, successChannel, errorChannel)
	go handleResponse(successChannel, errorChannel,
		messaging.GetNonSubscribeTimeout(), done)

	sleep := Randn(stock.MinTrade, stock.MaxTrade)
	time.Sleep(time.Duration(sleep) * time.Microsecond)

	cycle <- <-done
}
Exemple #24
0
// PublishMessages calls the publish method of messaging package numberOfMessages times
// and appends the count with the message to distinguish from the others.
//
// Parameters:
// pubnubInstance: a reference of *messaging.Pubnub,
// channel: the pubnub channel to publish the messages,
// t: a reference to *testing.T,
// startMessagesFrom: the message identifer,
// numberOfMessages: number of messages to send,
// message: message to send.
//
// returns a bool if the publish of all messages is successful.
func PublishMessages(pubnubInstance *messaging.Pubnub, channel string,
	t *testing.T, startMessagesFrom int, numberOfMessages int,
	message string, sleep func(int)) bool {

	assert := assert.New(t)
	messagesReceived := 0
	messageToSend := ""
	tOut := messaging.GetNonSubscribeTimeout()
	messaging.SetNonSubscribeTimeout(30)

	successChannel := make(chan []byte)
	errorChannel := make(chan []byte)

	for i := startMessagesFrom; i < startMessagesFrom+numberOfMessages; i++ {
		messageToSend = message + strconv.Itoa(i)

		go pubnubInstance.Publish(channel, messageToSend, successChannel, errorChannel)
		select {
		case <-successChannel:
			messagesReceived++
		case err := <-errorChannel:
			assert.Fail("Failed to get channel list", string(err))
		case <-messaging.Timeout():
			assert.Fail("WhereNow timeout")
		}
	}

	sleep(3)
	if messagesReceived == numberOfMessages {
		return true
	}

	messaging.SetNonSubscribeTimeout(tOut)

	return false
}
Exemple #25
0
// pamPresenceRoutine calls the GrantPresence routine of the messaging package
// as a parallel process. This is used to grant or revoke the R, W permissions
// to revoke set read and write false and ttl as -1
func pamPresenceRoutine(channels string, read bool, write bool, ttl int) {
	var errorChannel = make(chan []byte)
	var pamChannel = make(chan []byte)
	go pub.GrantPresence(channels, read, write, ttl, "", pamChannel, errorChannel)
	go handleResult(pamChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Presence Grant")
}
Exemple #26
0
// TimeRoutine calls the GetTime routine of the messaging package as a parallel
// process.
func timeRoutine() {
	var errorChannel = make(chan []byte)
	channel := make(chan []byte)
	go pub.GetTime(channel, errorChannel)
	go handleResult(channel, errorChannel, messaging.GetNonSubscribeTimeout(), "Time")
}
Exemple #27
0
func setUserStateJSON(channel string, jsonString string) {
	var errorChannel = make(chan []byte)
	var successChannel = make(chan []byte)
	go pub.SetUserStateJSON(channel, jsonString, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Set User State JSON")
}
Exemple #28
0
func getUserState(channel string) {
	var errorChannel = make(chan []byte)
	var successChannel = make(chan []byte)
	go pub.GetUserState(channel, successChannel, errorChannel)
	go handleResult(successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Get User State")
}
Exemple #29
0
func delUserState(channel string, key string) {
	var errorChannel = make(chan []byte)
	var successChannel = make(chan []byte)
	go pub.SetUserStateKeyVal(channel, key, "", successChannel, errorChannel)
	go handleResult(successChannel, errorChannel, messaging.GetNonSubscribeTimeout(), "Del User State")
}