Skip to content

Commit

Permalink
[AMCP] #475 Added special command REQ that can be prepended before an…
Browse files Browse the repository at this point in the history
…y command to identify the response with a client specified request id, allowing a client to know exactly what asynchronous response matched a specific request.
  • Loading branch information
Helge Norberg committed Jan 17, 2017
1 parent 5754c0f commit 67621ce
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ AMCP
values.
o MIXER CHROMA syntax deprecated (still supported) in favour of the more
advanced syntax required by the rewritten chroma key code.
o Added special command REQ that can be prepended before any command to
identify the response with a client specified request id, allowing a client
to know exactly what asynchronous response matched a specific request.



Expand Down
11 changes: 10 additions & 1 deletion protocol/amcp/AMCPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace amcp {
int min_num_params_;
std::wstring name_;
std::wstring replyString_;
std::wstring request_id_;
public:
AMCPCommand(const command_context& ctx, const amcp_command_func& command, int min_num_params, const std::wstring& name)
: ctx_(ctx)
Expand Down Expand Up @@ -134,9 +135,17 @@ namespace amcp {
return name_;
}

void set_request_id(std::wstring request_id)
{
request_id_ = std::move(request_id);
}

void SetReplyString(const std::wstring& str)
{
replyString_ = str;
if (request_id_.empty())
replyString_ = str;
else
replyString_ = L"RES " + request_id_ + L" " + str;
}
};
}}}
17 changes: 17 additions & 0 deletions protocol/amcp/AMCPCommandsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2948,6 +2948,21 @@ std::wstring lock_command(command_context& ctx)
CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(L"Unknown LOCK command " + command));
}

void req_describer(core::help_sink& sink, const core::help_repository& repo)
{
sink.short_description(L"Perform any command with an additional request id identifying the response.");
sink.syntax(L"REQ [request_id:string] COMMAND...");
sink.para()
->text(L"This special command modifies the AMCP protocol a little bit to prepend ")
->code(L"RES request_id")->text(L" to the response, in order to see what asynchronous response matches what request.");
sink.para()->text(L"Examples:");
sink.example(L"REQ unique PLAY 1-0 AMB\n");
sink.example(
L">> REQ unique PLAY 1-0 AMB\n"
L"<< RES unique 202 PLAY OK");
}


void register_commands(amcp_command_repository& repo)
{
repo.register_channel_command( L"Basic Commands", L"LOADBG", loadbg_describer, loadbg_command, 1);
Expand Down Expand Up @@ -3034,6 +3049,8 @@ void register_commands(amcp_command_repository& repo)
repo.register_command( L"Query Commands", L"HELP", help_describer, help_command, 0);
repo.register_command( L"Query Commands", L"HELP PRODUCER", help_producer_describer, help_producer_command, 0);
repo.register_command( L"Query Commands", L"HELP CONSUMER", help_consumer_describer, help_consumer_command, 0);

repo.help_repo()->register_item({ L"AMCP", L"Protocol Commands" }, L"REQ", req_describer);
}

} //namespace amcp
Expand Down
25 changes: 22 additions & 3 deletions protocol/amcp/AMCPProtocolStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Author: Nicklas P Andersson
*/


#include "../StdAfx.h"

#include "AMCPProtocolStrategy.h"
Expand Down Expand Up @@ -106,7 +106,7 @@ struct AMCPProtocolStrategy::impl
void Parse(const std::wstring& message, ClientInfoPtr client)
{
CASPAR_LOG_COMMUNICATION(info) << L"Received message from " << client->address() << ": " << message << L"\\r\\n";

command_interpreter_result result;
if(interpret_command_string(message, result, client))
{
Expand All @@ -115,7 +115,7 @@ struct AMCPProtocolStrategy::impl
else
result.queue->AddCommand(result.command);
}

if (result.error != error_state::no_error)
{
std::wstringstream answer;
Expand Down Expand Up @@ -157,6 +157,22 @@ struct AMCPProtocolStrategy::impl
if (!tokens.empty() && tokens.front().at(0) == L'/')
tokens.pop_front();

std::wstring request_id;

if (boost::iequals(tokens.front(), L"REQ"))
{
tokens.pop_front();

if (tokens.empty())
{
result.error = error_state::parameters_error;
return false;
}

request_id = tokens.front();
tokens.pop_front();
}

// Fail if no more tokens.
if (tokens.empty())
{
Expand Down Expand Up @@ -234,6 +250,9 @@ struct AMCPProtocolStrategy::impl
if (result.command->parameters().size() < result.command->minimum_parameters())
result.error = error_state::parameters_error;
}

if (result.command)
result.command->set_request_id(std::move(request_id));
}
catch (std::out_of_range&)
{
Expand Down
5 changes: 5 additions & 0 deletions protocol/amcp/amcp_command_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,9 @@ void amcp_command_repository::register_channel_command(
self.channel_commands.insert(std::make_pair(std::move(name), std::make_pair(std::move(command), min_num_params)));
}

spl::shared_ptr<core::help_repository> amcp_command_repository::help_repo() const
{
return impl_->help_repo;
}

}}}
1 change: 1 addition & 0 deletions protocol/amcp/amcp_command_repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class amcp_command_repository : boost::noncopyable

void register_command(std::wstring category, std::wstring name, core::help_item_describer describer, amcp_command_func command, int min_num_params);
void register_channel_command(std::wstring category, std::wstring name, core::help_item_describer describer, amcp_command_func command, int min_num_params);
spl::shared_ptr<core::help_repository> help_repo() const;
private:
struct impl;
spl::shared_ptr<impl> impl_;
Expand Down

0 comments on commit 67621ce

Please sign in to comment.