Beispiel #1
0
func TestQue(t *testing.T) {
	_, e := toolkit.CallResult(b.Address+"/broadcaster/que", "POST",
		toolkit.M{}.Set("userid", userid).Set("secret", userSecret).Set("key", "Ch01:QueMessage01").Set("data", "Ini adalah Channel 01 Que Message 01").ToBytes("json", nil))

	if e != nil {
		t.Errorf("Sending Que Error: " + e.Error())
		return
	}

	time.Sleep(3 * time.Second)
	found := 0
	for _, s := range subs {
		_, e = toolkit.CallResult(s.Address+"/subscriber/collectmessage", "POST",
			toolkit.M{}.Set("subscriber", "http://localhost:12345").Set("secret", s.Secret).Set("key", "").ToBytes("json", nil))
		if e == nil {
			found++
		} else {
			t.Logf("Node %s, could not collect message: %s", s.Address, e.Error())
		}
	}

	if found != 1 {
		t.Errorf("Que not collected properly. Should only 1 subscriber collecting it, got %d", found)
	}
}
Beispiel #2
0
func (s *Subscriber) Start(address, broadcaster, broadcastServerSecret string) error {
	if string(broadcaster[len(broadcaster)-1]) != "/" {
		broadcaster += "/broadcaster/"
	}
	s.Broadcaster = broadcaster
	s.Address = address
	s.messages = map[string]*Message{}

	//--- get confirmation from Broadcaster first
	r, e := toolkit.CallResult(broadcaster+"addnode", "POST",
		toolkit.M{}.Set("subscriber", s.Address).Set("secret", broadcastServerSecret).ToBytes("json", nil))
	if e != nil {
		return e
	} else {
		//fmt.Printf("R: %v \n", r)
		s.Secret = r.Data.(string)
	}

	app := knot.NewApp("pakpos")
	app.DefaultOutputType = knot.OutputJson
	app.Register(s)
	go func() {
		knot.StartApp(app, address)
	}()
	return nil
}
Beispiel #3
0
func (b *Broadcaster) RemoveNode(k *knot.WebContext) interface{} {
	var nodeModel struct {
		Subscriber string
		Secret     string
	}
	k.GetPayload(&nodeModel)

	result := toolkit.NewResult()
	if nodeModel.Subscriber == "" {
		result.SetErrorTxt("Invalid subscriber info " + nodeModel.Subscriber)
		return result
	}

	if b.validateToken("node", nodeModel.Secret, nodeModel.Subscriber) == false {
		result.SetErrorTxt("Not authorised")
		return result
	}

	sub := b.Subscibers[nodeModel.Subscriber]
	_, e := toolkit.CallResult(sub.Url("subscriber/stop"), "POST", toolkit.M{}.Set("secret", sub.Secret).ToBytes("json", nil))
	if e != nil {
		result.SetErrorTxt(e.Error())
		return result
	}

	delete(b.Subscibers, nodeModel.Subscriber)
	k.Server.Log().Info(fmt.Sprintf("%s has been removed as subscriber. Current active subscibers: %d", nodeModel.Subscriber, len(b.Subscibers)))
	return result
}
Beispiel #4
0
func (s *Session) CallResult(method string, data []byte) *toolkit.Result {
	weburl := s.URL + "/master/" + method
	result, e := toolkit.CallResult(weburl, "POST", data)
	if e != nil {
		result.Data = nil
		return result.SetError(e)
	}
	return result
}
Beispiel #5
0
func TestQueInvalid(t *testing.T) {
	_, e := toolkit.CallResult(b.Address+"/broadcaster/que", "POST",
		toolkit.M{}.Set("userid", userid).Set("secret", userSecret).Set("key", "Ch01:QueMessage02").Set("data", "Ini adalah Channel 02 Que Message 02").ToBytes("json", nil))

	if e != nil {
		t.Errorf("Sending Que Error: " + e.Error())
		return
	}

	time.Sleep(15 * time.Second)
}
Beispiel #6
0
func Login(url, userid, password string) (*Session, error) {
	//return nil, errors.New("Login is not yet activated")
	if userid != "arief" || password != "darmawan" {
		return nil, errors.New("Authorisation failed")
	}

	loginUrl := MakeUrl(url, "master", "login")
	result, e := toolkit.CallResult(loginUrl, "POST", toolkit.M{}.Set("userid", userid).Set("password", password).ToBytes("json", nil))
	if e != nil {
		return nil, e
	}
	sess := &Session{UserID: userid, Secret: result.Data.(string)}
	sess.URL = url
	return sess, nil
}
Beispiel #7
0
func (s *Subscriber) ReceiveMessage(messageType string, key string, message interface{}) error {
	if messageType == "Que" {
		r, e := toolkit.CallResult(s.Broadcaster+"getmessage", "POST",
			toolkit.M{}.Set("subscriber", s.Address).Set("secret", s.Secret).Set("key", key).ToBytes("", nil))
		if e != nil {
			return e
		}
		message = r.Data
		delete(s.messages, key)
		s.Server.Log().Info(fmt.Sprintf("Message %s has been received by %s: %v", key, s.Address, message))
		return nil
	} else {
		s.Server.Log().Info(fmt.Sprintf("Message %s has been received by %s: %v", key, s.Address, message))
		return nil
	}
}
Beispiel #8
0
func (m *MessageMonitor) distributeQue() {
	//--- inform all targets that new message has been created
	wg := new(sync.WaitGroup)

	targetCount := len(m.Targets)
	var newtargets []string
	var failtargets []string
	for _, t := range m.Targets {
		wg.Add(1)
		go func(wg *sync.WaitGroup, t string) {
			defer wg.Done()
			sub, exist := m.broadcaster.Subscibers[t]
			if !exist {
				m.broadcaster.Log().Warning(fmt.Sprintf(
					"Unable to send new que to %s for key %s. %s",
					t, m.Key, "Subscriber not found"))
				failtargets = append(failtargets, t)
			}
			url := sub.Url("subscriber/newkey")
			msg := toolkit.M{}.Set("key", m.Key).Set("secret", sub.Secret).Set("expiry", m.Expiry).ToBytes("json", nil)
			_, e := toolkit.CallResult(url, "POST", msg)
			if e != nil {
				m.broadcaster.Log().Warning(fmt.Sprintf(
					"Unable to send new que to %s for key %s. %s",
					url, m.Key, e.Error()))
				failtargets = append(failtargets, t)
			} else {
				newtargets = append(newtargets, t)
			}
		}(wg, t)
	}
	wg.Wait()
	m.Targets = newtargets
	if len(newtargets) == 0 {
		m.broadcaster.Log().Error("No target is accepting key " + m.Key)
		return
	}
	m.broadcaster.Log().Info(fmt.Sprintf("Ping %d servers for new message %s. Succcess: %d Fail: %d", targetCount, m.Key, len(newtargets), len(failtargets)))

	//-- loop while not all target complete receival or expire
	for len(m.Targets) != m.Success && time.Now().After(m.Expiry) == false {
		time.Sleep(1 * time.Millisecond)
	}
}
Beispiel #9
0
func TestSubcribeChannel(t *testing.T) {
	_, e := toolkit.CallResult(b.Address+"/broadcaster/subscribechannel", "POST",
		toolkit.M{}.Set("Subscriber", subs[1].Address).Set("Secret", subs[1].Secret).Set("Channel", "Ch01").ToBytes("json", nil))
	if e != nil {
		t.Error(e)
		return
	}

	_, e = call(b.Address+"/broadcaster/broadcast", "POST",
		toolkit.M{}.
			Set("userid", userid).
			Set("secret", userSecret).
			Set("key", "Ch01:Message01").
			Set("data", "Ini adalah Channel 01 - Broadcast Message 01").
			ToBytes("json", nil),
		200)
	if e != nil {
		t.Errorf("%s %s", b.Address+"/broadcaster/broadcast", e.Error())
		return
	}
	time.Sleep(2 * time.Second)

}
Beispiel #10
0
func (s *Session) Logout() {
	url := MakeUrl(s.URL, "master", "logout")
	toolkit.CallResult(url, "POST", toolkit.ToBytes(s, "json"))
}