Example #1
0
func (r *Router) toEdgeAction(msg *message.Message, reply *message.Reply) (err error) {
	switch msg.Action {
	case message.ADD:
		// index, _ := engine.UnmarshalObjectIndex(msg.Target)
		// r.nextStepMsgs.Put(index, msg)
		body := msg.UnmarshalToAddEdge()
		r.net.AddEdge(body)
		reply.Value = nil
		reply.State = 200
		return nil
	case message.DEL:
		r.msgs.Put(msg.Target, msg)

		reply.Value = nil
		reply.State = 200
		return nil
	case message.PUT:
		r.msgs.Put(msg.Target, msg)

		reply.Value = nil
		reply.State = 200
		return nil
	case message.GET:
		r.msgs.Put(msg.Target, msg)

		reply.Value = nil
		reply.State = 200
		return nil
	default:
		reply.Value, _ = json.Marshal(errors.New("object type is null"))
		reply.State = 201
		return errors.New("text")
	}
	return nil
}
Example #2
0
func (r *Router) toNodeAction(msg *message.Message) (reply *message.Reply) {
	switch msg.Action {
	case message.ADD:
		body := msg.UnmarshalToAddNode()
		r.net.AddNode(engine.NewObjectId(body.Id))
	case message.DEL:
		r.msgs.Put(msg.Target, msg)
		reply.Value = nil
		reply.State = 200
		return nil
	case message.PUT:
		r.msgs.Put(msg.Target, msg)
		reply.Value = nil
		reply.State = 200
		return nil
	case message.GET:
		r.msgs.Put(msg.Target, msg)
		reply.Value = nil
		reply.State = 200
		return nil
	default:
		v, _ := json.Marshal(errors.New("action type is null"))
		reply = &message.Reply{
			Value: v,
			State: 201,
		}
		return
	}
	reply = &message.Reply{
		Value: nil,
		State: 200,
	}
	return reply
}
Example #3
0
func (r *Router) toNodeAction(msg *message.Message, reply *message.Reply) (err error) {
	switch msg.Action {
	case message.ADD:

		body := msg.UnmarshalToAddNode()
		// println(body.Id)
		n := r.net.AddNode(engine.NewObjectId(body.Id))
		if n != nil {
			reply.Value, _ = json.Marshal(n.Index.ToByte())
			reply.State = 200
			return nil
		} else {
			reply.Value = nil
			reply.State = 201
			return errors.New("text")
		}

	case message.DEL:
		r.msgs.Put(msg.Target, msg)
		reply.Value = nil
		reply.State = 200
		return nil
	case message.PUT:
		r.msgs.Put(msg.Target, msg)
		reply.Value = nil
		reply.State = 200
		return nil
	case message.GET:
		r.msgs.Put(msg.Target, msg)
		reply.Value = nil
		reply.State = 200
		return nil
	default:
		v, _ := json.Marshal(errors.New("action type is null"))
		reply = &message.Reply{
			Value: v,
			State: 201,
		}
		return
	}
	return errors.New("unknown errors.")
}
Example #4
0
func (s *Slave) PullSignal(msg *message.Message, reply *message.Reply) (err error) {
	Log.Print("PullSignal")
	if msg.Action == message.NEXTSTEP {
		ns := msg.UnmarshalToNextstep()
		MasterHost = ns.Host
		Log.Print(MasterHost, " ", ns.Step)
		go s.processor.Run(ns.Step, s.MessageContainer)
		// reply = &message.Reply{
		// Value: nil,
		// State: 200,
		// }
		reply.State = 200
		reply.Value = nil
	}
	if msg.Action == message.SYNC {
		//active nodes
		//vote to active
		Log.Print("SYNC REQUEST")
		s.MessageContainer.Nextstep()

		net := s.Net
		for k, _ := range s.MessageContainer.CurMsgMap {
			Log.Print(k.String())
			index, _ := k.Unmarshal()
			net.GetNodeByIndex(index).VoteToActive()
		}
		size, _ := s.MessageContainer.Size()
		// reply = &message.Reply{
		// 	Value: size,
		// 	State: 200,
		// }
		reply.State = 200
		reply.Value, _ = json.Marshal(size)
	}
	return nil
}