Exemplo n.º 1
0
func (c *Controller) KendoGridData(obj orm.IModel, objs interface{}, ins toolkit.M) *toolkit.Result {
	result := toolkit.NewResult()
	s := c.KendoGridSettings(ins)
	//fmt.Printf("Settings: %s \n", toolkit.JsonString(s))
	cursor := c.Orm.Find(obj, s)
	e = cursor.FetchAll(objs, true)

	s.Unset("limit")
	s.Unset("skip")
	cursor = c.Orm.Find(obj, s)
	defer cursor.Close()
	count := cursor.Count()

	if e != nil {
		result.Status = toolkit.Status_NOK
		result.Message = e.Error()
	} else {
		//result.Data = toolkit.M{"Data": dsUsers.Data,
		result.Data = toolkit.M{"Data": objs,
			"Count": count}
		result.Status = toolkit.Status_OK
	}

	return result
}
Exemplo n.º 2
0
func (c *Coordinator) RequestJoin(in toolkit.M) *toolkit.Result {
	var e error
	r := toolkit.NewResult()
	referenceID := in.GetString("auth_referenceid")
	//toolkit.Println("User ID Request Join : " + referenceID)
	secret := in.GetString("auth_secret")
	nodeid := in.GetString("nodeid")
	noderole := NodeRoleEnum(in.GetString("noderole"))

	//--- init  node
	node := new(Node)
	node.ID = nodeid
	node.Role = noderole
	node.UserID = referenceID
	node.Secret = secret
	//node.InitRPC()
	e = c.AddNode(node)
	if e != nil {
		r.SetErrorTxt(e.Error())
	}
	//fmt.Printf("Nodes now:\n%s\n", toolkit.JsonString(c.nodes))

	r.Data = node.Secret
	return r
}
Exemplo n.º 3
0
/*
Write Write bytes of data into sebar storage.
- Data need to be defined as []byte on in["data"]
- To use memory or disk should be defined on in["storage"] as: MEM, DSK (sebar.StorageTypeMemory, sebar.StorageTypeMemory)
- If no in["storage"] or the value is not eq to either disk or memory, it will be defaulted to memory
*/
func (s *Storage) Write(in toolkit.M) *toolkit.Result {
	r := toolkit.NewResult()
	key := in.Get("key").(string)
	storage := StorageTypeEnum(in.GetString("storage"))
	if storage != StorageTypeMemory && storage != StorageTypeDisk {
		storage = StorageTypeMemory
	}
	dataToWrite := in.Get("data").([]byte)
	dataLen := len(dataToWrite)

	// Validation
	nodeCoordinator := s.NodeByID(s.Coordinator)
	if nodeCoordinator == nil {
		return r.SetErrorTxt(s.Address + " no Coordinator has been setup")
	}

	// Since all is ok commit the change
	var ms *StorageMedia
	if storage == StorageTypeMemory {
		ms = s.MemoryStorage
	} else {
		ms = s.DiskStorage
	}
	ms.write(key, dataToWrite, nodeCoordinator)
	s.Log.Info(toolkit.Sprintf("Writing %s (%s) to node %s", key, ParseSize(float64(dataLen)), s.Address))
	return r
}
Exemplo n.º 4
0
func (b *Broadcaster) SubscribeChannel(k *knot.WebContext) interface{} {
	result := toolkit.NewResult()
	var model struct {
		Subscriber, Secret, Channel string
	}
	k.GetPayload(&model)
	if b.validateToken("node", model.Secret, model.Subscriber) == false {
		result.SetErrorTxt("Subscriber not authorised")
		return result
	}
	model.Channel = strings.ToLower(model.Channel)
	subs, exist := b.channelSubscribers[model.Channel]
	if !exist {
		subs = []string{}
	}
	for _, s := range subs {
		if s == model.Subscriber {
			result.SetErrorTxt(fmt.Sprintf("%s has been subscribe to channel %s", model.Subscriber, model.Channel))
			return result
		}
	}
	subs = append(subs, model.Subscriber)
	b.channelSubscribers[model.Channel] = subs
	b.Server.Log().Info(fmt.Sprintf("Node %s successfully subscibed to channel %s. Current subscribers count: %d", model.Subscriber, model.Channel, len(subs)))
	return result
}
Exemplo n.º 5
0
func (s *Subscriber) CollectMessage(k *knot.WebContext) interface{} {
	result := toolkit.NewResult()
	var model struct {
		Secret string
		Key    string
	}
	k.GetPayload(&model)
	if model.Secret != s.Secret {
		result.SetErrorTxt("Call is not authorised")
		return result
	}
	if model.Key == "" {
		for k, _ := range s.messages {
			model.Key = k
			break
		}
		if model.Key == "" {
			result.SetErrorTxt("No key has been provided to collect message")
			return result
		}
	}

	var data interface{}
	e := s.ReceiveMessage("Que", model.Key, &data)
	if e != nil {
		result.SetErrorTxt("Unable to get message " + model.Key + " from broadcaster. " + e.Error())
		return result
	}
	result.Data = data
	return result
}
Exemplo n.º 6
0
func (c *Coordinator) UpdateMetadata(in toolkit.M) *toolkit.Result {
	result := toolkit.NewResult()
	keys := []string{}
	bs := in.Get("keys", []byte{}).([]byte)
	toolkit.FromBytes(bs, "gob", &keys)
	return result
}
Exemplo n.º 7
0
func (b *Broadcaster) Que(k *knot.WebContext) interface{} {
	result := toolkit.NewResult()
	var model struct {
		UserID, Secret, Key string
		Data                interface{}
	}
	k.GetPayload(&model)
	if b.validateToken("user", model.Secret, model.UserID) == false {
		result.SetErrorTxt("User " + model.UserID + " is not authorised")
		return result
	}

	targets := b.getChannelSubscribers(model.Key)
	if len(targets) == 0 {
		result.SetErrorTxt("No subscriber can receive this message")
		return result
	}

	mm := NewMessageMonitor(b, model.Key, model.Data, DefaultExpiry())
	mm.DistributionType = DistributeAsQue
	result.Data = mm.Message
	go func() {
		mm.Wait()
	}()
	return result
}
Exemplo n.º 8
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
}
Exemplo n.º 9
0
func (b *Broadcaster) AddNode(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 nodeModel.Secret != b.secret {
		result.SetErrorTxt("Not authorised")
		return result
	}

	if b.Subscibers == nil {
		b.Subscibers = map[string]*SubscriberInfo{}
	} else {
		if _, exist := b.Subscibers[nodeModel.Subscriber]; exist {
			result.SetErrorTxt(fmt.Sprintf("%s has been registered as subsriber", nodeModel.Subscriber))
			return result
		}
	}
	si := new(SubscriberInfo)
	si.Address = nodeModel.Subscriber
	si.Protocol = pro
	si.Secret = toolkit.RandomString(32)
	b.Subscibers[nodeModel.Subscriber] = si
	result.Data = si.Secret
	k.Server.Log().Info(fmt.Sprintf("%s register as new subscriber. Current active subscibers: %d", nodeModel.Subscriber, len(b.Subscibers)))
	return result
}
Exemplo n.º 10
0
func (s *Storage) StorageStatus(in toolkit.M) *toolkit.Result {
	r := toolkit.NewResult()
	r.SetBytes(struct {
		Memory StorageInfo
		Disk   StorageInfo
	}{s.MemoryStorage.StorageInfo, s.DiskStorage.StorageInfo}, "")
	return r
}
Exemplo n.º 11
0
func (c *Coordinator) Get(in toolkit.M) *toolkit.Result {
	result := toolkit.NewResult()
	result.SetErrorTxt("Get command is still under development")
	key := in.GetString("key")
	owner, table, datakey := ParseKey(key)
	key = MakeKey(owner, table, datakey)
	return result
}
Exemplo n.º 12
0
func (a *controller) Hi(in toolkit.M) *toolkit.Result {
	r := toolkit.NewResult()
	name := in.GetString("name")
	r.SetBytes(struct {
		HelloMessage string
		TimeNow      time.Time
		Scores       []Score
	}{"Hello " + name, time.Now(), []Score{{"Bahasa Indonesia", 90}, {"Math", 85}}}, "gob")
	return r
}
Exemplo n.º 13
0
func (s *Subscriber) getMsgAsResult(key string) *toolkit.Result {
	result := toolkit.NewResult()
	if key == "" {
		if len(s.messageKeys) > 0 {
			key = s.messageKeys[0]
		}
	}

	// if no key is provided, check from the latest
	if key == "" {
		result.Status = toolkit.Status_NOK
		result.Message = "No key has been provided to receive the message"
	} else {
		msgQue, exist := s.MessageQues[key]
		if !exist {
			result.Status = toolkit.Status_NOK
			result.Message = "Key " + key + " is not exist on message que or it has been collected. Available keys are: " + strings.Join(s.messageKeys, ",")
		} else {
			url := fmt.Sprintf("%s/getmsg", s.BroadcasterAddress)
			r, e := toolkit.HttpCall(url, "POST",
				toolkit.Jsonify(msgQue), nil)
			if e != nil {
				result.SetErrorTxt("Subscriber ReceiveMsg Call Error: " + e.Error())
			} else if r.StatusCode != 200 {
				result.SetErrorTxt("Subsciber ReceiveMsg Call Error: " + r.Status)
			} else {
				//var resultMsg toolkit.Result
				e := toolkit.Unjson(toolkit.HttpContent(r),
					&result)
				if e != nil {
					result.SetErrorTxt(fmt.Sprintf("Subsciber ReceiveMsg Decode Error: ", e.Error()))
				}
				/*else {

					if resultMsg.Status == toolkit.Status_OK {
						result.Data = resultMsg.Data
					} else {
						result.SetErrorTxt(resultMsg.Message)
					}
				}
				*/
			}
		}
	}

	if result.Status == "OK" {
		url := fmt.Sprintf("%s/msgreceived")
		toolkit.HttpCall(url, "POST", toolkit.Jsonify(struct {
			Key        string
			Subscriber string
		}{key, s.Address}), nil)
	}
	return result
}
Exemplo n.º 14
0
func (b *Broadcaster) Logout(k *knot.WebContext) interface{} {
	r := toolkit.NewResult()
	m := toolkit.M{}
	k.GetPayload(&m)
	if !b.validateToken("user", m.GetString("secret"), m.GetString("userid")) {
		r.SetErrorTxt("Not authorised")
		return r
	}
	delete(b.userTokens, m.GetString("userid"))
	return r
}
Exemplo n.º 15
0
func (a *Client) Call(methodName string, in toolkit.M) *toolkit.Result {
	if a.client == nil {
		return toolkit.NewResult().SetErrorTxt("Unable to call, no connection handshake")
	}
	if in == nil {
		in = toolkit.M{}
	}
	out := toolkit.NewResult()
	in["method"] = methodName
	if in.GetString("auth_referenceid") == "" {
		in["auth_referenceid"] = a.sessionID
	}
	//fmt.Println("SessionID: " + a.sessionID)
	if in.Has("auth_secret") == false {
		in.Set("auth_secret", a.secret)
	}
	e := a.client.Call("Rpc.Do", in, out)
	//_ = "breakpoint"
	if e != nil {
		return out.SetErrorTxt(a.address + "." + methodName + " Fail: " + e.Error())
	}
	return out
}
Exemplo n.º 16
0
func (b *Broadcaster) Stop(k *knot.WebContext) interface{} {
	m := toolkit.M{}
	result := toolkit.NewResult()
	k.GetPayload(&m)
	defer func() {
		if result.Status == toolkit.Status_OK {
			k.Server.Stop()
		}
	}()
	valid := b.validateToken("user", m.GetString("secret"),
		m.GetString("userid"))
	if !valid {
		result.SetErrorTxt("Not authorised")
		return result
	}

	return result
}
Exemplo n.º 17
0
func (s *Subscriber) PushMessage(k *knot.WebContext) interface{} {
	result := toolkit.NewResult()
	var model struct {
		Secret string
		Key    string
		Data   interface{}
	}
	k.GetPayload(&model)
	if model.Secret != s.Secret {
		result.SetErrorTxt("Broadcaster not authorised")
		return result
	}
	e := s.ReceiveMessage("Broadcast", model.Key, model.Data)
	if e != nil {
		result.SetErrorTxt("Unable to accept message: " + e.Error())
		return result
	}
	return result
}
Exemplo n.º 18
0
func call(url, call string, data []byte, expectedStatus int) (*toolkit.Result, error) {
	cfg := toolkit.M{}
	if expectedStatus != 0 {
		cfg.Set("expectedstatus", expectedStatus)
	}
	r, e := toolkit.HttpCall(url, call, data, cfg)
	if e != nil {
		return nil, fmt.Errorf(url + " Call Error: " + e.Error())
	}
	result := toolkit.NewResult()
	bs := toolkit.HttpContent(r)
	edecode := toolkit.Unjson(bs, result)
	if edecode != nil {
		return nil, fmt.Errorf(url + " Http Result Decode Error: " + edecode.Error() + "\nFollowing got: " + string(bs))
	}
	if result.Status == toolkit.Status_NOK {
		return nil, fmt.Errorf(url + " Http Result Error: " + result.Message)
	}
	return result, nil
}
Exemplo n.º 19
0
func (b *Broadcaster) MsgStatus(k *knot.WebContext) interface{} {
	k.Config.NoLog = true
	result := toolkit.NewResult()
	var model struct {
		UserID, Secret, Key string
	}
	k.GetPayload(&model)
	if b.validateToken("user", model.Secret, model.UserID) == false {
		result.SetErrorTxt("Not authorised")
		return result
	}

	m, e := b.messages[model.Key]
	if e == false {
		result.SetErrorTxt("Message " + model.Key + " is not exist")
		return result
	}

	result.Data = fmt.Sprintf("Targets: %d, Success: %d, Fail: %d", len(m.Targets), m.Success, m.Fail)
	return result
}
Exemplo n.º 20
0
func (b *Broadcaster) Login(k *knot.WebContext) interface{} {
	r := toolkit.NewResult()
	var loginInfo struct{ UserID, Password string }
	k.GetPayload(&loginInfo)
	if loginInfo.UserID != "arief" && loginInfo.Password != "darmawan" {
		r.SetErrorTxt("Invalid Login")
		return r
	}

	b.initToken()
	if t, te := b.userTokens[loginInfo.UserID]; te {
		if !t.IsExpired() {
			r.SetErrorTxt("User " + loginInfo.UserID + " has logged-in before and already has a valid session created")
		}
	}

	t := NewToken("user", loginInfo.UserID)
	b.userTokens[loginInfo.UserID] = t
	r.Data = t.Secret
	return r
}
Exemplo n.º 21
0
func (c *Coordinator) Set(in toolkit.M) *toolkit.Result {
	var e error
	if in == nil {
		in = toolkit.M{}
	}
	result := toolkit.NewResult()

	key := in.GetString("key")

	// validation
	if key == "" {
		return result.SetErrorTxt("Key is empty")
	}

	data := in.Get("data", []byte{}).([]byte)
	if len(data) == 0 {
		return result.SetErrorTxt("Data is not valid")
	}

	// get available node
	nodeIdx, e := c.getAvailableNode(data)
	if e != nil {
		return result.SetErrorTxt("Coordinator.Set: " + e.Error())
	}
	node := c.Node(RoleStorage, nodeIdx)

	// write the data
	delete(in, "auth_referenceid")
	delete(in, "auth_secret")
	in.Set("data", data)
	rw := node.Call("write", in)

	result.Data = rw.Data
	result.Status = rw.Status
	if result.Status != toolkit.Status_OK {
		result.SetErrorTxt("Coordinator.Set: " + rw.Message)
	}
	return result
}
Exemplo n.º 22
0
func (b *Broadcaster) GetMessage(k *knot.WebContext) interface{} {
	result := toolkit.NewResult()
	var model struct {
		Subscriber, Secret, Key string
	}
	k.GetPayload(&model)
	if model.Key == "" {
		return result.SetErrorTxt("Key is empty")
	}
	if !b.validateToken("node", model.Secret, model.Subscriber) {
		return result.SetErrorTxt("Subscriber is not authorised")
	}
	msg, exist := b.messages[model.Key]
	if !exist {
		return result.SetErrorTxt(fmt.Sprintf("Message %s is not exist. Either it has been fully collected or message is never exist", model.Key))
	}
	targets := b.getChannelSubscribers(model.Key)
	found := false
	foundIndex := 0
	for idx, t := range targets {
		if t == model.Subscriber {
			found = true
			foundIndex = idx
			break
		}
	}
	if !found {
		return result.SetErrorTxt(fmt.Sprintf("Subscibers %s is not valid subscriber of message %s", model.Subscriber, model.Key))
	}
	targets = append(targets[0:foundIndex], targets[foundIndex+1:]...)
	if len(targets) == 0 {
		delete(b.channelSubscribers, model.Key)
	} else {
		b.channelSubscribers[model.Key] = targets
	}
	result.Data = msg.Data
	return result
}
Exemplo n.º 23
0
func (s *Subscriber) NewKey(k *knot.WebContext) interface{} {
	result := toolkit.NewResult()
	var model struct {
		Secret string
		Key    string
		Expiry time.Time
	}
	e := k.GetPayload(&model)
	if e != nil {
		result.SetErrorTxt("Decode error: %s" + e.Error())
		return result
	}
	if model.Secret != s.Secret {
		result.SetErrorTxt("Broadcaster is not authorised")
		return result
	}
	msg := new(Message)
	msg.Key = model.Key
	msg.Expiry = model.Expiry
	s.messages[model.Key] = msg
	s.Server.Log().Info(fmt.Sprintf("Receive new key %s. Current keys available in que: %d", model.Key, len(s.messages)))
	return result
}
Exemplo n.º 24
0
func (b *Broadcaster) initRoute() {
	//-- add node
	b.Route("/nodeadd", func(kr *knot.WebContext) interface{} {
		url := kr.Query("node")
		b.Subscibers = append(b.Subscibers, url)
		kr.Server.Log().Info(fmt.Sprintf("Add node %s to %s", url, b.Address))
		return "OK"
	})

	//-- add channel subscribtion
	b.Route("/channelregister", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()
		cr := &ChannelRegister{}
		k.GetPayload(cr)
		b.addChannelSubcriber(cr.Channel, cr.Subscriber)
		return result
	})

	//-- get the message (used for DstributeAsQue type)
	b.Route("/getmsg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()
		tm := toolkit.M{}
		e := k.GetPayload(&tm)
		if e != nil {
			fmt.Println(e.Error())
			result.SetErrorTxt(fmt.Sprintf("Broadcaster GetMsg Payload Error: %s", e.Error()))
		} else {
			key := tm.Get("Key", "").(string)
			if key == "" {
				result.SetErrorTxt("Broadcaste GetMsg Error: No key is provided")
			} else {
				m, exist := b.messages[key]
				//fmt.Println(m.Key + " : " + m.Data.(string))
				if exist == false {
					result.SetErrorTxt("Message " + key + " is not exist")
				} else {
					result.Data = m.Data
					//fmt.Printf("Sent data: %v \n", m.Data)
				}
			}
		}
		return result
	})

	//-- invoked to identify if a msg has been received (used for DistAsQue)
	b.Route("/msgreceived", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()

		tm := toolkit.M{}
		e := k.GetPayload(&tm)
		if e != nil {
			result.SetErrorTxt("Broadcaster MsgReceived Payload Error: " + e.Error())
		} else {
			key := tm.Get("key", "").(string)
			subscriber := tm.Get("subscriber", "").(string)

			mm, exist := b.messages[key]
			if exist == false {
				result.SetErrorTxt("Broadcaster MsgReceived Error: " + key + " is not exist")
			} else {
				for tIndex, t := range mm.Targets {
					if t == subscriber {
						mm.Status[tIndex] = "OK"
						break
					}
				}
			}
		}

		return result
	})

	//-- gracefully stop the server
	b.Route("/stop", func(k *knot.WebContext) interface{} {
		result := toolkit.NewResult()
		defer func() {
			if result.Status == toolkit.Status_OK {
				k.Server.Stop()
			}
		}()
		for _, s := range b.Subscibers {
			url := "http://" + s + "/stop"
			bs, e := webcall(url, "GET", nil)
			if e != nil {
				result.SetErrorTxt("Unable to stop " + s + ": " + e.Error())
			}
			sresult := toolkit.NewResult()
			toolkit.Unjson(bs, sresult)
			if sresult.Status == toolkit.Status_NOK {
				result.SetErrorTxt("Unable to stop " + s + ": " + sresult.Message)
			}
		}

		k.Config.OutputType = knot.OutputJson
		return result
	})
}
Exemplo n.º 25
0
func (s *Subscriber) Start(address string) error {
	s.Address = address
	s.Actions = map[string]FnTrigger{}
	s.MessageQues = map[string]*MessageQue{}
	broadcasterUrl := s.BroadcasterAddress + "/nodeadd?node=" + s.Address
	r, e := toolkit.HttpCall(broadcasterUrl, "GET", nil, nil)
	if e != nil {
		return fmt.Errorf("Unable to contact Broadcaster Server. %s", e.Error())
	}
	if sOk := toolkit.HttpContentString(r); sOk != "OK" {
		return fmt.Errorf("Unable to add %s as subsriber to %s. Error: %s", s.Address, s.BroadcasterAddress, sOk)
	}

	//-- inform subscriber if newkey is available to be collected
	s.Route("/newkey", func(k *knot.WebContext) interface{} {
		result := toolkit.NewResult()
		k.Config.OutputType = knot.OutputJson
		msg := new(MessageQue)
		e := k.GetPayload(&msg)
		if e != nil {
			result.Message = e.Error()
			result.Status = toolkit.Status_NOK
		} else {
			msg.Data = nil
			msg.Collected = false
			_, exist := s.MessageQues[msg.Key]
			if !exist {
				s.messageKeys = append(s.messageKeys, msg.Key)
			}
			s.MessageQues[msg.Key] = msg
			//s.messageKeys = append(s.messageKeys, msg.Key)
		}
		return result
	})

	s.Route("/getmsg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		key := k.Query("key")
		result := s.getMsgAsResult(key)
		return result
	})

	//-- replicate message to subscribe
	s.Route("/msg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()
		var payload Message
		eDecode := k.GetPayload(&payload)
		if eDecode != nil {
			result.Status = toolkit.Status_NOK
			result.Message = fmt.Sprintf("Subscriber Message Decode Error %s", eDecode.Error())
		} else {
			result.Data = payload
		}
		return result
	})

	//-- stop the server
	s.Route("/stop", func(k *knot.WebContext) interface{} {
		defer k.Server.Stop()
		k.Config.OutputType = knot.OutputJson
		result := new(toolkit.Result)
		result.Status = "OK"
		return result
	})

	go func() {
		s.Server.Listen()
	}()
	return nil
}
Exemplo n.º 26
0
func (s *Server2RPC) Hi2(in toolkit.M) *toolkit.Result {
	return toolkit.NewResult().SetData("Hi from server 2")
}
Exemplo n.º 27
0
func (a *Server) Start(address string) error {
	if a.Log == nil {
		le, e := toolkit.NewLog(true, false, "", "", "")
		if e == nil {
			a.Log = le
		} else {
			return errors.New("Unable to setup log")
		}
	}

	if a.rpcObject == nil {
		//return errorlib.Error(packageName, objServer, "Start", "RPC Object is not yet properly initialized")
		a.rpcObject = new(Rpc)
	}
	/*
		if reloadConfig {
			a.ReadConfig()
		}
	*/

	if a.Address == "" {
		if address != "" {
			a.Address = address
		}
		/*else {
			a.Address = fmt.Sprintf("%s:%d", a.ServerName, a.Port)
		}
		*/
		if a.Address == "" {
			return errors.New("RPC Server address is empty")
		}
	}

	//init a ping method. Ping method will return "EACIIT RPC Application Server"
	a.AddFn("ping", func(in toolkit.M) *toolkit.Result {
		result := toolkit.NewResult()
		result.Data = "EACIIT RPC Application Server"
		return result
	}, false, "")

	a.AddFn("addsession", func(in toolkit.M) *toolkit.Result {
		referenceID := in.GetString("auth_referenceid")
		result := toolkit.NewResult()
		if referenceID == "" {
			return result.SetErrorTxt("Empty user provided")
		}

		//session, exist := a.sessions[referenceID]
		var session *Session

		for _, session = range a.sessions {
			if session.ReferenceID == referenceID && session.IsValid() && !a.AllowMultiLogin {
				return result.SetErrorTxt(referenceID + " already has active session on other connection")
			} else if session.ReferenceID == referenceID && !session.IsValid() && !a.AllowMultiLogin {
				delete(a.sessions, session.SessionID)
			}
		}
		session = a.RegisterSession(referenceID)
		//a.sessions[session.SessionID] = session
		//result.SetBytes(session, MarshallingMethod())
		result.Data = toolkit.M{}.Set("referenceid", session.SessionID).Set("secret", session.Secret).ToBytes("gob")
		a.Log.Info(a.Address + " has new session " + referenceID + " : " + session.SessionID)
		return result
	}, true, "")

	a.AddFn("removesession", func(in toolkit.M) *toolkit.Result {
		result := toolkit.NewResult()
		referenceID := in.GetString("auth_referenceid")
		delete(a.sessions, referenceID)
		return result
	}, true, "session")

	a.Log.Info("Starting server " + a.Address + ". Registered functions are: " + strings.Join(func() []string {
		ret := []string{}
		for k, _ := range a.rpcObject.Fns {
			ret = append(ret, k)
		}
		return ret
	}(), ", "))

	a.rpcServer = rpc.NewServer()
	a.rpcServer.Register(a.rpcObject)
	l, e := net.Listen("tcp", fmt.Sprintf("%s", a.Address))
	if e != nil {
		return e
	}

	a.sessions = map[string]*Session{}
	a.listener = l
	go func() {
		a.rpcServer.Accept(l)
		//rpc.Accept(l)
		/*
			listenerConnection, err := l.Accept()
			if err != nil {
				a.Log.Error("Unable to setup RPC Listener Connection " + err.Error())
				return
			}
			go a.rpcServer.ServeConn(listenerConnection)
		*/
	}()
	return nil
}
Exemplo n.º 28
0
func (s *Storage) StopServer(in toolkit.M) *toolkit.Result {
	r := toolkit.NewResult()
	s.SebarServer.Stop()
	return r
}