Skip to content

Commit

Permalink
feat(core): add post message internal func
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanshkc committed Jul 23, 2022
1 parent 6812294 commit 77cc2ba
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/constants/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ const (
CodeOK = "OK"
// CodeOffline is the failure code for offline clients.
CodeOffline = "OFFLINE"
// CodeNoBridge is the failure code when the intended bridge cannot be located.
CodeNoBridge = "NO_BRIDGE"
// CodeBridgeNotFound is the failure code when the intended bridge cannot be located.
CodeBridgeNotFound = "BRIDGE_NOT_FOUND"
)
75 changes: 74 additions & 1 deletion src/core/func_internal_post_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,85 @@ package core
import (
"context"

"github.com/shivanshkc/rosenbridge/src/core/constants"
"github.com/shivanshkc/rosenbridge/src/core/deps"
"github.com/shivanshkc/rosenbridge/src/core/models"
"github.com/shivanshkc/rosenbridge/src/utils/errutils"
"github.com/shivanshkc/rosenbridge/src/utils/httputils"
)

// PostMessageInternal is invoked by another cluster node. Its role is to send messages to the bridges that are hosted
// under this node.
//
// nolint:funlen // Core functions are big.
func PostMessageInternal(ctx context.Context, params *models.OutgoingMessageInternalReq,
) (*models.OutgoingMessageInternalRes, error) {
panic("implement me")
// Getting the dependencies.
resolver, bridgeDB, bridgeMG := deps.DepManager.GetDiscoveryAddressResolver(),
deps.DepManager.GetBridgeDatabase(),
deps.DepManager.GetBridgeManager()

// This slice will hold all bridges IDs that are to be deleted from the database.
var staleBridgeIDs []string

// The response that will be finally sent.
response := &models.OutgoingMessageInternalRes{
CodeAndReason: &models.CodeAndReason{Code: constants.CodeOK},
Bridges: nil,
}

// Looping over all bridge IDs to deliver the messages.
for _, bridgeID := range params.BridgeIDs {
// Creating the bridge info to add in the response.
bridgeII := &models.BridgeIdentityInfo{ClientID: "", BridgeID: bridgeID}
bridgeInfo := &models.BridgeInfo{BridgeIdentityInfo: bridgeII, NodeAddr: resolver.Read()}

// Getting the concerned bridge.
bridge := bridgeMG.GetBridge(ctx, bridgeID)
if bridge == nil {
// This bridge record will be deleted.
staleBridgeIDs = append(staleBridgeIDs, bridgeID)
// Updating the response for success.
response.Bridges = append(response.Bridges, &models.BridgeStatus{
BridgeInfo: bridgeInfo,
CodeAndReason: &models.CodeAndReason{Code: constants.CodeBridgeNotFound},
})
// Continuing with the next bridge ID.
continue
}

// Forming the bridge message that will be sent over the bridge.
message := &models.BridgeMessage{
Type: constants.MessageIncomingReq,
RequestID: httputils.GetReqCtx(ctx).ID,
Body: &models.IncomingMessageReq{SenderID: params.SenderID, Message: params.Message},
}

// Sending the message and updating the response accordingly.
if err := bridge.SendMessage(message); err != nil {
// Converting the error to HTTP error to get the code and reason.
errHTTP := errutils.ToHTTPError(err)
// Updating the response as per the error.
response.Bridges = append(response.Bridges, &models.BridgeStatus{
BridgeInfo: bridgeInfo,
CodeAndReason: &models.CodeAndReason{Code: errHTTP.Code, Reason: errHTTP.Reason},
})
// Continuing with the next bridge ID.
continue
}

// Updating the response for success.
response.Bridges = append(response.Bridges, &models.BridgeStatus{
BridgeInfo: bridgeInfo,
CodeAndReason: &models.CodeAndReason{Code: constants.CodeOK},
})
}

// Deleting the stale database records without blocking.
go func() {
// TODO: Log the error without importing the src/logger dependency.
_ = bridgeDB.DeleteBridgesForNode(ctx, staleBridgeIDs, resolver.Read())
}()

return response, nil
}
10 changes: 10 additions & 0 deletions src/core/models/access.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package models

// IncomingMessageReq represents an incoming message for a client.
//
// It is called "incoming message request" because the naming is done from the client's perspective.
type IncomingMessageReq struct {
// SenderID is the ID of the client who sent the message.
SenderID string `json:"sender_id"`
// Message is the main message content.
Message string `json:"message"`
}

// OutgoingMessageReq represents a request from a client to send a message.
//
// It is called "outgoing message request" because the naming is done from the client's perspective.
Expand Down

0 comments on commit 77cc2ba

Please sign in to comment.