Esempio n. 1
0
func TestIsDifferentUrlPrefixes(t *testing.T) {
	m := &pb.FileDescriptorProto{}
	a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
	if !Is(a, m) {
		t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
	}
}
Esempio n. 2
0
File: ipc.go Progetto: yinqiwen/ssf
func (ipc *procIPCEventHandler) OnEvent(event *Event, conn io.ReadWriteCloser) {
	if event.GetType() == EventType_NOTIFY {
		switch event.GetMsgType() {
		case proto.MessageName((*HeartBeat)(nil)):
			var hbres HeartBeat
			hbres.Res = proto.Bool(true)
			notify(&hbres, 0, conn)
		case proto.MessageName((*AdminEvent)(nil)):
			event.decode()
			attach := getSessionAttach(event.GetHashCode())
			if nil != attach {
				attach.(*writeBack).wr.Write([]byte(event.Msg.(*AdminEvent).GetContent()))
			}

		default:
			dispatch(event)
		}
	} else {
		if event.GetTo() != getProcessorName() {
			dispatch(event)
			return
		}
		event.decode()
		if event.GetType() == EventType_RESPONSE {
			triggerClientSessionRes(event.Msg, event.GetHashCode())
		} else {
			//hanlder event
		}
	}
}
Esempio n. 3
0
func (c *Connection) handle() {
	var msg proto.Message
	for {
		select {
		case <-c.doneCh:
			return
		case msg = <-c.recv:
		}
		log.WithField("msg", msg).Debug("new msg")
		switch v := msg.(type) {
		case *protos.SubmitTask:
			if v.GetMulti() {
				generateTasks(v.GetOptions())
			} else {
				t := &Task{}
				t.id = getNextTaskID()
				t.options = v.GetOptions()
				taskChan <- t
			}
		case *protos.TaskStatus:
			c.worker.taskStatusChan <- v
		case *protos.TaskResult:
			c.worker.taskResultChan <- v
		case *protos.SubmitCode:
			addJobType(v)
		default:
			log.WithFields(log.Fields{
				"type":    proto.MessageName(msg),
				"message": v,
			}).Error("connection.handle invalid type")
		}
	}
}
Esempio n. 4
0
func writeEvent(ev *Event, writer io.Writer) error {
	if len(ev.raw) > 0 {
		_, err := writer.Write(ev.raw)
		return err
	}
	var buf bytes.Buffer
	dataBuf, err := proto.Marshal(ev.Msg)
	if nil != err {
		return err
	}
	msgtype := proto.MessageName(ev.Msg)
	ev.MsgType = &msgtype
	headbuf, _ := proto.Marshal(ev)
	headerLen := uint32(len(headbuf))

	// length = msglength(3byte) + headerlen(1byte)
	length := uint32(len(dataBuf))
	length = (length << 8) + headerLen
	buf.Write(MAGIC_EVENT_HEADER)
	binary.Write(&buf, binary.LittleEndian, length)

	buf.Write(headbuf)
	buf.Write(dataBuf)
	_, err = buf.WriteTo(writer)

	return err
}
Esempio n. 5
0
// MarshalAny takes the protocol buffer and encodes it into google.protobuf.Any.
func MarshalAny(pb proto.Message) (*any.Any, error) {
	value, err := proto.Marshal(pb)
	if err != nil {
		return nil, err
	}
	return &any.Any{TypeUrl: googleApis + proto.MessageName(pb), Value: value}, nil
}
Esempio n. 6
0
// Is returns true if any value contains a given message type.
func Is(any *any.Any, pb proto.Message) bool {
	aname, err := AnyMessageName(any)
	if err != nil {
		return false
	}

	return aname == proto.MessageName(pb)
}
Esempio n. 7
0
func messageNameForPackage(pkg string, message golang.Message) (string, error) {
	switch pkg {
	case "golang":
		return golang.MessageName(message), nil
	//case "gogo":
	//return gogo.MessageName(message), nil
	default:
		return "", fmt.Errorf("lion: unknown package: %s", pkg)
	}
}
Esempio n. 8
0
func packMessage(message proto.Message, target *actor.PID) (*messages.MessageEnvelope, error) {
	typeName := proto.MessageName(message)
	bytes, err := proto.Marshal(message)
	if err != nil {
		return nil, err
	}
	envelope := &messages.MessageEnvelope{
		TypeName:    typeName,
		MessageData: bytes,
		Target:      target,
	}

	return envelope, nil
}
Esempio n. 9
0
// UnmarshalAny parses the protocol buffer representation in a google.protobuf.Any
// message and places the decoded result in pb. It returns an error if type of
// contents of Any message does not match type of pb message.
//
// pb can be a proto.Message, or a *DynamicAny.
func UnmarshalAny(any *any.Any, pb proto.Message) error {
	if d, ok := pb.(*DynamicAny); ok {
		if d.Message == nil {
			var err error
			d.Message, err = Empty(any)
			if err != nil {
				return err
			}
		}
		return UnmarshalAny(any, d.Message)
	}

	aname, err := AnyMessageName(any)
	if err != nil {
		return err
	}

	mname := proto.MessageName(pb)
	if aname != mname {
		return fmt.Errorf("mismatched message type: got %q want %q", aname, mname)
	}
	return proto.Unmarshal(any.Value, pb)
}
Esempio n. 10
0
func makeGolden() []golden {
	nested := &pb.Nested{Bunny: "Monty"}
	nb, err := proto.Marshal(nested)
	if err != nil {
		panic(err)
	}
	m1 := &pb.Message{
		Name:        "David",
		ResultCount: 47,
		Anything:    &anypb.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(nested), Value: nb},
	}
	m2 := &pb.Message{
		Name:        "David",
		ResultCount: 47,
		Anything:    &anypb.Any{TypeUrl: "http://[::1]/type.googleapis.com/" + proto.MessageName(nested), Value: nb},
	}
	m3 := &pb.Message{
		Name:        "David",
		ResultCount: 47,
		Anything:    &anypb.Any{TypeUrl: `type.googleapis.com/"/` + proto.MessageName(nested), Value: nb},
	}
	m4 := &pb.Message{
		Name:        "David",
		ResultCount: 47,
		Anything:    &anypb.Any{TypeUrl: "type.googleapis.com/a/path/" + proto.MessageName(nested), Value: nb},
	}
	m5 := &anypb.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(nested), Value: nb}

	any1 := &testpb.MyMessage{Count: proto.Int32(47), Name: proto.String("David")}
	proto.SetExtension(any1, testpb.E_Ext_More, &testpb.Ext{Data: proto.String("foo")})
	proto.SetExtension(any1, testpb.E_Ext_Text, proto.String("bar"))
	any1b, err := proto.Marshal(any1)
	if err != nil {
		panic(err)
	}
	any2 := &testpb.MyMessage{Count: proto.Int32(42), Bikeshed: testpb.MyMessage_GREEN.Enum(), RepBytes: [][]byte{[]byte("roboto")}}
	proto.SetExtension(any2, testpb.E_Ext_More, &testpb.Ext{Data: proto.String("baz")})
	any2b, err := proto.Marshal(any2)
	if err != nil {
		panic(err)
	}
	m6 := &pb.Message{
		Name:        "David",
		ResultCount: 47,
		Anything:    &anypb.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(any1), Value: any1b},
		ManyThings: []*anypb.Any{
			&anypb.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(any2), Value: any2b},
			&anypb.Any{TypeUrl: "type.googleapis.com/" + proto.MessageName(any1), Value: any1b},
		},
	}

	const (
		m1Golden = `
name: "David"
result_count: 47
anything: <
  [type.googleapis.com/proto3_proto.Nested]: <
    bunny: "Monty"
  >
>
`
		m2Golden = `
name: "David"
result_count: 47
anything: <
  ["http://[::1]/type.googleapis.com/proto3_proto.Nested"]: <
    bunny: "Monty"
  >
>
`
		m3Golden = `
name: "David"
result_count: 47
anything: <
  ["type.googleapis.com/\"/proto3_proto.Nested"]: <
    bunny: "Monty"
  >
>
`
		m4Golden = `
name: "David"
result_count: 47
anything: <
  [type.googleapis.com/a/path/proto3_proto.Nested]: <
    bunny: "Monty"
  >
>
`
		m5Golden = `
[type.googleapis.com/proto3_proto.Nested]: <
  bunny: "Monty"
>
`
		m6Golden = `
name: "David"
result_count: 47
anything: <
  [type.googleapis.com/testdata.MyMessage]: <
    count: 47
    name: "David"
    [testdata.Ext.more]: <
      data: "foo"
    >
    [testdata.Ext.text]: "bar"
  >
>
many_things: <
  [type.googleapis.com/testdata.MyMessage]: <
    count: 42
    bikeshed: GREEN
    rep_bytes: "roboto"
    [testdata.Ext.more]: <
      data: "baz"
    >
  >
>
many_things: <
  [type.googleapis.com/testdata.MyMessage]: <
    count: 47
    name: "David"
    [testdata.Ext.more]: <
      data: "foo"
    >
    [testdata.Ext.text]: "bar"
  >
>
`
	)
	return []golden{
		{m1, strings.TrimSpace(m1Golden) + "\n", strings.TrimSpace(compact(m1Golden)) + " "},
		{m2, strings.TrimSpace(m2Golden) + "\n", strings.TrimSpace(compact(m2Golden)) + " "},
		{m3, strings.TrimSpace(m3Golden) + "\n", strings.TrimSpace(compact(m3Golden)) + " "},
		{m4, strings.TrimSpace(m4Golden) + "\n", strings.TrimSpace(compact(m4Golden)) + " "},
		{m5, strings.TrimSpace(m5Golden) + "\n", strings.TrimSpace(compact(m5Golden)) + " "},
		{m6, strings.TrimSpace(m6Golden) + "\n", strings.TrimSpace(compact(m6Golden)) + " "},
	}
}
Esempio n. 11
0
func GetMessageType(message proto.Message) string {
	return proto.MessageName(message)
}
Esempio n. 12
0
func (p *pusher) getLogrusEntry(entry *protolog.Entry) (*logrus.Entry, error) {
	logrusLevel, ok := levelToLogrusLevel[entry.Level]
	if !ok {
		return nil, fmt.Errorf("protolog: no logrus Level for %v", entry.Level)
	}
	logrusEntry := logrus.NewEntry(p.logger)
	logrusEntry.Time = prototime.TimestampToTime(entry.Timestamp)
	logrusEntry.Level = logrusLevel

	if p.options.EnableID {
		logrusEntry.Data["_id"] = entry.Id
	}
	if !p.options.DisableContexts {
		contexts, err := entry.UnmarshalledContexts()
		if err != nil {
			return nil, err
		}
		for _, context := range contexts {
			if context == nil {
				continue
			}
			name := proto.MessageName(context)
			switch name {
			case "protolog.Fields":
				protologFields, ok := context.(*protolog.Fields)
				if !ok {
					return nil, fmt.Errorf("protolog: expected *protolog.Fields, got %T", context)
				}
				for key, value := range protologFields.Value {
					if value != "" {
						logrusEntry.Data[key] = value
					}
				}
			default:
				if err := addProtoMessage(logrusEntry, context); err != nil {
					return nil, err
				}
			}
		}
	}
	event, err := entry.UnmarshalledEvent()
	if err != nil {
		return nil, err
	}
	if event != nil {
		name := proto.MessageName(event)
		switch name {
		case "protolog.Event":
			protologEvent, ok := event.(*protolog.Event)
			if !ok {
				return nil, fmt.Errorf("protolog: expected *protolog.Event, got %T", event)
			}
			logrusEntry.Message = trimRightSpace(protologEvent.Message)
		case "protolog.WriterOutput":
			writerOutput, ok := event.(*protolog.WriterOutput)
			if !ok {
				return nil, fmt.Errorf("protolog: expected *protolog.WriterOutput, got %T", event)
			}
			logrusEntry.Message = trimRightSpace(string(writerOutput.Value))
		default:
			logrusEntry.Data["_event"] = name
			if err := addProtoMessage(logrusEntry, event); err != nil {
				return nil, err
			}
		}
	}
	return logrusEntry, nil
}
Esempio n. 13
0
func messageName(message proto.Message) string {
	if message == nil {
		return ""
	}
	return proto.MessageName(message)
}