github.com/annchain/OG@v0.0.9/plugin/og/adapter_og.go (about) 1 package og 2 3 import ( 4 "errors" 5 general_message "github.com/annchain/OG/message" 6 "github.com/annchain/OG/ogcore/communication" 7 "github.com/annchain/OG/ogcore/message" 8 ) 9 10 type OgMessageUnmarshaller struct { 11 } 12 13 func (a *OgMessageUnmarshaller) Unmarshal( 14 messageType message.OgMessageType, messageBytes []byte) ( 15 outMsg message.OgMessage, err error) { 16 switch messageType { 17 case message.OgMessageTypePing: 18 m := &message.OgMessagePing{} 19 _, err = m.UnmarshalMsg(messageBytes) 20 outMsg = m 21 case message.OgMessageTypePong: 22 m := &message.OgMessagePong{} 23 _, err = m.UnmarshalMsg(messageBytes) 24 outMsg = m 25 case message.OgMessageTypeQueryStatusRequest: 26 m := &message.OgMessageQueryStatusRequest{} 27 _, err = m.UnmarshalMsg(messageBytes) 28 outMsg = m 29 case message.OgMessageTypeQueryStatusResponse: 30 m := &message.OgMessageQueryStatusResponse{} 31 _, err = m.UnmarshalMsg(messageBytes) 32 outMsg = m 33 case message.OgMessageTypeBatchSyncRequest: 34 m := &message.OgMessageBatchSyncRequest{} 35 _, err = m.UnmarshalMsg(messageBytes) 36 outMsg = m 37 case message.OgMessageTypeSyncResponse: 38 m := &message.OgMessageSyncResponse{} 39 _, err = m.UnmarshalMsg(messageBytes) 40 outMsg = m 41 case message.OgMessageTypeNewResource: 42 m := &message.OgMessageNewResource{} 43 _, err = m.UnmarshalMsg(messageBytes) 44 outMsg = m 45 case message.OgMessageTypeHeightSyncRequest: 46 m := &message.OgMessageHeightSyncRequest{} 47 _, err = m.UnmarshalMsg(messageBytes) 48 outMsg = m 49 case message.OgMessageTypeHeaderRequest: 50 m := &message.OgMessageHeaderRequest{} 51 _, err = m.UnmarshalMsg(messageBytes) 52 outMsg = m 53 default: 54 err = errors.New("message type of OG not supported") 55 } 56 return 57 } 58 59 type DefaultOgMessageAdapter struct { 60 unmarshaller OgMessageUnmarshaller 61 } 62 63 func (d *DefaultOgMessageAdapter) AdaptGeneralMessage(incomingMsg general_message.GeneralMessage) (ogMessage message.OgMessage, err error) { 64 mssageType := incomingMsg.GetType() 65 if mssageType != MessageTypeOg { 66 err = errors.New("SimpleOgAdapter received a message of an unsupported type") 67 return 68 } 69 gma := incomingMsg.(*GeneralMessageOg) 70 71 return d.unmarshaller.Unmarshal(gma.InnerMessageType, gma.InnerMessage) 72 } 73 74 func (d *DefaultOgMessageAdapter) AdaptGeneralPeer(gnrPeer general_message.GeneralPeer) (communication.OgPeer, error) { 75 return communication.OgPeer{ 76 Id: gnrPeer.Id, 77 PublicKey: gnrPeer.PublicKey, 78 Address: gnrPeer.Address, 79 PublicKeyBytes: gnrPeer.PublicKeyBytes, 80 }, nil 81 } 82 83 func (d *DefaultOgMessageAdapter) AdaptOgMessage(outgoingMsg message.OgMessage) (msg general_message.GeneralMessage, err error) { 84 msg = &GeneralMessageOg{ 85 InnerMessageType: outgoingMsg.GetType(), 86 InnerMessage: outgoingMsg.ToBytes(), 87 } 88 return 89 } 90 91 func (d *DefaultOgMessageAdapter) AdaptOgPeer(annPeer *communication.OgPeer) (general_message.GeneralPeer, error) { 92 return general_message.GeneralPeer{ 93 Id: annPeer.Id, 94 PublicKey: annPeer.PublicKey, 95 Address: annPeer.Address, 96 PublicKeyBytes: annPeer.PublicKeyBytes, 97 }, nil 98 }