func (as *AgentServer) Run() { //register agent killHeartBeaterChan := make(chan bool, 1) go client.NewHeartBeater(as.Port, as.Master).StartAgentHeartBeat(killHeartBeaterChan, func(values url.Values) { resource.AddToValues(values, as.computeResource, as.allocatedResource) values.Add("dataCenter", *as.Option.DataCenter) values.Add("rack", *as.Option.Rack) }) for { // Listen for an incoming connection. conn, err := as.l.Accept() if err != nil { fmt.Println("Error accepting: ", err.Error()) os.Exit(1) } // Handle connections in a new goroutine. as.wg.Add(1) go func() { defer as.wg.Done() defer conn.Close() as.handleRequest(conn) }() } }
func (as *AgentServer) handleWriteConnection(r io.Reader, name string) { as.name2StoreCond.L.Lock() ds, ok := as.name2Store[name] if ok { as.doDelete(name) } s, err := store.NewLocalFileDataStore(as.dir, fmt.Sprintf("%s-%d", name, as.Port)) if err != nil { log.Printf("Failed to create a queue on disk: %v", err) as.name2StoreCond.L.Unlock() return } as.name2Store[name] = NewLiveDataStore(s) ds = as.name2Store[name] // println(name, "is broadcasting...") as.name2StoreCond.Broadcast() as.name2StoreCond.L.Unlock() //register stream go client.NewHeartBeater(as.Port, *as.Option.Master).StartChannelHeartBeat(ds.killHeartBeater, name) // println(name, "start writing.") buf := make([]byte, 4) for { _, message, err := util.ReadBytes(r, buf) if err == io.EOF { // println("agent recv eof:", string(message.Bytes())) break } if err == nil { util.WriteBytes(ds.store, buf, message) // println("agent recv:", string(message.Bytes())) } if message.Flag() != util.Data { // println("finished writing", name) break } } }