clang format, based on cpprest

This commit is contained in:
Benjamin Sergeant
2019-05-30 08:46:50 -07:00
parent 879a4b38aa
commit c65fec7271
47 changed files with 426 additions and 414 deletions

View File

@ -6,14 +6,15 @@
#pragma once
#include <memory>
#include <functional>
#include <memory>
namespace ix
{
class Socket;
class RedisClient {
class RedisClient
{
public:
using OnRedisSubscribeResponseCallback = std::function<void(const std::string&)>;
using OnRedisSubscribeCallback = std::function<void(const std::string&)>;
@ -21,15 +22,11 @@ namespace ix
RedisClient() = default;
~RedisClient() = default;
bool connect(const std::string& hostname,
int port);
bool connect(const std::string& hostname, int port);
bool auth(const std::string& password,
std::string& response);
bool auth(const std::string& password, std::string& response);
bool publish(const std::string& channel,
const std::string& message,
std::string& errMsg);
bool publish(const std::string& channel, const std::string& message, std::string& errMsg);
bool subscribe(const std::string& channel,
const OnRedisSubscribeResponseCallback& responseCallback,
@ -40,5 +37,4 @@ namespace ix
std::shared_ptr<Socket> _socket;
};
}
} // namespace ix

View File

@ -6,11 +6,10 @@
#pragma once
#include <ixwebsocket/IXHttpClient.h>
#include <jsoncpp/json/json.h>
#include <regex>
#include <ixwebsocket/IXHttpClient.h>
namespace ix
{
class SentryClient

View File

@ -6,16 +6,15 @@
#pragma once
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
#include <jsoncpp/json/json.h>
#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <unordered_map>
#include <memory>
#include <jsoncpp/json/json.h>
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
namespace ix
{
@ -72,8 +71,7 @@ namespace ix
/// Publish a message to a channel
///
/// No-op if the connection is not established
bool publish(const Json::Value& channels,
const Json::Value& msg);
bool publish(const Json::Value& channels, const Json::Value& msg);
// Subscribe to a channel, and execute a callback when an incoming
// message arrives.
@ -126,8 +124,7 @@ namespace ix
const std::string& errorMsg = std::string(),
const WebSocketHttpHeaders& headers = WebSocketHttpHeaders(),
const std::string& subscriptionId = std::string());
void invokeErrorCallback(const std::string& errorMsg,
const std::string& serializedPdu);
void invokeErrorCallback(const std::string& errorMsg, const std::string& serializedPdu);
///
/// Member variables

View File

@ -7,12 +7,10 @@
#pragma once
#include "IXCobraMetricsThreadedPublisher.h"
#include <chrono>
#include <jsoncpp/json/json.h>
#include <string>
#include <unordered_map>
#include <chrono>
namespace ix
{
@ -28,10 +26,11 @@ namespace ix
/// to make shouldPush as fast as possible. _enabled default to false.
///
/// The code that set those is ran only once at init, and
/// the last value to be set is _enabled, which is also the first value checked in shouldPush,
/// so there shouldn't be any race condition.
/// the last value to be set is _enabled, which is also the first value checked in
/// shouldPush, so there shouldn't be any race condition.
///
/// 2. The queue of messages is thread safe, so multiple metrics can be safely pushed on multiple threads
/// 2. The queue of messages is thread safe, so multiple metrics can be safely pushed on
/// multiple threads
///
/// 3. Access to _last_update is protected as it needs to be read/write.
///
@ -62,40 +61,44 @@ namespace ix
void push(const std::string& id,
const CobraMetricsPublisher::Message& data = CobraMetricsPublisher::Message());
/// Richer interface using json, which supports types (bool, int, float) and hierarchies of elements
/// Richer interface using json, which supports types (bool, int, float) and hierarchies of
/// elements
///
/// The shouldPushTest argument should be set to false, and used in combination with the shouldPush method
/// for places where we want to be as lightweight as possible when collecting metrics. When set to false,
/// it is used so that we don't do double work when computing whether a metrics should be sent or not.
void push(const std::string& id,
const Json::Value& data,
bool shouldPushTest = true);
/// The shouldPushTest argument should be set to false, and used in combination with the
/// shouldPush method for places where we want to be as lightweight as possible when
/// collecting metrics. When set to false, it is used so that we don't do double work when
/// computing whether a metrics should be sent or not.
void push(const std::string& id, const Json::Value& data, bool shouldPushTest = true);
/// Interface used by lua. msg is a json encoded string.
void push(const std::string& id,
const std::string& data,
bool shouldPushTest = true);
void push(const std::string& id, const std::string& data, bool shouldPushTest = true);
/// Tells whether a metric can be pushed.
/// A metric can be pushed if it satisfies those conditions:
///
/// 1. the metrics system should be enabled
/// 2. the metrics shouldn't be black-listed
/// 3. the metrics shouldn't have reached its rate control limit at this "sampling"/"calling" time
/// 3. the metrics shouldn't have reached its rate control limit at this
/// "sampling"/"calling" time
bool shouldPush(const std::string& id) const;
/// Get generic information json object
Json::Value& getGenericAttributes();
/// Set generic information values
void setGenericAttributes(const std::string& attrName,
const Json::Value& value);
void setGenericAttributes(const std::string& attrName, const Json::Value& value);
/// Set a unique id for the session. A uuid can be used.
void setSession(const std::string& session) { _session = session; }
void setSession(const std::string& session)
{
_session = session;
}
/// Get the unique id used to identify the current session
const std::string& getSession() const { return _session; }
const std::string& getSession() const
{
return _session;
}
/// Return the number of milliseconds since the epoch (~1970)
uint64_t getMillisecondsSinceEpoch() const;
@ -117,7 +120,6 @@ namespace ix
bool isAuthenticated() const;
private:
/// Lookup an id in our metrics to see whether it is blacklisted
/// Complexity is logarithmic
bool isMetricBlacklisted(const std::string& id) const;
@ -150,15 +152,16 @@ namespace ix
/// Metrics control (black list + rate control)
std::vector<std::string> _blacklist;
std::unordered_map<std::string, int> _rate_control;
std::unordered_map<std::string, std::chrono::time_point<std::chrono::steady_clock>> _last_update;
std::unordered_map<std::string, std::chrono::time_point<std::chrono::steady_clock>>
_last_update;
mutable std::mutex _last_update_mutex; // protect access to _last_update
// const strings for internal ids
static const std::string kSetRateControlId;
static const std::string kSetBlacklistId;
/// Our protocol version. Can be used by subscribers who would want to be backward compatible
/// if we change the way we arrange data
/// Our protocol version. Can be used by subscribers who would want to be backward
/// compatible if we change the way we arrange data
static const int kVersion;
};

View File

@ -7,16 +7,14 @@
#pragma once
#include "IXCobraConnection.h"
#include <jsoncpp/json/json.h>
#include <string>
#include <queue>
#include <mutex>
#include <thread>
#include <map>
#include <atomic>
#include <condition_variable>
#include <jsoncpp/json/json.h>
#include <map>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
namespace ix
{
@ -67,8 +65,7 @@ namespace ix
};
/// Push a message to be processed by the background thread
void pushMessage(MessageKind messageKind,
const Json::Value& msg);
void pushMessage(MessageKind messageKind, const Json::Value& msg);
/// Get a wait time which is increasing exponentially based on the number of retries
uint64_t getWaitTimeExp(int retry_count);

View File

@ -3,20 +3,19 @@
namespace ix
{
class IXCoreLogger
{
public:
using LogFunc = std::function<void(const char*)>;
static void Log(const char* msg);
class IXCoreLogger
{
public:
using LogFunc = std::function<void(const char*)>;
static void Log(const char* msg);
static void setLogFunction(LogFunc& func)
{
_currentLogger = func;
}
static void setLogFunction(LogFunc& func) {
_currentLogger = func;
}
private:
static LogFunc _currentLogger;
};
private:
static LogFunc _currentLogger;
};
} // ix
} // namespace ix

View File

@ -12,4 +12,4 @@ namespace ix
{
std::string base64_encode(const std::string& data, size_t len);
std::string base64_decode(const std::string& encoded_string);
}
} // namespace ix

View File

@ -13,4 +13,3 @@ namespace ix
{
uint64_t djb2Hash(const std::vector<uint8_t>& data);
}

View File

@ -9,9 +9,9 @@
namespace ix
{
/**
* Generate a random uuid
*/
std::string uuid4();
/**
* Generate a random uuid
*/
std::string uuid4();
}
} // namespace ix

View File

@ -6,11 +6,10 @@
#pragma once
#include "nlohmann/json.hpp"
#include <string>
#include <vector>
#include "nlohmann/json.hpp"
namespace snake
{
struct AppConfig
@ -31,16 +30,11 @@ namespace snake
bool verbose;
};
bool isAppKeyValid(
const AppConfig& appConfig,
std::string appkey);
bool isAppKeyValid(const AppConfig& appConfig, std::string appkey);
std::string getRoleSecret(
const AppConfig& appConfig,
std::string appkey,
std::string role);
std::string getRoleSecret(const AppConfig& appConfig, std::string appkey, std::string role);
std::string generateNonce();
void dumpConfig(const AppConfig& appConfig);
}
} // namespace snake

View File

@ -6,27 +6,47 @@
#pragma once
#include <string>
#include <future>
#include <ixwebsocket/IXConnectionState.h>
#include "IXRedisClient.h"
#include <future>
#include <ixwebsocket/IXConnectionState.h>
#include <string>
namespace snake
{
class SnakeConnectionState : public ix::ConnectionState
{
public:
std::string getNonce() { return _nonce; }
void setNonce(const std::string& nonce) { _nonce = nonce; }
std::string getNonce()
{
return _nonce;
}
void setNonce(const std::string& nonce)
{
_nonce = nonce;
}
std::string appkey() { return _appkey; }
void setAppkey(const std::string& appkey) { _appkey = appkey; }
std::string appkey()
{
return _appkey;
}
void setAppkey(const std::string& appkey)
{
_appkey = appkey;
}
std::string role() { return _role; }
void setRole(const std::string& role) { _role = role; }
std::string role()
{
return _role;
}
void setRole(const std::string& role)
{
_role = role;
}
ix::RedisClient& redisClient() { return _redisClient; }
ix::RedisClient& redisClient()
{
return _redisClient;
}
std::future<void> fut;
@ -37,4 +57,4 @@ namespace snake
ix::RedisClient _redisClient;
};
}
} // namespace snake

