コード例 #1
0
ファイル: cspub_server.go プロジェクト: gethinzhang/crawler
func (server *CspubResultServer) request_handler(conn net.Conn, out chan string) {
	for {
		_, resp, err := baidu.NsheadRead(conn)
		if err != nil {
			env.Log.Warn("read error " + err.Error())
			break
		}

		for _, receiver := range server.receivers {
			context := receiver.GetContextInterface()
			fetchResult := &CspubFetchResult{
				User_data: context,
			}
			if err := baidu.Unmarshal(resp, fetchResult); err != nil {
				env.Log.Warn("unmarshal resp %s error", string(resp))
				break
			}

			go receiver.HandleCspubResult(fetchResult)
		}
	}
}
コード例 #2
0
ファイル: cspub_client.go プロジェクト: gethinzhang/crawler
func (client *CspubClient) Connect(addr string) *CspubError {
	for i := 0; i < client.Retry; i++ {
		conn, err := net.DialTimeout("tcp", addr,
			time.Duration(client.Timeout)*time.Second)
		if err != nil {
			log.Printf("connect to %s error (%d/%d): %s",
				addr, i+1, client.Retry, err.Error())
			continue
		}
		client.Connection = conn

		cslogin := CspubLogin{
			Username: client.Username,
			Userkey:  client.Userkey,
			Cmd_type: CMD_LOGIN,
			Priority: 0,
		}

		login_body, merr := baidu.Marshal(cslogin)
		if merr != nil {
			return &CspubError{"packing error: " + merr.Error()}
		}
		request := baidu.NsheadPack(login_body, 0)
		client.Connection.Write(request)
		_, response, cerr := baidu.NsheadRead(client.Connection)
		if cerr != nil {
			return &CspubError{"read pack error: " + cerr.Error()}
		}

		baidu.Unmarshal(response, &client.Cred)
		return nil
	}

	env.Log.Warn("connect to %s error after try %d times", addr, client.Retry)

	return &CspubError{"connect error"}
}