Commit d929ec32 authored by atraczyk's avatar atraczyk Committed by Nicolas Jager

dring interface: use queue of lambdas to wrap API calls

- changes the client-to-daemon interface to use a queue of lambdas
  instead of a enumerated task type parsed with a switch statement

- resulting code is: less duplicated, easier to read and modify

- better seperates the UI thread code

Change-Id: Id13defbab9ba36c3a2e51b3845de59ddd6667aaf
Reviewed-by: default avatarNicolas Jäger <nicolas.jager@savoirfairelinux.com>
parent deeeab47
......@@ -60,7 +60,7 @@ AccountsViewModel::raiseUnreadContactRequest()
newUnreadContactRequest();
}
void
Account^
AccountsViewModel::addRingAccount( std::string& alias,
std::string& ringID,
std::string& accountID,
......@@ -92,9 +92,10 @@ AccountsViewModel::addRingAccount( std::string& alias,
accountsList_->InsertAt(0, account);
contactListModels_->Insert(account->accountID_, ref new ContactListModel(account->accountID_));
accountAdded(account);
return account;
}
void
Account^
AccountsViewModel::addSipAccount( std::string& alias,
std::string& accountID,
bool active,
......@@ -121,6 +122,7 @@ AccountsViewModel::addSipAccount( std::string& alias,
accountsList_->InsertAt(0, account);
contactListModels_->Insert(account->accountID_, ref new ContactListModel(account->accountID_));
accountAdded(account);
return account;
}
void
......
......@@ -64,7 +64,7 @@ internal:
}
/* functions */
void addRingAccount(std::string& alias,
Account^ addRingAccount(std::string& alias,
std::string& ringID,
std::string& accountID,
std::string& deviceId,
......@@ -75,7 +75,7 @@ internal:
bool dhtPublicInCalls,
bool turnEnabled,
std::string& turnAddress);
void addSipAccount( std::string& alias,
Account^ addSipAccount( std::string& alias,
std::string& accountID,
bool active,
std::string& sipHostname,
......
......@@ -442,7 +442,7 @@ void RingClientUWP::MainPage::OnregistrationStateRegistered(const std::string& a
{
/* do not connect those delegates before initial registration on dht is fine.
Otherwise your going to mess with the wizard */
RingD::instance->nameRegistred += ref new RingClientUWP::NameRegistred(this, &RingClientUWP::MainPage::OnnameRegistred);
RingD::instance->nameRegistered += ref new RingClientUWP::NameRegistered(this, &RingClientUWP::MainPage::OnnameRegistred);
RingD::instance->volatileDetailsChanged += ref new RingClientUWP::VolatileDetailsChanged(this, &MainPage::OnvolatileDetailsChanged);
}
......@@ -450,7 +450,8 @@ void RingClientUWP::MainPage::OncallPlaced(Platform::String ^callId)
{
}
void RingClientUWP::MainPage::OnnameRegistred(bool status)
void
MainPage::OnnameRegistred(bool status, String ^accountId)
{
showLoadingOverlay(false, false);
}
......
......@@ -96,7 +96,7 @@ private:
void OnregistrationStateUnregistered(const std::string& accountId);
void OnregistrationStateRegistered(const std::string& accountId);
void OncallPlaced(Platform::String ^callId);
void OnnameRegistred(bool status);
void OnnameRegistred(bool status, String ^accountId);
void OnvolatileDetailsChanged(const std::string &accountId, const std::map<std::string, std::string>& details);
};
}
This diff is collapsed.
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com> *
* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -53,7 +53,6 @@ delegate void SetOverlayStatusText(String^ statusText, String^ color);
delegate void CallsListRecieved(const std::vector<std::string>& callsList);
delegate void AudioMuted(const std::string& callId, bool state);
delegate void VideoMuted(const std::string& callId, bool state);
delegate void NameRegistred(bool status);
delegate void FullScreenToggled(bool state);
delegate void WindowResized(float width, float height);
delegate void NetworkChanged();
......@@ -64,6 +63,7 @@ delegate void VolatileDetailsChanged(const std::string& accountId, const std::ma
delegate void NewBuddyNotification(const std::string& accountId, const std::string& uri, int status);
delegate void VCardUpdated(Contact^ owner);
delegate void ShareRequested();
delegate void NameRegistered(bool status, String^ accountId);
using SharedCallback = std::shared_ptr<DRing::CallbackWrapperBase>;
using namespace std::placeholders;
......@@ -206,19 +206,14 @@ internal: // why this property has to be internal and not public ?
void pauseCall(String ^ callId);
void unPauseCall(String ^ callId);
void getKnownDevices(String^ accountId);
void askToExportOnRing(String^ accountId, String^ password);
void eraseCacheFolder();
void ExportOnRing(String^ accountId, String^ password);
void updateAccount(String^ accountId);
void deleteAccount(String^ accountId);
void registerThisDevice(String^ pin, String^ archivePassword);
void getCallsList();
void killCall(String^ callId);
void switchDebug();
void muteVideo(String^ callId, bool muted);
void muteAudio(const std::string& callId, bool muted);
void subscribeBuddy(const std::string& accountId, const std::string& uri, bool flag);
void registerName(String^ accountId, String^ password, String^ username);
void registerName_new(const std::string& accountId, const std::string& password, const std::string& username);
std::map<std::string, std::string> getVolatileAccountDetails(Account^ account);
void lookUpName(const std::string& accountId, String^ name);
void lookUpAddress(const std::string& accountId, String^ address);
......@@ -230,7 +225,7 @@ internal: // why this property has to be internal and not public ?
void revokeDevice(const std::string& accountId, const std::string& password, const std::string& deviceId);
void showLoadingOverlay(String^ text, String^ color);
void hideLoadingOverlay(String^ text, String^ color, int delayInMilliseconds = 2000);
void OnaccountAdded();
void OnaccountAdded(const std::string& accountId);
void OnaccountUpdated();
void OnaccountDeleted();
......@@ -262,7 +257,6 @@ internal: // why this property has to be internal and not public ?
event CallsListRecieved^ callsListRecieved; // est implemente a la base pour regler le probleme du boutton d'appel qui est present lorsqu'un appel est en cours, mais il n'est pas utilise. Voir si ca peut servir a autre chose
event AudioMuted^ audioMuted;
event VideoMuted^ videoMuted;
event NameRegistred^ nameRegistred;
event FullScreenToggled^ fullScreenToggled;
event WindowResized^ windowResized;
event NetworkChanged^ networkChanged;
......@@ -273,90 +267,14 @@ internal: // why this property has to be internal and not public ?
event NewBuddyNotification^ newBuddyNotification;
event VCardUpdated^ vCardUpdated;
event ShareRequested^ shareRequested;
event NameRegistered^ nameRegistered;
private:
/* sub classes */
enum class Request {
None,
AddRingAccount,
AddSIPAccount,
RefuseIncommingCall,
AcceptIncommingCall,
CancelOutGoingCall,
PlaceCall,
HangUpCall,
PauseCall,
UnPauseCall,
RegisterDevice,
GetKnownDevices,
ExportOnRing,
UpdateAccount,
DeleteAccount,
GetCallsList,
KillCall,
switchDebug,
MuteVideo,
MuteAudio,
LookUpName,
LookUpAddress,
SendContactRequest,
AcceptContactRequest,
DiscardContactRequest,
RegisterName,
RemoveContact,
RevokeDevice,
SubscribeBuddy,
SendSIPMessage
};
Vector<String^>^ callIdsList_;
ref class Task
{
internal:
Task(Request r) {
request = r;
}
Task(Request r, String^ c) {
request = r;
_callId = c;
}
Task(Request r, String^ p, String^ P) {
request = r;
_pin = p;
_password = P;
}
public:
property Request request;
property String^ _alias;
property String^ _callId;
property String^ _pin;
property String^ _password; // refacto : is it safe ? are tasks destroy quickly after been used ?
property String^ _accountId;
property bool _upnp;
property String^ _sipPassword;
property String^ _sipHostname;
property String^ _sipUsername;
property bool _muted;
property String^ _registeredName; // public username
property String^ _address; // ringId
internal:
std::string _accountId_new;
std::string _payload;
std::map<std::string, std::string> _payload2;
std::string _password_new;
std::string _publicUsername_new;
std::string _callid_new;
std::string _ringId_new;
std::string _deviceId;
bool _audioMuted_new;
};
/* functions */
RingD(); // singleton
void dequeueTasks();
void InternetConnectionChanged(Platform::Object^ sender);
//CallStatus translateCallStatus(String^ state);
......@@ -366,18 +284,19 @@ private:
bool hasInternet_;
bool isAddingDevice = false;
bool isAddingAccount = false;
bool shouldRegister = false;
String^ nameToRegister = "";
bool isUpdatingAccount = false;
bool isDeletingAccount = false;
bool isInBackground_ = false;
bool isInvisible_ = false;
bool daemonInitialized_ = false;
bool daemonRunning_ = false;
bool editModeOn_ = false;
bool debugModeOn_ = true;
bool callToastPopped_ = false;
std::string localFolder_;
std::queue<Task^> tasksList_;
Utils::task_queue tasks_;
StartingStatus startingStatus_ = StartingStatus::NORMAL;
Ringtone^ ringtone_;
......
......@@ -189,11 +189,19 @@ SmartPanel::SmartPanel()
RingD::instance->newBuddyNotification += ref new RingClientUWP::NewBuddyNotification(this, &RingClientUWP::Views::SmartPanel::OnnewBuddyNotification);
RingD::instance->nameRegistered += ref new RingClientUWP::NameRegistered(this, &SmartPanel::OnnameRegistred);
_networkConnectivityNotificationRow_->Height = Utils::hasInternet() ? 0 : 32;
selectMenu(MenuOpen::CONTACTS_LIST);
}
void
SmartPanel::OnnameRegistred(bool status, String ^accountId)
{
updatePageContent();
}
void
SmartPanel::OnregistrationStateChanged(const std::string& accountId)
{
......@@ -890,6 +898,8 @@ void RingClientUWP::Views::SmartPanel::_acceptAccountModification__Click(Platfor
account->name_ = _accountAliasTextBoxEdition_->Text;
if (account->accountType_ == "RING") {
if (_RegisterStateEdition_->IsOn)
account->_username = _usernameTextBoxEdition_->Text;
account->_active = _enabledState_->IsOn;
account->_upnpState = _upnpState_->IsOn;
account->_autoAnswer = _autoAnswerToggle_->IsOn;
......@@ -911,7 +921,7 @@ void RingClientUWP::Views::SmartPanel::_acceptAccountModification__Click(Platfor
updatePageContent();
if (_usernameValidEdition_->Visibility == Windows::UI::Xaml::Visibility::Visible && _usernameTextBoxEdition_->Text->Length() > 2)
RingD::instance->registerName_new(Utils::toString(account->accountID_), "", Utils::toString(_usernameTextBoxEdition_->Text));
RingD::instance->registerName(account->accountID_, "", _usernameTextBoxEdition_->Text);
}
......@@ -1007,7 +1017,7 @@ void RingClientUWP::Views::SmartPanel::_smartList__PointerExited(Platform::Objec
void RingClientUWP::Views::SmartPanel::_registerOnBlockchainEdition__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
RingD::instance->registerName_new(Utils::toString(account->accountID_), "", Utils::toString(_usernameTextBoxEdition_->Text));
RingD::instance->registerName(account->accountID_, "", _usernameTextBoxEdition_->Text);
}
......@@ -1854,7 +1864,7 @@ void RingClientUWP::Views::SmartPanel::requestPin()
/* hide the button while we are waiting... */
_closePin_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
RingD::instance->askToExportOnRing(accountId, password);
RingD::instance->ExportOnRing(accountId, password);
}
void
......
......@@ -80,7 +80,6 @@ private:
void _cancelCallBtn__Click(Platform::Object^ sender, RoutedEventArgs^ e);
void SmartPanelItem_Grid_PointerEntered(Platform::Object^ sender, PointerRoutedEventArgs^ e);
void SmartPanelItem_Grid_PointerExited(Platform::Object^ sender, PointerRoutedEventArgs^ e);
void generateQRcode();
void _videoDeviceComboBox__SelectionChanged(Platform::Object^ sender, RoutedEventArgs^);
void _videoResolutionComboBox__SelectionChanged(Platform::Object^ sender, RoutedEventArgs^);
void _videoRateComboBox__SelectionChanged(Platform::Object^ sender, RoutedEventArgs^);
......@@ -128,6 +127,7 @@ private:
void _addAccountYes__Click(Platform::Object^ sender, RoutedEventArgs^ e);
void OnregistrationStateErrorGeneric(const std::string& accountId);
void _PINTextBox__GotFocus(Platform::Object^ sender, RoutedEventArgs^ e);
void OnnameRegistred(bool status, String ^accountId);
void OnregistrationStateRegistered(const std::string& accountId);
void OncallPlaced(Platform::String ^callId);
void OncontactDataModified(Platform::String ^account, Contact^ contact);
......
......@@ -564,6 +564,21 @@ runOnWorkerThread( std::function<void()> const& f,
}, Platform::CallbackContext::Any), priority);
}
template <typename... Args>
void
runOnWorkerThreadDelayed(int delayInMilliSeconds, std::function<void()> const& f,
WorkItemPriority priority = WorkItemPriority::Normal)
{
// duration is measured in 100-nanosecond units
TimeSpan delay;
delay.Duration = 10000 * delayInMilliSeconds;
ThreadPoolTimer^ delayTimer = ThreadPoolTimer::CreateTimer(
ref new TimerElapsedHandler([=](ThreadPoolTimer^ source)
{
f();
}), delay);
}
template <typename... Args>
void
runOnUIThread( std::function<void()> const& f,
......@@ -590,6 +605,37 @@ runOnUIThreadDelayed(int delayInMilliSeconds, std::function<void()> const& f)
}), delay);
}
class task_queue {
public:
task_queue() {}
task_queue(task_queue const& other) {}
void add_task(std::function<void()> const& task) {
std::lock_guard<std::mutex> lk(taskMutex_);
tasks_.push(task);
}
void dequeue_tasks() {
std::lock_guard<std::mutex> lk(taskMutex_);
while (!tasks_.empty()) {
auto f = tasks_.front();
f();
tasks_.pop();
}
}
void clear() {
std::lock_guard<std::mutex> lk(taskMutex_);
std::queue<std::function<void()>> empty;
std::swap(tasks_, empty);
}
private:
std::mutex taskMutex_;
mutable std::queue<std::function<void()>> tasks_;
};
namespace time
{
......@@ -637,7 +683,7 @@ dateTimeToEpoch(DateTime dateTime)
return static_cast<std::time_t>(dateTime.UniversalTime / TICKS_PER_SECOND - EPOCH_DIFFERENCE);
}
}
} /*namespace time*/
namespace xaml
{
......
......@@ -211,10 +211,6 @@
</ClInclude>
<ClInclude Include="resource.h" />
<ClInclude Include="ResourceManager.h" />
<ClInclude Include="RingConsolePanel.xaml.h">
<DependentUpon>RingConsolePanel.xaml</DependentUpon>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="RingD.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
</ClInclude>
......@@ -262,9 +258,6 @@
</Page>
<Page Include="MessageTextPage.xaml" />
<Page Include="PreviewPage.xaml" />
<Page Include="RingConsolePanel.xaml">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</Page>
<Page Include="ScrollBars.xaml">
<SubType>Designer</SubType>
</Page>
......@@ -404,10 +397,6 @@
<DependentUpon>PreviewPage.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="ResourceManager.cpp" />
<ClCompile Include="RingConsolePanel.xaml.cpp">
<DependentUpon>RingConsolePanel.xaml</DependentUpon>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="RingD.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
</ClCompile>
......
......@@ -347,9 +347,6 @@
<Page Include="MessageTextPage.xaml">
<Filter>Views</Filter>
</Page>
<Page Include="RingConsolePanel.xaml">
<Filter>Views</Filter>
</Page>
<Page Include="SmartPanel.xaml">
<Filter>Views</Filter>
</Page>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment