Exemplo n.º 1
0
// Poll fetches the latest state of a long-running operation.
//
// If Poll fails, the error is returned and op is unmodified.
// If Poll succeeds and the operation has completed with failure,
// the error is returned and op.Done will return true.
// If Poll succeeds and the operation has completed successfully,
// op.Done will return true; if resp != nil, the response of the operation
// is stored in resp.
func (op *Operation) Poll(ctx context.Context, resp proto.Message) error {
	if !op.Done() {
		p, err := op.c.GetOperation(ctx, &pb.GetOperationRequest{Name: op.Name()})
		if err != nil {
			return err
		}
		op.proto = p
	}
	if !op.Done() {
		return nil
	}

	switch r := op.proto.Result.(type) {
	case *pb.Operation_Error:
		// TODO (pongad): r.Details may contain further information
		return grpc.Errorf(codes.Code(r.Error.Code), "%s", r.Error.Message)
	case *pb.Operation_Response:
		if resp == nil {
			return nil
		}
		return ptypes.UnmarshalAny(r.Response, resp)
	default:
		return fmt.Errorf("unsupported result type %[1]T: %[1]v", r)
	}
}
Exemplo n.º 2
0
func (kvserver *KvServer) PingStreamCS(stream BfKvService_PingStreamCSServer) error {
	log.Printf("===PingStreamCS===,clientid=(%s)", getClientId(stream.Context()))
	pingResp := &BfPingData{Message: message}
	anyResp, err := ptypes.MarshalAny(pingResp)
	if err != nil {
		log.Fatalf("MarshalAny fail,%v", err)
		return err
	}

	for {
		anyReq, err := stream.Recv()
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}

		pingReq := &BfPingData{}
		if ptypes.Is(anyReq, pingReq) {
			ptypes.UnmarshalAny(anyReq, pingReq)
			log.Printf("recv,%s", pingReq.Message)
		} else {
			log.Fatalf("PingStreamS,%v", anyReq)
			return err
		}

		if err := stream.Send(anyResp); err != nil {
			return err
		}
	}
}
Exemplo n.º 3
0
func (kvserver *KvServer) PingStreamS(anyReq *Any, stream BfKvService_PingStreamSServer) error {
	log.Printf("===PingStreamS===,clientid=(%s)", getClientId(stream.Context()))

	pingReq := &BfPingData{}
	if ptypes.Is(anyReq, pingReq) {
		ptypes.UnmarshalAny(anyReq, pingReq)
		log.Printf("recv,%s", pingReq.Message)
	} else {
		log.Fatalf("PingStreamS,%v", anyReq)
		return nil
	}

	pingResp := &BfPingData{Message: message}
	anyResp, err := ptypes.MarshalAny(pingResp)
	if err != nil {
		log.Fatalf("MarshalAny fail,%v", err)
		return err
	}
	rd := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < 10; i++ {
		log.Printf("send,%d", i)
		if err := stream.Send(anyResp); err != nil {
			return err
		}
		s := 500 + rd.Int31n(500) - 1
		time.Sleep(time.Duration(s) * time.Millisecond)
	}

	return nil
}
Exemplo n.º 4
0
//===internal api===
func (client *BfTrderClient) DispatchPush(anyResp *Any) {
	if ptypes.Is(anyResp, tickType_) {
		tickResp := &BfTickData{}
		ptypes.UnmarshalAny(anyResp, tickResp)
		spi_.OnTick(tickResp)
	} else if ptypes.Is(anyResp, pingType_) {
		pingResp := &BfPingData{}
		ptypes.UnmarshalAny(anyResp, pingResp)
		spi_.OnPing(pingResp)
	} else if ptypes.Is(anyResp, accountType_) {
		accountResp := &BfAccountData{}
		ptypes.UnmarshalAny(anyResp, accountResp)
		spi_.OnAccount(accountResp)
	} else if ptypes.Is(anyResp, positionType_) {
		positionResp := &BfPositionData{}
		ptypes.UnmarshalAny(anyResp, positionResp)
		spi_.OnPosition(positionResp)
	} else if ptypes.Is(anyResp, orderType_) {
		orderResp := &BfOrderData{}
		ptypes.UnmarshalAny(anyResp, orderResp)
		spi_.OnOrder(orderResp)
	} else if ptypes.Is(anyResp, tradeType_) {
		tradeResp := &BfTradeData{}
		ptypes.UnmarshalAny(anyResp, tradeResp)
		spi_.OnTrade(tradeResp)
	} else if ptypes.Is(anyResp, logType_) {
		logResp := &BfLogData{}
		ptypes.UnmarshalAny(anyResp, logResp)
		spi_.OnLog(logResp)
	} else if ptypes.Is(anyResp, errorType_) {
		errorResp := &BfErrorData{}
		ptypes.UnmarshalAny(anyResp, errorResp)
		spi_.OnError(errorResp)
	} else if ptypes.Is(anyResp, notificationType_) {
		notificationResp := &BfNotificationData{}
		ptypes.UnmarshalAny(anyResp, notificationResp)
		spi_.OnNotification(notificationResp)
	} else {
		log.Printf("invalid type message,%v", anyResp)
	}
}
Exemplo n.º 5
0
func fromLogEntry(le *logpb.LogEntry) (*logging.Entry, error) {
	time, err := ptypes.Timestamp(le.Timestamp)
	if err != nil {
		return nil, err
	}
	var payload interface{}
	switch x := le.Payload.(type) {
	case *logpb.LogEntry_TextPayload:
		payload = x.TextPayload

	case *logpb.LogEntry_ProtoPayload:
		var d ptypes.DynamicAny
		if err := ptypes.UnmarshalAny(x.ProtoPayload, &d); err != nil {
			return nil, fmt.Errorf("logging: unmarshalling proto payload: %v", err)
		}
		payload = d.Message

	case *logpb.LogEntry_JsonPayload:
		// Leave this as a Struct.
		// TODO(jba): convert to map[string]interface{}?
		payload = x.JsonPayload

	default:
		return nil, fmt.Errorf("logging: unknown payload type: %T", le.Payload)
	}
	hr, err := toHTTPRequest(le.HttpRequest)
	if err != nil {
		return nil, err
	}
	return &logging.Entry{
		Timestamp:   time,
		Severity:    logging.Severity(le.Severity),
		Payload:     payload,
		Labels:      le.Labels,
		InsertID:    le.InsertId,
		HTTPRequest: hr,
		Operation:   le.Operation,
		LogName:     slashUnescaper.Replace(le.LogName),
		Resource:    le.Resource,
	}, nil
}
Exemplo n.º 6
0
// Metadata unmarshals op's metadata into meta.
// If op does not contain any metadata, Metadata returns ErrNoMetadata and meta is unmodified.
func (op *Operation) Metadata(meta proto.Message) error {
	if m := op.proto.Metadata; m != nil {
		return ptypes.UnmarshalAny(m, meta)
	}
	return ErrNoMetadata
}