MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / processMessage

Method processMessage

app/src/API/CommandHandler.cpp:102–148  ·  view source on GitHub ↗

* @brief Process an incoming message and generate a response */

Source from the content-addressed store, hash-verified

100 * @brief Process an incoming message and generate a response
101 */
102QByteArray API::CommandHandler::processMessage(const QByteArray& data, const CommandOrigin origin)
103{
104 QString type;
105 QJsonObject json;
106
107 if (!parseMessage(data, type, json)) {
108 return CommandResponse::makeError(
109 QString(), ErrorCode::InvalidJson, QStringLiteral("Failed to parse JSON message"))
110 .toJsonBytes();
111 }
112
113 if (type == MessageType::Command) {
114 const auto request = CommandRequest::fromJson(json);
115 if (!request.isValid()) {
116 return CommandResponse::makeError(
117 request.id, ErrorCode::InvalidMessageType, QStringLiteral("Missing 'command' field"))
118 .toJsonBytes();
119 }
120 return processCommand(request, origin).toJsonBytes();
121 }
122
123 else if (type == MessageType::Batch) {
124 const QString batchId = json.value(QStringLiteral("id")).toString();
125 const auto commandsArray = json.value(QStringLiteral("commands")).toArray();
126
127 if (commandsArray.isEmpty()) {
128 return CommandResponse::makeError(batchId,
129 ErrorCode::InvalidMessageType,
130 QStringLiteral("Empty or invalid 'commands' array"))
131 .toJsonBytes();
132 }
133
134 if (commandsArray.size() > kMaxBatchCommands) {
135 return CommandResponse::makeError(
136 batchId, ErrorCode::InvalidParam, QStringLiteral("Batch size exceeds limit"))
137 .toJsonBytes();
138 }
139
140 const auto batch = BatchRequest::fromJson(json);
141 return processBatch(batch, origin).toJsonBytes();
142 }
143
144 return CommandResponse::makeError(QString(),
145 ErrorCode::InvalidMessageType,
146 QStringLiteral("Unknown message type: %1").arg(type))
147 .toJsonBytes();
148}
149
150/**
151 * @brief Process a single command request; remote-origin device-write commands must clear the

Callers

nothing calls this directly

Calls 7

parseMessageFunction · 0.85
fromJsonFunction · 0.85
isEmptyMethod · 0.80
toJsonBytesMethod · 0.45
isValidMethod · 0.45
valueMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected