Skip to content

Commit

Permalink
Adding synchronization code to IE driver
Browse files Browse the repository at this point in the history
The IE driver can only handle one command at a time. Commands from
multithreaded client libraries should block on the second thread's
command until the first thread's executing command is complete.
  • Loading branch information
jimevans committed Dec 31, 2018
1 parent f82f8ef commit c31f68d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
16 changes: 14 additions & 2 deletions cpp/iedriver/IECommandExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,21 @@ LRESULT IECommandExecutor::OnSetCommand(UINT uMsg,
LPARAM lParam,
BOOL& bHandled) {
LOG(TRACE) << "Entering IECommandExecutor::OnSetCommand";
LRESULT set_command_result = 0;

LPCSTR json_command = reinterpret_cast<LPCSTR>(lParam);
this->current_command_.Deserialize(json_command);
return 0;
Command requested_command;
requested_command.Deserialize(json_command);

this->set_command_mutex_.lock();
if (this->current_command_.command_type() == CommandType::NoCommand ||
requested_command.command_type() == CommandType::Quit) {
this->current_command_.Deserialize(json_command);
set_command_result = 1;
}
this->set_command_mutex_.unlock();

return set_command_result;
}

LRESULT IECommandExecutor::OnExecCommand(UINT uMsg,
Expand Down Expand Up @@ -180,6 +191,7 @@ LRESULT IECommandExecutor::OnGetResponse(UINT uMsg,

// Reset the serialized response for the next command.
this->serialized_response_ = "";
this->current_command_.Reset();
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions cpp/iedriver/IECommandExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ctime>
#include <map>
#include <mutex>
#include <string>
#include <unordered_map>

Expand Down Expand Up @@ -269,6 +270,7 @@ class IECommandExecutor : public CWindowImpl<IECommandExecutor>, public IElement
bool is_valid_;
bool is_quitting_;
bool is_awaiting_new_window_;
std::mutex set_command_mutex_;

BrowserFactory* factory_;
InputManager* input_manager_;
Expand Down
15 changes: 11 additions & 4 deletions cpp/iedriver/IESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,17 @@ bool IESession::ExecuteCommand(const std::string& serialized_command,
// 3. Waiting for the response to be populated
// 4. Retrieving the response
// 5. Retrieving whether the command sent caused the session to be ready for shutdown
::SendMessage(this->executor_window_handle_,
WD_SET_COMMAND,
NULL,
reinterpret_cast<LPARAM>(serialized_command.c_str()));
LRESULT set_command_result = ::SendMessage(this->executor_window_handle_,
WD_SET_COMMAND,
NULL,
reinterpret_cast<LPARAM>(serialized_command.c_str()));
while (set_command_result == 0) {
::Sleep(500);
set_command_result = ::SendMessage(this->executor_window_handle_,
WD_SET_COMMAND,
NULL,
reinterpret_cast<LPARAM>(serialized_command.c_str()));
}
::PostMessage(this->executor_window_handle_,
WD_EXEC_COMMAND,
NULL,
Expand Down
14 changes: 14 additions & 0 deletions cpp/webdriver-server/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,18 @@ std::string Command::Serialize() {
return output;
}

void Command::Copy(const Command& source) {
this->command_type_ = source.command_type_;
this->command_parameters_ = source.command_parameters_;
this->is_valid_parameters_ = source.is_valid_parameters_;
this->session_id_ = source.session_id_;
}

void Command::Reset() {
this->command_type_ = CommandType::NoCommand;
this->session_id_ = "";
this->command_parameters_.clear();
this->is_valid_parameters_ = false;
}

} // namespace webdriver
2 changes: 2 additions & 0 deletions cpp/webdriver-server/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Command {
virtual ~Command(void);
std::string Serialize(void);
void Deserialize(const std::string& json);
void Copy(const Command& source);
void Reset(void);

std::string command_type(void) const { return this->command_type_; }
bool is_valid_parameters(void) const { return this->is_valid_parameters_; }
Expand Down

0 comments on commit c31f68d

Please sign in to comment.