github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/common/messages.go (about) 1 // Copyright (c) 2014, Google, Inc.. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // File: messages.go 14 15 package simpleexample_messages 16 17 import ( 18 "errors" 19 "log" 20 21 "github.com/golang/protobuf/proto" 22 "github.com/jlmucb/cloudproxy/go/util" 23 ) 24 25 func PrintMessage(msg *SimpleMessage) { 26 log.Printf("Message\n") 27 if msg.MessageType != nil { 28 log.Printf("\tmessage type: %d\n", *msg.MessageType) 29 } else { 30 log.Printf("\tmessage type: nil\n") 31 } 32 if msg.RequestType != nil { 33 log.Printf("\trequest_type: %s\n", *msg.RequestType) 34 } else { 35 log.Printf("\trequest_type: nil\n") 36 } 37 if msg.Err != nil { 38 log.Printf("\terror: %s\n", msg.Err) 39 } 40 log.Printf("\tdata (%d): ", len(msg.GetData())) 41 for _, data := range msg.GetData() { 42 log.Printf("\t: %x\n", data) 43 } 44 log.Printf("\n") 45 } 46 47 func SendMessage(ms *util.MessageStream, msg *SimpleMessage) error { 48 out, err := proto.Marshal(msg) 49 if err != nil { 50 return errors.New("SendRequest: Can't encode response") 51 } 52 send := string(out) 53 _, err = ms.WriteString(send) 54 if err != nil { 55 return errors.New("SendResponse: Writestring error") 56 } 57 return nil 58 } 59 60 func GetMessage(ms *util.MessageStream) (*SimpleMessage, 61 error) { 62 resp, err := ms.ReadString() 63 if err != nil { 64 return nil, err 65 } 66 msg := new(SimpleMessage) 67 err = proto.Unmarshal([]byte(resp), msg) 68 if err != nil { 69 return nil, errors.New("GetResponse: Can't unmarshal message") 70 } 71 return msg, nil 72 } 73 74 func SendRequest(ms *util.MessageStream, msg *SimpleMessage) error { 75 m1 := int32(MessageType_REQUEST) 76 msg.MessageType = &m1 77 return SendMessage(ms, msg) 78 } 79 80 func SendResponse(ms *util.MessageStream, msg *SimpleMessage) error { 81 m1 := int32(MessageType_RESPONSE) 82 msg.MessageType = &m1 83 return SendMessage(ms, msg) 84 } 85 86 func GetRequest(ms *util.MessageStream) (*SimpleMessage, error) { 87 msg, err := GetMessage(ms) 88 if err != nil || *msg.MessageType != int32(MessageType_REQUEST) { 89 return nil, errors.New("GetResponse: reception error") 90 } 91 return msg, nil 92 } 93 94 func GetResponse(ms *util.MessageStream) (*SimpleMessage, error) { 95 msg, err := GetMessage(ms) 96 if err != nil || *msg.MessageType != int32(MessageType_RESPONSE) { 97 return nil, errors.New("GetResponse: reception error") 98 } 99 return msg, nil 100 }