// handleConnection will decode the data received and aggregates it into its // stats func (m *Monitor) handleConnection(conn net.Conn) { dec := json.NewDecoder(conn) nerr := 0 for { measure := &SingleMeasure{} if err := dec.Decode(measure); err != nil { // if end of connection if err == io.EOF || strings.Contains(err.Error(), "closed") { break } // otherwise log it log.Lvl2("Error: monitor decoding from", conn.RemoteAddr().String(), ":", err) nerr++ if nerr > 1 { log.Lvl2("Monitor: too many errors from", conn.RemoteAddr().String(), ": Abort.") break } } log.Lvlf3("Monitor: received a Measure from %s: %+v", conn.RemoteAddr().String(), measure) // Special case where the measurement is indicating a FINISHED step switch strings.ToLower(measure.Name) { case "end": log.Lvl3("Finishing monitor") m.done <- conn.RemoteAddr().String() default: m.measures <- measure } } }
// GenLocalHosts will create n hosts with the first one being connected to each of // the other nodes if connect is true. func GenLocalHosts(n int, connect bool, processMessages bool) []*Host { hosts := make([]*Host, n) for i := 0; i < n; i++ { host := NewLocalHost(2000 + i*10) hosts[i] = host } root := hosts[0] for _, host := range hosts { host.ListenAndBind() log.Lvlf3("Listening on %s %x", host.ServerIdentity.First(), host.ServerIdentity.ID) if processMessages { host.StartProcessMessages() } if connect && root != host { log.Lvl4("Connecting", host.ServerIdentity.First(), host.ServerIdentity.ID, "to", root.ServerIdentity.First(), root.ServerIdentity.ID) if _, err := host.Connect(root.ServerIdentity); err != nil { log.Fatal(host.ServerIdentity.Addresses, "Could not connect hosts", root.ServerIdentity.Addresses, err) } // Wait for connection accepted in root connected := false for !connected { time.Sleep(time.Millisecond * 10) root.networkLock.Lock() for id := range root.connections { if id.Equal(host.ServerIdentity.ID) { connected = true break } } root.networkLock.Unlock() } log.Lvl4(host.ServerIdentity.First(), "is connected to root") } } return hosts }
// Runs a command on the remote host and outputs an eventual error if debug level >= 3 func runSSH(host, cmd string) { if _, err := platform.SSHRun("", host, cmd); err != nil { log.Lvlf3("Host %s got error %s while running [%s]", host, err.Error(), cmd) } }