Beispiel #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
}
Beispiel #2
0
func (s *Superstep) Put(msg *message.Message, reply *message.Reply) (err error) {
	Log.Print("Put")
	if msg.Action == message.SLAVEDONE {
		atomic.AddInt32(&s.counter, 1)
		Log.Print("<<Slave Done: ", s.counter)
		if s.counter == s.slaveNum {
			atomic.StoreInt32(&s.counter, 0)
			Log.Print("=Slave Done Counter Reset: ", s.counter)
			s.runningStep <- 2
		}
		reply.State = 200
		reply.Value = nil
		return nil
	}
	return errors.New("wrong messages action.")
}
Beispiel #3
0
func (r *Router) Route(msg *message.Message, reply *message.Reply) (err error) {
	switch msg.Object {
	case message.LOCAL:
		return r.toLocalAction(msg, reply)
	case message.NODE:
		return r.toNodeAction(msg, reply)
	case message.EDGE:
		return r.toEdgeAction(msg, reply)
	default:
		reply.Value, _ = json.Marshal(errors.New("object type is null"))
		reply.State = 201
		return errors.New("object type is null")
	}
}
Beispiel #4
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.")
}
Beispiel #5
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
}