func writerPipe(conn *net.TCPConn) {
	for {
		var msg string
		fmt.Scanln(&msg)

		if strings.EqualFold(msg, "quit") {
			quitSp <- true
			break
		}
		testMessage := &pb.TestMessage{
			TestInt:    proto.Int32(123),
			TestString: proto.String("est"),
		}
		fmt.Println(testMessage)
		byt, _ := proto.Marshal(testMessage)
		fmt.Println(byt)

		buff := &pb.MobileSuiteProtobuf{
			Type:    proto.Int32(321),
			Arena:   proto.Int32(111),
			Command: proto.Int32(0xa),
			Message: byt,
		}
		fmt.Println(buff)
		bybuf, _ := proto.Marshal(buff)
		fmt.Println(bybuf)
		conn.Write(bybuf)

	}
}
Exemplo n.º 2
0
// Insert can insert record into a btree
func (t *Btree) insert(record TreeLog) error {
	tnode, err := t.getTreeNode(t.GetRoot())
	if err != nil {
		if err.Error() != "no data" {
			return err
		}
		nnode := t.newTreeNode()
		nnode.NodeType = proto.Int32(isLeaf)
		_, err = nnode.insertRecord(record, t)
		if err == nil {
			t.Nodes[nnode.GetId()], err = proto.Marshal(nnode)
		}
		t.Root = proto.Int64(nnode.GetId())
		return err
	}
	clonednode, err := tnode.insertRecord(record, t)
	if err == nil && len(clonednode.GetKeys()) > int(t.GetNodeMax()) {
		nnode := t.newTreeNode()
		nnode.NodeType = proto.Int32(isNode)
		key, left, right := clonednode.split(t)
		nnode.insertOnce(key, left, right, t)
		t.Nodes[nnode.GetId()], err = proto.Marshal(nnode)
		t.Root = proto.Int64(nnode.GetId())
	} else {
		t.Root = proto.Int64(clonednode.GetId())
	}
	return err
}
Exemplo n.º 3
0
// WriteRequest - implement rpc.ClientCodec interface.
func (c *pbClientCodec) WriteRequest(r *rpc.Request, body interface{}) (err error) {
	// Use a mutex to guarantee the header/body are written in the correct order.
	c.mu.Lock()
	defer c.mu.Unlock()

	// This is protobuf, of course we copy it.
	pbr := &Request{ServiceMethod: &r.ServiceMethod, Seq: &r.Seq}
	data, err := proto.Marshal(pbr)
	if err != nil {
		return
	}
	_, err = WriteNetString(c.rwc, data)
	if err != nil {
		return
	}

	// Of course this is a protobuf! Trust me or detonate the program.
	data, err = proto.Marshal(body.(proto.Message))
	if err != nil {
		return
	}
	_, err = WriteNetString(c.rwc, data)
	if err != nil {
		return
	}

	if flusher, ok := c.rwc.(flusher); ok {
		err = flusher.Flush()
	}
	return
}
Exemplo n.º 4
0
func (c *clientCodec) WriteRequest(rpcreq *rpc.Request, param interface{}) error {
	rr := *rpcreq
	req := &Request{}

	c.mutex.Lock()
	req.Id = proto.Uint64(c.next)
	c.next++
	c.pending[*req.Id] = &rr
	c.mutex.Unlock()

	req.Method = proto.String(rpcreq.ServiceMethod)
	if msg, ok := param.(proto.Message); ok {
		body, err := proto.Marshal(msg)
		if err != nil {
			return err
		}
		req.Body = body
	} else {
		return fmt.Errorf("marshal request param error: %s", param)
	}

	f, err := proto.Marshal(req)
	if err != nil {
		return err
	}

	if err := write(c.w, f); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 5
0
// WriteResponse writes a response on the codec.
func (c *pbServerCodec) WriteResponse(r *rpc.Response, body interface{}, last bool) (err error) {
	// Use a mutex to guarantee the header/body are written in the correct order.
	c.mu.Lock()
	defer c.mu.Unlock()
	rtmp := &Response{ServiceMethod: &r.ServiceMethod, Seq: &r.Seq, Error: &r.Error}
	data, err := proto.Marshal(rtmp)
	if err != nil {
		return
	}
	_, err = WriteNetString(c.rwc, data)
	if err != nil {
		return
	}

	if pb, ok := body.(proto.Message); ok {
		data, err = proto.Marshal(pb)
		if err != nil {
			return
		}
	} else {
		data = nil
	}
	_, err = WriteNetString(c.rwc, data)
	if err != nil {
		return
	}

	if flusher, ok := c.rwc.(flusher); ok {
		err = flusher.Flush()
	}
	return
}
Exemplo n.º 6
0
//处理查找节点的请求
//本节点定期查询已知节点是否在线,更新节点信息
func (this *Manager) read() {
	clientConn, _ := this.serverManager.GetController().GetSession(this.rootId.String())
	for {
		node := <-this.nodeStore.OutFindNode
		if node.NodeId != nil {
			findNodeOne := &msg.FindNodeReq{
				NodeId: proto.String(this.nodeStore.GetRootId()),
				FindId: proto.String(node.NodeId.String()),
			}
			findNodeBytes, _ := proto.Marshal(findNodeOne)
			// clientConn := this.serverManager.GetController().GetClientByName("firstConnPeer")
			// fmt.Println(clientConn)
			clientConn.Send(msg.FindNodeReqNum, &findNodeBytes)
		}
		if node.NodeIdShould != nil {
			findNodeOne := &msg.FindNodeReq{
				NodeId: proto.String(this.nodeStore.GetRootId()),
				FindId: proto.String(node.NodeIdShould.String()),
			}
			findNodeBytes, _ := proto.Marshal(findNodeOne)
			// clientConn := this.serverManager.GetController().GetClientByName("firstConnPeer")
			clientConn.Send(msg.FindNodeReqNum, &findNodeBytes)
		}
	}
}
Exemplo n.º 7
0
func sendRequest(c *Client, conn *connection, rpcCall *call) error {
	//log.Println("About to call RPC: ", rpcCall.procedure)

	// 0. RpcRequestHeaderProto
	var clientId [16]byte = [16]byte(*c.ClientId)
	rpcReqHeaderProto := hadoop_common.RpcRequestHeaderProto{RpcKind: &gohadoop.RPC_PROTOCOL_BUFFFER, RpcOp: &gohadoop.RPC_FINAL_PACKET, CallId: &rpcCall.callId, ClientId: clientId[0:16], RetryCount: &rpcCall.retryCount}
	rpcReqHeaderProtoBytes, err := proto.Marshal(&rpcReqHeaderProto)
	if err != nil {
		log.Fatal("proto.Marshal(&rpcReqHeaderProto)", err)
		return err
	}

	// 1. RequestHeaderProto
	requestHeaderProto := rpcCall.procedure
	requestHeaderProtoBytes, err := proto.Marshal(requestHeaderProto)
	if err != nil {
		log.Fatal("proto.Marshal(&requestHeaderProto)", err)
		return err
	}

	// 2. Param
	paramProto := rpcCall.request
	paramProtoBytes, err := proto.Marshal(paramProto)
	if err != nil {
		log.Fatal("proto.Marshal(&paramProto)", err)
		return err
	}

	totalLength := len(rpcReqHeaderProtoBytes) + sizeVarint(len(rpcReqHeaderProtoBytes)) + len(requestHeaderProtoBytes) + sizeVarint(len(requestHeaderProtoBytes)) + len(paramProtoBytes) + sizeVarint(len(paramProtoBytes))
	var tLen int32 = int32(totalLength)
	if totalLengthBytes, err := gohadoop.ConvertFixedToBytes(tLen); err != nil {
		log.Fatal("ConvertFixedToBytes(totalLength)", err)
		return err
	} else {
		if _, err := conn.con.Write(totalLengthBytes); err != nil {
			log.Fatal("conn.con.Write(totalLengthBytes)", err)
			return err
		}
	}

	if err := writeDelimitedBytes(conn, rpcReqHeaderProtoBytes); err != nil {
		log.Fatal("writeDelimitedBytes(conn, rpcReqHeaderProtoBytes)", err)
		return err
	}
	if err := writeDelimitedBytes(conn, requestHeaderProtoBytes); err != nil {
		log.Fatal("writeDelimitedBytes(conn, requestHeaderProtoBytes)", err)
		return err
	}
	if err := writeDelimitedBytes(conn, paramProtoBytes); err != nil {
		log.Fatal("writeDelimitedBytes(conn, paramProtoBytes)", err)
		return err
	}

	//log.Println("Succesfully sent request of length: ", totalLength)

	return nil
}
Exemplo n.º 8
0
func (c *context) Call(service, method string, in, out proto.Message, opts *internal.CallOptions) error {
	req, err := proto.Marshal(in)
	if err != nil {
		return fmt.Errorf("error marshalling request: %v", err)
	}

	remReq := &pb.Request{
		ServiceName: proto.String(service),
		Method:      proto.String(method),
		Request:     req,
		// NOTE(djd): RequestId is unused in the server.
	}

	req, err = proto.Marshal(remReq)
	if err != nil {
		return fmt.Errorf("proto.Marshal: %v", err)
	}

	// TODO(djd): Respect opts.Timeout?
	resp, err := c.client.Post(c.url, "application/octet-stream", bytes.NewReader(req))
	if err != nil {
		return fmt.Errorf("error sending request: %v", err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("bad response %d; body: %q", resp.StatusCode, body)
	}
	if err != nil {
		return fmt.Errorf("failed reading response: %v", err)
	}
	remResp := &pb.Response{}
	if err := proto.Unmarshal(body, remResp); err != nil {
		return fmt.Errorf("error unmarshalling response: %v", err)
	}

	if ae := remResp.GetApplicationError(); ae != nil {
		return &internal.APIError{
			Code:    ae.GetCode(),
			Detail:  ae.GetDetail(),
			Service: service,
		}
	}

	if remResp.Response == nil {
		return fmt.Errorf("unexpected response: %s", proto.MarshalTextString(remResp))
	}

	return proto.Unmarshal(remResp.Response, out)
}
Exemplo n.º 9
0
func writeConnectionContext(c *Client, conn *connection, connectionId *connection_id, authProtocol gohadoop.AuthProtocol) error {
	// Create hadoop_common.IpcConnectionContextProto
	ugi, _ := gohadoop.CreateSimpleUGIProto()
	ipcCtxProto := hadoop_common.IpcConnectionContextProto{UserInfo: ugi, Protocol: &connectionId.protocol}

	// Create RpcRequestHeaderProto
	var callId int32 = -3
	var clientId [16]byte = [16]byte(*c.ClientId)

	/*if (authProtocol == gohadoop.AUTH_PROTOCOL_SASL) {
	  callId = SASL_RPC_CALL_ID
	}*/

	rpcReqHeaderProto := hadoop_common.RpcRequestHeaderProto{RpcKind: &gohadoop.RPC_PROTOCOL_BUFFFER, RpcOp: &gohadoop.RPC_FINAL_PACKET, CallId: &callId, ClientId: clientId[0:16], RetryCount: &gohadoop.RPC_DEFAULT_RETRY_COUNT}

	rpcReqHeaderProtoBytes, err := proto.Marshal(&rpcReqHeaderProto)
	if err != nil {
		log.Fatal("proto.Marshal(&rpcReqHeaderProto)", err)
		return err
	}

	ipcCtxProtoBytes, _ := proto.Marshal(&ipcCtxProto)
	if err != nil {
		log.Fatal("proto.Marshal(&ipcCtxProto)", err)
		return err
	}

	totalLength := len(rpcReqHeaderProtoBytes) + sizeVarint(len(rpcReqHeaderProtoBytes)) + len(ipcCtxProtoBytes) + sizeVarint(len(ipcCtxProtoBytes))
	var tLen int32 = int32(totalLength)
	totalLengthBytes, err := gohadoop.ConvertFixedToBytes(tLen)

	if err != nil {
		log.Fatal("ConvertFixedToBytes(totalLength)", err)
		return err
	} else if _, err := conn.con.Write(totalLengthBytes); err != nil {
		log.Fatal("conn.con.Write(totalLengthBytes)", err)
		return err
	}

	if err := writeDelimitedBytes(conn, rpcReqHeaderProtoBytes); err != nil {
		log.Fatal("writeDelimitedBytes(conn, rpcReqHeaderProtoBytes)", err)
		return err
	}
	if err := writeDelimitedBytes(conn, ipcCtxProtoBytes); err != nil {
		log.Fatal("writeDelimitedBytes(conn, ipcCtxProtoBytes)", err)
		return err
	}

	return nil
}
func runAvailabilityPipelineAugmented(startTimestamp int64, timestamps map[string]int64, moreTimestamps map[string]int64) {
	levelDbManager := store.NewSliceManager()

	tracesStore := levelDbManager.Writer("traces")
	tracesStore.BeginWriting()
	for encodedKey, timestamp := range timestamps {
		trace := Trace{
			TraceCreationTimestamp: proto.Int64(timestamp),
		}
		encodedTrace, err := proto.Marshal(&trace)
		if err != nil {
			panic(fmt.Errorf("Error encoding protocol buffer: %v", err))
		}
		tracesStore.WriteRecord(&store.Record{Key: []byte(encodedKey), Value: encodedTrace})
	}
	tracesStore.EndWriting()

	writer := bytes.NewBuffer([]byte{})
	transformer.RunPipeline(AvailabilityPipeline(levelDbManager, writer, startTimestamp))

	tracesStore.BeginWriting()
	for encodedKey, timestamp := range moreTimestamps {
		trace := Trace{
			TraceCreationTimestamp: proto.Int64(timestamp),
		}
		encodedTrace, err := proto.Marshal(&trace)
		if err != nil {
			panic(fmt.Errorf("Error encoding protocol buffer: %v", err))
		}
		tracesStore.WriteRecord(&store.Record{Key: []byte(encodedKey), Value: encodedTrace})
	}
	tracesStore.EndWriting()

	anotherTracesSlice := make([]*store.Record, 0)
	for encodedKey, timestamp := range moreTimestamps {
		trace := Trace{
			TraceCreationTimestamp: proto.Int64(timestamp),
		}
		encodedTrace, err := proto.Marshal(&trace)
		if err != nil {
			panic(fmt.Errorf("Error encoding protocol buffer: %v", err))
		}
		anotherTracesSlice = append(anotherTracesSlice, &store.Record{Key: []byte(encodedKey), Value: encodedTrace})
	}

	anotherWriter := bytes.NewBuffer([]byte{})
	transformer.RunPipeline(AvailabilityPipeline(levelDbManager, anotherWriter, startTimestamp))
	fmt.Printf("%s", anotherWriter.Bytes())
}
Exemplo n.º 11
0
func (n *TreeNode) merge(tree *Btree, index int) int64 {
	left, err := tree.getTreeNode(n.Childrens[index])
	if err != nil {
		return -1
	}
	right, err := tree.getTreeNode(n.Childrens[index+1])
	if err != nil {
		return -1
	}
	if len(left.Keys)+len(right.Keys) > int(tree.GetNodeMax()) {
		return -1
	}
	if (len(left.Values) + len(right.Values)) > int(tree.GetLeafMax()) {
		return -1
	}
	leftClone := left.clone(tree)
	n.Childrens[index] = leftClone.GetId()
	if leftClone.GetNodeType() == isLeaf {
		if index == len(n.Keys) {
			n.Childrens = n.Childrens[:index]
			n.Keys = n.Keys[:index-1]
		} else {
			n.Childrens = append(n.Childrens[:index+1], n.Childrens[index+2:]...)
			n.Keys = append(n.Keys[:index], n.Keys[index+1:]...)
		}
		// add right to left
		leftClone.Values = append(leftClone.Values, right.Values...)
		leftClone.Keys = append(leftClone.Keys, right.Keys...)
	} else {
		leftClone.Keys = append(leftClone.Keys, append([][]byte{n.Keys[index]}, right.Keys...)...)
		// merge childrens
		leftClone.Childrens = append(leftClone.Childrens, right.Childrens...)
		// remove old key
		n.Keys = append(n.Keys[:index], n.Keys[index+1:]...)
		// remove old right node
		n.Childrens = append(n.Childrens[:index+1], n.Childrens[index+2:]...)
		// check size, spilt if over size
		if len(leftClone.Keys) > int(tree.GetNodeMax()) {
			key, left, right := leftClone.split(tree)
			n.insertOnce(key, left, right, tree)
		}
	}
	atomic.StoreInt32(right.IsDirt, 1)
	atomic.StoreInt32(left.IsDirt, 1)
	tree.Nodes[right.GetId()], err = proto.Marshal(right)
	tree.Nodes[left.GetId()], err = proto.Marshal(left)
	tree.Nodes[leftClone.GetId()], err = proto.Marshal(leftClone)
	return leftClone.GetId()
}
Exemplo n.º 12
0
func WriteBlock(file *os.File, block proto.Message, blockType string) error {
	blobContent, err := proto.Marshal(block)
	if err != nil {
		return err
	}

	var blobContentLength int32 = int32(len(blobContent))

	var compressedBlob bytes.Buffer
	zlibWriter := zlib.NewWriter(&compressedBlob)
	zlibWriter.Write(blobContent)
	zlibWriter.Close()

	blob := OSMPBF.Blob{}
	blob.ZlibData = compressedBlob.Bytes()
	blob.RawSize = &blobContentLength
	blobBytes, err := proto.Marshal(&blob)
	if err != nil {
		return err
	}

	var blobBytesLength int32 = int32(len(blobBytes))

	blobHeader := OSMPBF.BlobHeader{}
	blobHeader.Type = &blockType
	blobHeader.Datasize = &blobBytesLength
	blobHeaderBytes, err := proto.Marshal(&blobHeader)
	if err != nil {
		return err
	}

	var blobHeaderLength int32 = int32(len(blobHeaderBytes))

	err = binary.Write(file, binary.BigEndian, blobHeaderLength)
	if err != nil {
		return err
	}
	_, err = file.Write(blobHeaderBytes)
	if err != nil {
		return err
	}
	_, err = file.Write(blobBytes)
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 13
0
func (conn *ProtoBufConn) Call(cmd uint32, args interface{}) (err error) {

	var msg proto.Message
	var buf []byte

	switch m := args.(type) {
	case proto.Message:
		msg = m

		buf, err = proto.Marshal(msg)
		if err != nil {
			return err
		}
	case []byte:
		buf = m
	default:
		return fmt.Errorf("Call args type error %v", args)
	}

	req := &protobuf.Packet{}
	req.Cmd = &cmd
	req.SerializedPacket = buf

	return conn.writeRequest(req)
}
Exemplo n.º 14
0
// Request serializes the data (using protobuf), adds the header and sends it to Riak.
func (c *Client) request(req proto.Message, name string) (err error, conn *net.TCPConn) {
	err, conn = c.getConn()
	if err != nil {
		return err, nil
	}
	// Serialize the request using protobuf
	pbmsg, err := proto.Marshal(req)
	if err != nil {
		return err, conn
	}
	// Build message with header: <length:32> <msg_code:8> <pbmsg>
	i := int32(len(pbmsg) + 1)
	msgbuf := []byte{byte(i >> 24), byte(i >> 16), byte(i >> 8), byte(i), messageCodes[name]}
	msgbuf = append(msgbuf, pbmsg...)
	// Send to Riak
	err = c.write(conn, msgbuf)
	// If an error occurred when sending request
	if err != nil {
		// Make sure connection will be released in the end
		defer c.releaseConn(conn)

		var errno syscall.Errno

		// If the error is not recoverable like a broken pipe, close all connections,
		// so next time when getConn() is called it will Connect() again
		if operr, ok := err.(*net.OpError); ok {
			if errno, ok = operr.Err.(syscall.Errno); ok {
				if errno == syscall.EPIPE {
					c.Close()
				}
			}
		}
	}
	return err, conn
}
Exemplo n.º 15
0
// usageString returns a description of the amount of space taken up by a body
// with the given contents and a bool indicating overflow.
func (draft *Draft) usageString() (string, bool) {
	var replyToId *uint64
	if draft.inReplyTo != 0 {
		replyToId = proto.Uint64(1)
	}
	var dhPub [32]byte

	msg := &pond.Message{
		Id:               proto.Uint64(0),
		Time:             proto.Int64(1 << 62),
		Body:             []byte(draft.body),
		BodyEncoding:     pond.Message_RAW.Enum(),
		InReplyTo:        replyToId,
		MyNextDh:         dhPub[:],
		Files:            draft.attachments,
		DetachedFiles:    draft.detachments,
		SupportedVersion: proto.Int32(protoVersion),
	}

	serialized, err := proto.Marshal(msg)
	if err != nil {
		panic("error while serialising candidate Message: " + err.Error())
	}

	s := fmt.Sprintf("%d of %d bytes", len(serialized), pond.MaxSerializedMessage)
	return s, len(serialized) > pond.MaxSerializedMessage
}
Exemplo n.º 16
0
func (c *context) Call(service, method string, in, out ProtoMessage, opts *CallOptions) error {
	if service == "__go__" {
		if method == "GetNamespace" {
			out.(*basepb.StringProto).Value = proto.String(c.req.Header.Get("X-AppEngine-Current-Namespace"))
			return nil
		}
		if method == "GetDefaultNamespace" {
			out.(*basepb.StringProto).Value = proto.String(c.req.Header.Get("X-AppEngine-Default-Namespace"))
			return nil
		}
	}
	if f, ok := apiOverrides[struct{ service, method string }{service, method}]; ok {
		return f(in, out, opts)
	}
	data, err := proto.Marshal(in)
	if err != nil {
		return err
	}

	requestID := c.req.Header.Get("X-Appengine-Internal-Request-Id")
	res, err := call(service, method, data, requestID)
	if err != nil {
		return err
	}
	return proto.Unmarshal(res, out)
}
Exemplo n.º 17
0
func mustMarshal(p interface{}) []byte {
	buf, err := proto.Marshal(p)
	if err != nil {
		panic(err)
	}
	return buf
}
Exemplo n.º 18
0
// Heart 测试
func tstHeart() {
	tstfun := "tstHeart"
	util.LogInfo("<<<<<<%s TEST", tstfun)
	syn := &pushproto.Talk{
		Type: pushproto.Talk_HEART.Enum(),
	}

	data, err := proto.Marshal(syn)
	if err != nil {
		util.LogError("%s ERROR:proto marshal error:%s", tstfun, err)
		return
	}

	sb := util.Packdata(data)
	tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) {
		pb_type := pb.GetType()
		if pb_type == pushproto.Talk_HEART {
			util.LogInfo(">>>>>>%s PASS", tstfun)
		} else {
			util.LogError("%s ERROR", tstfun)
		}

	})

}
Exemplo n.º 19
0
func mustMarshal(p proto.Message) []byte {
	buf, err := proto.Marshal(p)
	if err != nil {
		panic(err)
	}
	return buf
}
Exemplo n.º 20
0
func (conn *ProtoBufConn) WriteObj(value interface{}) error {

	var msg proto.Message

	switch m := value.(type) {
	case proto.Message:
		msg = m
	default:
		return fmt.Errorf("WriteObj value type error %v", value)
	}

	buf, err := proto.Marshal(msg)
	if err != nil {
		return err
	}

	req := &protobuf.Packet{}

	t := reflect.Indirect(reflect.ValueOf(msg)).Type()
	if conn.resultServer == "" {
		req.SetCmd(conn.protocol[t.PkgPath()+"."+t.Name()])
	} else {
		req.SetCmd(conn.protocol[conn.resultServer+"."+t.Name()])
	}
	req.SerializedPacket = buf
	return conn.writeRequest(req)
}
Exemplo n.º 21
0
func handleChosen(slot uint64) {

	var msg fproto.InstructionChosen
	msg.Slot = new(uint64)
	*msg.Slot = slot
	msgBuf, err := proto.Marshal(&msg)
	if err != nil {
		panic("generated bad instruction chosen message")
	}

	var baseMsg baseproto.Message
	baseMsg.MsgType = new(uint32)
	*baseMsg.MsgType = 7
	baseMsg.Content = msgBuf

	connectionsLock.Lock()

	for _, conn := range connections {

		conn.lock.Lock()

		if !conn.sendingBurst {
			conn.conn.Send(&baseMsg)
		}

		if timer, exists := conn.offerTimers[slot]; exists {
			timer.Stop()
			delete(conn.offerTimers, slot)
		}

		conn.lock.Unlock()
	}

	connectionsLock.Unlock()
}
Exemplo n.º 22
0
// Serialize serializes the given struct into bytes with protobuf, then base64 encodes it.  The struct passed to Serialize must satisfy proto.Message.
func (p ProtobufStateSerializer) Serialize(state interface{}) (string, error) {
	bin, err := proto.Marshal(state.(proto.Message))
	if err != nil {
		return "", errors.Trace(err)
	}
	return base64.StdEncoding.EncodeToString(bin), nil
}
Exemplo n.º 23
0
func (pkg *Package) Marshal() []byte {
	bytes, err := proto.Marshal(pkg.Package)
	if err != nil {
		log.Panic(err)
	}
	return bytes
}
Exemplo n.º 24
0
// 连接建立,clientid获取
func tstSyn() {
	tstfun := "tstSyn"
	util.LogInfo("<<<<<<%s TEST", tstfun)
	syn := &pushproto.Talk{
		Type:       pushproto.Talk_SYN.Enum(),
		Appid:      proto.String("shawn"),
		Installid:  proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"),
		Auth:       proto.String("F**k"),
		Clienttype: proto.String("Android"),
		Clientver:  proto.String("1.0.0"),
	}

	data, err := proto.Marshal(syn)
	if err != nil {
		util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err)
		return
	}

	sb := util.Packdata(data)
	tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) {
		pb_type := pb.GetType()
		if pb_type == pushproto.Talk_SYNACK {
			util.LogInfo(">>>>>>%s PASS: client_id:%s", tstfun, pb.GetClientid())
		} else {
			util.LogError("%s ERROR", tstfun)
		}

	})

}
Exemplo n.º 25
0
func (this *GbpEncDecoder) Marshal(pack interface{}) ([]byte, error) {
	if protomsg, ok := pack.(proto.Message); ok {
		return proto.Marshal(protomsg)
	}

	return nil, ErrorTypeNotFit
}
Exemplo n.º 26
0
func main() {
	test := &example.Test{
		Label: proto.String("hello"),
		Type:  proto.Int32(17),
		Optionalgroup: &example.Test_OptionalGroup{
			RequiredField: proto.String("good bye"),
		},
	}
	log.Println(test)
	data, err := proto.Marshal(test)
	log.Println(data, err)
	if err != nil {
		log.Fatal("marshaling error: ", err)
	}
	newTest := &example.Test{}
	err = proto.Unmarshal(data, newTest)
	if err != nil {
		log.Fatal("unmarshaling error: ", err)
	}
	// Now test and newTest contain the same data.
	log.Println(test.GetLabel(), newTest.GetLabel())
	if test.GetLabel() != newTest.GetLabel() {
		log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
	}
	// etc.
}
Exemplo n.º 27
0
func handleProposalChange() {
	proposal, leader := store.Proposal()

	var msg fproto.Leader
	msg.Proposal = new(uint64)
	msg.Leader = new(uint32)
	*msg.Proposal = proposal
	*msg.Leader = uint32(leader)
	msgBuf, err := proto.Marshal(&msg)
	if err != nil {
		panic("generated bad leader update message")
	}

	var baseMsg baseproto.Message
	baseMsg.MsgType = new(uint32)
	*baseMsg.MsgType = 11
	baseMsg.Content = msgBuf

	connectionsLock.Lock()

	for _, conn := range connections {
		conn.conn.Send(&baseMsg)
	}

	connectionsLock.Unlock()
}
Exemplo n.º 28
0
func serializeNode(node *Node) ([]byte, error) {
	var rst []byte
	var data []byte
	var err error
	if data, err = proto.Marshal(&node.IndexMetaData); err != nil {
		return rst, err
	}
	rst = append(rst, encodefixed32(uint64(len(data)))...)
	rst = append(rst, data...)
	if data, err = proto.Marshal(&node.NodeRecordMetaData); err != nil {
		return rst, err
	}
	rst = append(rst, encodefixed32(uint64(len(data)))...)
	rst = append(rst, data...)
	return rst, nil
}
Exemplo n.º 29
0
func handleSend(conn net.Conn) {
	defer conn.Close()
	//encode by protobuf
	req := &testpb.TestReq{
		Str:     proto.String("kevin's test'"),
		IntList: []int32{9, 7},
	}

	buff, err := proto.Marshal(req)
	if err == nil {
		fmt.Println("TestReq:", req)
		fmt.Println("TestReq buff:", buff)
	} else {
		fmt.Println("Marshal TestReq failed", err)
		return
	}

	_, err = conn.Write(buff)
	checkError(err, conn)

	result, err := ioutil.ReadAll(conn)
	checkError(err, conn)

	fmt.Println("receive from svr:", string(result))

	//decode
	rsp := new(testpb.TestRsp)
	err = proto.Unmarshal(result, rsp)
	if err != nil {
		fmt.Println("Unmarshal TestRsp", err)
	} else {
		fmt.Println("Unmarshal result:", rsp)
	}
}
Exemplo n.º 30
0
// Echo 测试
func tstEcho() {
	tstfun := "tstEcho"
	util.LogInfo("<<<<<<%s TEST", tstfun)
	syn := &pushproto.Talk{
		Type:    pushproto.Talk_ECHO.Enum(),
		Extdata: []byte("JUST ECHO"),
	}

	data, err := proto.Marshal(syn)
	if err != nil {
		util.LogError("%s ERROR:proto marshal error:%s", tstfun, err)
		return
	}

	sb := util.Packdata(data)
	tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) {
		pb_type := pb.GetType()
		if pb_type == pushproto.Talk_ECHO {
			util.LogInfo(">>>>>>%s PASS: %s", tstfun, string(pb.GetExtdata()))
		} else {
			util.LogError("%s ERROR", tstfun)
		}

	})

}