| 1 |
package transport |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"sync" |
| 8 |
|
| 9 |
"github.com/sourcegraph/jsonrpc2" |
| 10 |
) |
| 11 |
|
| 12 |
// Handler is a function that handles incoming messages. |
| 13 |
type Handler func(ctx context.Context, method string, params json.RawMessage) (any, error) |
| 14 |
|
| 15 |
// NotificationHandler is a function that handles incoming notifications. |
| 16 |
type NotificationHandler func(ctx context.Context, method string, params json.RawMessage) |
| 17 |
|
| 18 |
// Router routes incoming messages to appropriate handlers. |
| 19 |
type Router struct { |
| 20 |
mu sync.RWMutex |
| 21 |
handlers map[string]Handler |
| 22 |
notificationHandlers map[string]NotificationHandler |
| 23 |
defaultHandler Handler |
| 24 |
} |
| 25 |
|
| 26 |
// NewRouter creates a new message router. |
| 27 |
func NewRouter() *Router { |
| 28 |
return &Router{ |
| 29 |
handlers: make(map[string]Handler), |
| 30 |
notificationHandlers: make(map[string]NotificationHandler), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Handle registers a handler for a specific method. |
| 35 |
func (r *Router) Handle(method string, handler Handler) { |
| 36 |
r.mu.Lock() |
| 37 |
defer r.mu.Unlock() |
| 38 |
r.handlers[method] = handler |
| 39 |
} |
| 40 |
|
| 41 |
// HandleNotification registers a notification handler for a specific method. |
| 42 |
func (r *Router) HandleNotification(method string, handler NotificationHandler) { |
| 43 |
r.mu.Lock() |
| 44 |
defer r.mu.Unlock() |
| 45 |
r.notificationHandlers[method] = handler |
| 46 |
} |
| 47 |
|
| 48 |
// SetDefaultHandler sets the default handler for unregistered methods. |
| 49 |
func (r *Router) SetDefaultHandler(handler Handler) { |
| 50 |
r.mu.Lock() |
| 51 |
defer r.mu.Unlock() |
| 52 |
r.defaultHandler = handler |
| 53 |
} |
| 54 |
|
| 55 |
// Route routes a message to the appropriate handler. |
| 56 |
func (r *Router) Route(ctx context.Context, req *jsonrpc2.Request) (any, error) { |
| 57 |
r.mu.RLock() |
| 58 |
defer r.mu.RUnlock() |
| 59 |
|
| 60 |
// Check if it's a notification (no ID) |
| 61 |
if req.ID == (jsonrpc2.ID{}) { |
| 62 |
if handler, ok := r.notificationHandlers[req.Method]; ok { |
| 63 |
handler(ctx, req.Method, *req.Params) |
| 64 |
} |
| 65 |
return nil, nil |
| 66 |
} |
| 67 |
|
| 68 |
// It's a request |
| 69 |
if handler, ok := r.handlers[req.Method]; ok { |
| 70 |
return handler(ctx, req.Method, *req.Params) |
| 71 |
} |
| 72 |
|
| 73 |
// Use default handler if available |
| 74 |
if r.defaultHandler != nil { |
| 75 |
return r.defaultHandler(ctx, req.Method, *req.Params) |
| 76 |
} |
| 77 |
|
| 78 |
return nil, fmt.Errorf("no handler for method: %s", req.Method) |
| 79 |
} |
| 80 |
|
| 81 |
// MessageType represents the type of a JSON-RPC message. |
| 82 |
type MessageType int |
| 83 |
|
| 84 |
const ( |
| 85 |
// RequestMessage is a request that expects a response. |
| 86 |
RequestMessage MessageType = iota |
| 87 |
// NotificationMessage is a notification that doesn't expect a response. |
| 88 |
NotificationMessage |
| 89 |
// ResponseMessage is a response to a request. |
| 90 |
ResponseMessage |
| 91 |
// ErrorMessage is an error response. |
| 92 |
ErrorMessage |
| 93 |
) |
| 94 |
|
| 95 |
// ParseMessageType determines the type of a JSON-RPC message. |
| 96 |
func ParseMessageType(msg *Message) MessageType { |
| 97 |
if msg.Error != nil { |
| 98 |
return ErrorMessage |
| 99 |
} |
| 100 |
if msg.Result != nil { |
| 101 |
return ResponseMessage |
| 102 |
} |
| 103 |
if msg.ID != nil { |
| 104 |
return RequestMessage |
| 105 |
} |
| 106 |
return NotificationMessage |
| 107 |
} |