Skip to content

Commit

Permalink
feat: Add fromMatrixID field in BaseMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronosXYZ committed Sep 22, 2019
1 parent 64f65ef commit 9590443
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
7 changes: 4 additions & 3 deletions api/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ const (

// BaseMessage is the basic message format of our protocol
type BaseMessage struct {
Body string `json:"body"`
To string `json:"to"`
Flag int `json:"flag"`
Body string `json:"body"`
To string `json:"to"`
Flag int `json:"flag"`
FromMatrixID string `json:"fromMatrixID"`
}

// GetTopicsRespondMessage is the format of the message to answer of request for topics
Expand Down
56 changes: 30 additions & 26 deletions pkg/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"context"
"encoding/json"
"log"
"sync"
Expand All @@ -25,9 +24,10 @@ type Handler struct {

// TextMessage is more end-user model of regular text messages
type TextMessage struct {
Topic string
Body string
From string
Topic string
Body string
FromPeerID string
FromMatrixID string
}

func NewHandler(pb *pubsub.PubSub, serviceTopic string, peerID peer.ID, networkTopics *mapset.Set) Handler {
Expand Down Expand Up @@ -59,23 +59,21 @@ func (h *Handler) HandleIncomingMessage(topic string, msg pubsub.Message, handle
switch message.Flag {
// Getting regular message
case api.FlagGenericMessage:
from := addr.String()
if h.matrixID != "" {
from = h.matrixID
}

textMessage := TextMessage{
Topic: topic,
Body: message.Body,
From: from,
Topic: topic,
Body: message.Body,
FromPeerID: addr.String(),
FromMatrixID: message.FromMatrixID,
}
handleTextMessage(textMessage)
// Getting topic request, answer topic response
case api.FlagTopicsRequest:
respond := &api.GetTopicsRespondMessage{
BaseMessage: api.BaseMessage{
Body: "",
Flag: api.FlagTopicsResponse,
Body: "",
Flag: api.FlagTopicsResponse,
FromMatrixID: h.matrixID,
To: addr.String(),
},
Topics: h.GetTopics(),
}
Expand Down Expand Up @@ -104,8 +102,10 @@ func (h *Handler) HandleIncomingMessage(topic string, msg pubsub.Message, handle
case api.FlagIdentityRequest:
respond := &api.GetIdentityRespondMessage{
BaseMessage: api.BaseMessage{
Body: "",
Flag: api.FlagIdentityResponse,
Body: "",
Flag: api.FlagIdentityResponse,
FromMatrixID: h.matrixID,
To: addr.String(),
},
PeerID: h.peerID,
MatrixID: h.matrixID,
Expand Down Expand Up @@ -161,27 +161,31 @@ func (h *Handler) BlacklistPeer(pid peer.ID) {
}

// Requesting topics from **other** peers
func (h *Handler) RequestNetworkTopics(ctx context.Context) {
func (h *Handler) RequestNetworkTopics() {
requestTopicsMessage := &api.BaseMessage{
Body: "",
Flag: api.FlagTopicsRequest,
Body: "",
Flag: api.FlagTopicsRequest,
To: "",
FromMatrixID: h.matrixID,
}

h.sendMessageToServiceTopic(ctx, requestTopicsMessage)
h.sendMessageToServiceTopic(requestTopicsMessage)
}

// Requests MatrixID from other peers
func (h *Handler) RequestPeersIdentity(ctx context.Context) {
// Requests MatrixID from specific peer
func (h *Handler) RequestPeersIdentity(peerID string) {
requestPeersIdentity := &api.BaseMessage{
Body: "",
Flag: api.FlagIdentityRequest,
Body: "",
To: peerID,
Flag: api.FlagIdentityRequest,
FromMatrixID: h.matrixID,
}

h.sendMessageToServiceTopic(ctx, requestPeersIdentity)
h.sendMessageToServiceTopic(requestPeersIdentity)
}

// Sends marshaled message to the service topic
func (h *Handler) sendMessageToServiceTopic(ctx context.Context, message *api.BaseMessage) {
func (h *Handler) sendMessageToServiceTopic(message *api.BaseMessage) {
sendData, err := json.Marshal(message)
if err != nil {
log.Println(err.Error())
Expand Down

0 comments on commit 9590443

Please sign in to comment.