Beispiel #1
0
func GetPeerInfo(file string, addr string) (*PeerInfo, error) {
	peerInfo := &PeerInfo{}

	dialer := &net.Dialer{
		Timeout: 5 * time.Second,
	}

	conn, err := dialer.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	_, err = conn.Write([]byte("  V1"))
	if err != nil {
		return nil, err
	}
	cmd := common.Lookup(file)
	_, err = cmd.WriteTo(conn)
	if err != nil {
		return nil, err
	}
	resp, err := proto.ReadResponse(conn)
	if err != nil {
		println("receive error  response")
		return nil, err
	}
	println(string(resp))
	err = json.Unmarshal(resp, peerInfo)
	if err != nil {
		return nil, err
	}
	return peerInfo, nil
}
Beispiel #2
0
func (lp *lookupPeer) Command(cmd *common.Command) ([]byte, error) {
	state := lp.state
	//init or reconnect
	if lp.state != stateConnected {
		err := lp.Connect()
		if err != nil {
			return nil, err
		}
		lp.state = stateConnected
		lp.Write([]byte("  V1"))
		if state == stateDisconnected {
			lp.connectCallback(lp)
		}
	}

	if cmd == nil {
		return nil, nil
	}
	_, err := cmd.WriteTo(lp)
	if err != nil {
		lp.Close()
		return nil, err
	}
	data, err := proto.ReadResponse(lp)
	if err != nil {
		lp.Close()
		return nil, err
	}
	return data, nil
}