View File

@ -18,9 +18,8 @@ namespace snake
class SnakeConnectionState;
struct AppConfig;
void processCobraMessage(
std::shared_ptr<SnakeConnectionState> state,
std::shared_ptr<ix::WebSocket> ws,
const AppConfig& appConfig,
const std::string& str);
}
void processCobraMessage(std::shared_ptr<SnakeConnectionState> state,
std::shared_ptr<ix::WebSocket> ws,
const AppConfig& appConfig,
const std::string& str);
} // namespace snake

View File

@ -6,10 +6,9 @@
#pragma once
#include <string>
#include <ixwebsocket/IXWebSocketServer.h>
#include "IXAppConfig.h"
#include <ixwebsocket/IXWebSocketServer.h>
#include <string>
namespace snake
{
@ -27,4 +26,4 @@ namespace snake
AppConfig _appConfig;
ix::WebSocketServer _server;
};
}
} // namespace snake

12
ws/ws.h
View File

@ -28,17 +28,13 @@ namespace ix
int ws_broadcast_server_main(int port, const std::string& hostname);
int ws_transfer_main(int port, const std::string& hostname);
int ws_chat_main(const std::string& url,
const std::string& user);
int ws_chat_main(const std::string& url, const std::string& user);
int ws_connect_main(const std::string& url, bool disableAutomaticReconnection);
int ws_receive_main(const std::string& url,
bool enablePerMessageDeflate,
int delayMs);
int ws_receive_main(const std::string& url, bool enablePerMessageDeflate, int delayMs);
int ws_send_main(const std::string& url,
const std::string& path);
int ws_send_main(const std::string& url, const std::string& path);
int ws_redis_publish_main(const std::string& hostname,
int port,
@ -95,4 +91,4 @@ namespace ix
const std::string& redisPassword,
bool verbose,
const std::string& appsConfigPath);
}
} // namespace ix