Skip to content

Commit

Permalink
Applied clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
mredd committed Mar 31, 2021
1 parent 1aaff6c commit 86e7309
Show file tree
Hide file tree
Showing 15 changed files with 618 additions and 664 deletions.
102 changes: 44 additions & 58 deletions aricpp/arimodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,29 @@
* DEALINGS IN THE SOFTWARE.
******************************************************************************/


#ifndef ARICPP_ARIMODEL_H_
#define ARICPP_ARIMODEL_H_

#include <memory>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include "client.h"
#include "channel.h"
#include "bridge.h"
#include "channel.h"
#include "client.h"

namespace aricpp
{

/**
* @brief Provides the telephony object model on top of a asterisk connection.
*
*
* This class provides methods to create channels (class Channel) and
* register handlers for stasis and channel events.
*/
class AriModel
{
public:

using ChannelPtr = std::shared_ptr<Channel>;

explicit AriModel(Client& c) : client(c) { Subscribe(); }
Expand Down Expand Up @@ -89,12 +87,12 @@ class AriModel
/// The handler takes a unique_ptr<Bridge> to the new bridge as parameter
/// If an error occour, the unique_ptr passed is empty.
template<typename CreationHandler>
void CreateBridge(CreationHandler&& h, Bridge::Type type=Bridge::Type::mixing)
void CreateBridge(CreationHandler&& h, Bridge::Type type = Bridge::Type::mixing)
{
client.RawCmd(
Method::post,
"/ari/bridges?type=" + static_cast<std::string>(type),
[ this, h(std::forward<CreationHandler>(h)) ](auto,auto,auto,auto body)
[this, h(std::forward<CreationHandler>(h))](auto, auto, auto, auto body)
{
try
{
Expand All @@ -112,8 +110,7 @@ class AriModel
Get<std::string>(tree, {"id"}),
Get<std::string>(tree, {"technology"}),
Get<std::string>(tree, {"bridge_type"}),
&client
);
&client);
h(std::unique_ptr<Bridge>(bridge));
}
catch (const std::exception& e)
Expand All @@ -128,24 +125,21 @@ class AriModel
std::cerr << "Unknown exception in POST bridge response\n";
h({});
}
}
);
});
}


private:

void Subscribe()
{
client.OnEvent(
"ChannelCreated",
[this](const JsonTree& e)
{
auto state = Get<std::string>( e, {"channel", "state"} );
auto id = Get<std::string>( e, {"channel", "id"} );
auto state = Get<std::string>(e, {"channel", "state"});
auto id = Get<std::string>(e, {"channel", "id"});
auto findResult = channels.find(id);
ChannelPtr channel;
if ( findResult == channels.end() )
if (findResult == channels.end())
{
auto it = channels.emplace(id, std::make_shared<Channel>(client, id, state));
channel = it.first->second;
Expand All @@ -156,54 +150,50 @@ class AriModel
channel->StateChanged(state);
}
if (chCreated) chCreated(channel);
}
);
});

client.OnEvent(
"StasisStart",
[this](const JsonTree& e)
{
const std::string id = Get<std::string>( e, {"channel", "id"} );
const std::string name = Get<std::string>( e, {"channel", "name"} );
const std::string ext = Get<std::string>( e, {"channel", "dialplan", "exten"} );
const std::string callerNum = Get<std::string>( e, {"channel", "caller", "number"} );
const std::string callerName = Get<std::string>( e, {"channel", "caller", "name"} );
const auto& args = Get<std::vector<std::string>>( e, {"args"} );
const std::string id = Get<std::string>(e, {"channel", "id"});
const std::string name = Get<std::string>(e, {"channel", "name"});
const std::string ext = Get<std::string>(e, {"channel", "dialplan", "exten"});
const std::string callerNum = Get<std::string>(e, {"channel", "caller", "number"});
const std::string callerName = Get<std::string>(e, {"channel", "caller", "name"});
const auto& args = Get<std::vector<std::string>>(e, {"args"});

auto ch = channels.find(id);
if ( ch == channels.end() ) return;
if (ch == channels.end()) return;

ch->second->StasisStart(name, ext, callerNum, callerName);
if (stasisStarted) stasisStarted(ch->second, args.empty());
}
);
});
client.OnEvent(
"ChannelDestroyed",
[this](const JsonTree& e)
{
auto id = Get<std::string>( e, {"channel", "id"} );
auto cause = Get<int>( e, {"cause"} );
auto causeTxt = Get<std::string>( e, {"cause_txt"} );
auto id = Get<std::string>(e, {"channel", "id"});
auto cause = Get<int>(e, {"cause"});
auto causeTxt = Get<std::string>(e, {"cause_txt"});

auto ch = channels.find(id);
if ( ch == channels.end() ) return;
if (ch == channels.end()) return;
ch->second->Dead(cause, causeTxt);
if (chDestroyed) chDestroyed(ch->second);
channels.erase(id);
}
);
});
client.OnEvent(
"ChannelStateChange",
[this](const JsonTree& e)
{
auto id = Get<std::string>( e, {"channel", "id"} );
auto state = Get<std::string>( e, {"channel", "state"} );
auto id = Get<std::string>(e, {"channel", "id"});
auto state = Get<std::string>(e, {"channel", "state"});
auto ch = channels.find(id);
if ( ch == channels.end() ) return;
if (ch == channels.end()) return;
ch->second->StateChanged(state);
if (chStateChanged) chStateChanged(ch->second);
}
);
});

