Esempio n. 1
0
func main() {
	raddr, err := net.ResolveTCPAddr("net", "localhost:1234")
	if err != nil {
		panic(err)
	}

	var line messages.Line
	for {
		line, err = messages.Connect(raddr)
		if err != nil {
			fmt.Println(err)
			time.Sleep(2e9)
		} else {
			break
		}
	}

	var j messages.Job
	j.Project = "john"
	j.Name = "py1"
	j.Version = 2

	w := messages.WorkerFromDirectory("john", "py1", "python", 2, "py1")

	msg := w.Message()
	line.Out <- msg
	for i := 0; i < 1; i++ {
		ji := j
		//ji.ID = fmt.Sprintf("%d", i)
		line.Out <- ji.Message()
	}

	for msg := range line.In {
		switch string(msg.Payload["type"]) {
		case messages.JobAckType:
			ja, _ := messages.JobAckFromMessage(msg)
			var jsr messages.JobStatusRequest
			jsr.JobHeader = ja.JobHeader
			msg = jsr.Message()
			msg.Broadcast = true
			line.Out <- msg
		case messages.JobStatusType:
			js, _ := messages.JobStatusFromMessage(msg)
			fmt.Println(js.JobHeader, js.Status)
		}
	}
}
Esempio n. 2
0
func handleMessages(mline messages.Line) {
	log.Println("connected to", mline.Conn.RemoteAddr())
	defer log.Println("lost connection to", mline.Conn.RemoteAddr())

	for msg := range mline.In {
		go func(msg messages.Message, conn *net.TCPConn) {
			if msg.Broadcast {
				broadcastMessage(msg)
			}

			// toss this to subscribers
			go func(msg messages.Message, line messages.Line) {
				subscriberLock.Lock()
				for _, ch := range subscriberChans {
					ch <- MsgSrc{msg, line}
				}
				subscriberLock.Unlock()
			}(msg, mline)

			typ, ok := msg.Payload["type"]

			if !ok {
				log.Println("received message with no type")
				return
			}
			switch string(typ) {
			case "load":
				l, err := LoadFromMessage(msg, conn)
				if err != nil {
					log.Println(err)
					return
				}
				recvLoad(l)
			case "ident":
				i, err := IdentFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvIdent(i, conn)
			case messages.WorkerType:
				w, err := messages.WorkerFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvWorker(Worker{w, conn})
			case messages.WorkerRequestType:
				w, err := messages.WorkerRequestFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvWorkerRequest(WorkerRequest{w, conn})
			case messages.JobType:
				j, err := messages.JobFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvJob(Job{j, conn})
			case messages.JobAckType:
				ja, err := messages.JobAckFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvJobAck(ja)
			case messages.JobStatusRequestType:
				jsr, err := messages.JobStatusRequestFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvJobStatusRequest(jsr)
			case messages.JobStatusType:
				js, err := messages.JobStatusFromMessage(msg)
				if err != nil {
					log.Println(err)
					return
				}
				recvJobStatus(js)
			}
		}(msg, mline.Conn)
	}
}