Project Moscato Team Messaging Middleware Implemetation Message Middleware by Golang Operate as Secure, Effectively
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

message.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package modules
  2. import "encoding/json"
  3. //*****메세지 타입 상수화
  4. const (
  5. KGM = 1 + iota //KeyGenMessage
  6. KSM //KeyShareMessage
  7. PM //PublishMessage
  8. SM //SubscriptionMessage
  9. RM //RegisterMessage
  10. WM //WithdrawMessage
  11. )
  12. //*****메세지 틀*****
  13. type Message struct {
  14. From string //메세지 만든 주체의 Ip주소
  15. Version string //메세지 버전
  16. Time string //메세지 만든 시간
  17. Kind int //메세지 종류
  18. }
  19. type MsgUnit interface {
  20. ConvertToJson() ([]byte, error) //메세지를 JSON형식으로 바꿔주는 멤버함수
  21. CheckType() int //메세지의 종류를 반환하는 함수
  22. }
  23. //*****각 메세지 형식 및 정의**********
  24. //KeyGen 명령 메세지
  25. type KeyGenMsg struct {
  26. Message
  27. iptable []string
  28. }
  29. //Key공유 메세지
  30. type KeyShareMsg struct {
  31. Message
  32. key string
  33. }
  34. //전달할 내용을 담은 메세지
  35. type PublishMsg struct {
  36. Message
  37. Topic []int64 //대주제 ex)soccer
  38. Value []int64 //topic의 세부적인 내용 ex)ManCity or 40
  39. Content []int64 //내용 ex)오늘 케빈 데 브라위너가 골을 넣었다
  40. }
  41. //구독 정보를 담은 메세지
  42. type SubscriptionMsg struct {
  43. Message
  44. Topic []int64 //대주제 ex)soccer
  45. Value []int64 //피연산자 ex)Mancity or 20 40
  46. Operator []string //연산자 ex) ==, < && >
  47. IsAlpha bool //value가 숫자인지 문자열인지, 문자열이면 단순비교, 숫자이면 범위연산
  48. }
  49. //Microservice 등록 메세지
  50. type RegisterMsg struct {
  51. Message
  52. PrivateKey int64
  53. }
  54. //Microservice 탈퇴 메세지(없앰)
  55. type WithdrawMsg struct {
  56. Message
  57. }
  58. //******ConverToJson을 메세지 종류별로 실행가능하게 구현******
  59. func (msg KeyGenMsg) ConvertToJson() ([]byte, error) {
  60. js := msg
  61. jsonBytes, err := json.Marshal(js)
  62. return jsonBytes, err
  63. }
  64. func (msg KeyShareMsg) ConvertToJson() ([]byte, error) {
  65. js := msg
  66. jsonBytes, err := json.Marshal(js)
  67. return jsonBytes, err
  68. }
  69. func (msg PublishMsg) ConvertToJson() ([]byte, error) {
  70. js := msg
  71. jsonBytes, err := json.Marshal(js)
  72. return jsonBytes, err
  73. }
  74. func (msg SubscriptionMsg) ConvertToJson() ([]byte, error) {
  75. js := msg
  76. jsonBytes, err := json.Marshal(js)
  77. return jsonBytes, err
  78. }
  79. func (msg RegisterMsg) ConvertToJson() ([]byte, error) {
  80. js := msg
  81. jsonBytes, err := json.Marshal(js)
  82. return jsonBytes, err
  83. }
  84. func (msg WithdrawMsg) ConvertToJson() ([]byte, error) {
  85. js := msg
  86. jsonBytes, err := json.Marshal(js)
  87. return jsonBytes, err
  88. }
  89. //CheckType함수 구현
  90. func (msg Message) CheckType() int {
  91. return msg.Kind //메세지 멤버변수 Kind 리턴
  92. }
  93. //KeyGenMsg 생성자
  94. func NewKeyGenMsg(table *MStable) *KeyGenMsg {
  95. m := &KeyGenMsg{}
  96. for _, value := range table.NodeTable { // MicroService테이블에서 ip주소를 다 가져와서 iptable에 넣음
  97. m.iptable = append(m.iptable, value.GetIpaddr())
  98. }
  99. return m
  100. }