client.OnEvent(
"ChannelVarset",
Expand All @@ -215,9 +205,9 @@ class AriModel
auto value = Get<std::string>(e, {"value"});
try
{
auto chId = Get<std::string>(e, {"channel","id"});
auto chId = Get<std::string>(e, {"channel", "id"});
auto ch = channels.find(chId);
if ( ch == channels.end() ) return;
if (ch == channels.end()) return;
// channel variable
chVarSet(ch->second, variable, value);
}
Expand All @@ -226,34 +216,30 @@ class AriModel
// global variable
chVarSet(nullptr, variable, value);
}
}
);
});
client.OnEvent(
"PlaybackStarted",
[this](const JsonTree& e)
{
const std::string id = Get<std::string>( e, {"playback", "id"} );
const std::string media_uri = Get<std::string>( e, {"playback", "media_uri"} );
const std::string target_uri = Get<std::string>( e, {"playback", "target_uri"} );
const std::string language = Get<std::string>( e, {"playback", "language"} );
const std::string state = Get<std::string>( e, {"playback", "state"} );
const std::string id = Get<std::string>(e, {"playback", "id"});
const std::string media_uri = Get<std::string>(e, {"playback", "media_uri"});
const std::string target_uri = Get<std::string>(e, {"playback", "target_uri"});
const std::string language = Get<std::string>(e, {"playback", "language"});
const std::string state = Get<std::string>(e, {"playback", "state"});
if (PlaybackStarted) PlaybackStarted(Playback(id, &client));
}
);
});

client.OnEvent(
"PlaybackFinished",
[this](const JsonTree& e)
{
const std::string id = Get<std::string>( e, {"playback", "id"} );
const std::string media_uri = Get<std::string>( e, {"playback", "media_uri"} );
const std::string target_uri = Get<std::string>( e, {"playback", "target_uri"} );
const std::string language = Get<std::string>( e, {"playback", "language"} );
const std::string state = Get<std::string>( e, {"playback", "state"} );
const std::string id = Get<std::string>(e, {"playback", "id"});
const std::string media_uri = Get<std::string>(e, {"playback", "media_uri"});
const std::string target_uri = Get<std::string>(e, {"playback", "target_uri"});
const std::string language = Get<std::string>(e, {"playback", "language"});
const std::string state = Get<std::string>(e, {"playback", "state"});
if (PlaybackFinished) PlaybackFinished(Playback(id, &client));
}
);

});
}

Client& client;
Expand Down
78 changes: 38 additions & 40 deletions aricpp/basicauth.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,71 +30,69 @@
* DEALINGS IN THE SOFTWARE.
******************************************************************************/


#ifndef ARICPP_BASICAUTH_H_
#define ARICPP_BASICAUTH_H_

#include <string>
#include <array>
#include <string>

namespace aricpp
{
namespace
{
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

inline std::string Base64Encode( const std::string& toEncode )
{
std::string ret;
std::array< unsigned char, 3 > char_array_3;
std::array< unsigned char, 4 > char_array_4;
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

int i = 0;
for ( auto ch: toEncode )
{
char_array_3[i++] = ch;
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for ( auto x: char_array_4 )
ret += base64_chars[x];

i = 0;
}
}
inline std::string Base64Encode(const std::string& toEncode)
{
std::string ret;
std::array<unsigned char, 3> char_array_3;
std::array<unsigned char, 4> char_array_4;

if (i)
int i = 0;
for (auto ch : toEncode)
{
char_array_3[i++] = ch;
if (i == 3)
{
for( auto j = i; j < 3; ++j )
char_array_3[j] = '\0';

char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for ( auto j = 0; (j < i + 1); ++j )
ret += base64_chars[char_array_4[j]];
for (auto x : char_array_4)
ret += base64_chars[x];

while((i++ < 3))
ret += '=';
i = 0;
}
}

if (i)
{
for (auto j = i; j < 3; ++j)
char_array_3[j] = '\0';

return ret;
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for (auto j = 0; (j < i + 1); ++j)
ret += base64_chars[char_array_4[j]];

while ((i++ < 3))
ret += '=';
}

return ret;
}

} // anonymous namespace

inline std::string GetBasicAuth( const std::string& user, const std::string& psw )
inline std::string GetBasicAuth(const std::string& user, const std::string& psw)
{
return "Basic " + Base64Encode( user + ':' + psw );
return "Basic " + Base64Encode(user + ':' + psw);
}

} // namespace aricpp
Expand Down
Loading

0 comments on commit 86e7309

Please sign in to comment.