From 0ccac89815ee92c69fefc148cfb272faf7309136 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 20 May 2016 15:34:44 -0400 Subject: Started implementing user streams You can now start a user stream and end it yourself. If it disconnects abnormally, it will reconnect with a backoff as described by Twitter. Some data structures have some fields parsed now; tweets have IDs, text, and authors. Users have IDs, screen names, and names. Notifications from the stream are parsed completely. The ability to follow and unfollow users has also been added, as well as the ability to get a list of friends and followers, and to reply to a tweet. --- src/client.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) (limited to 'src/client.h') diff --git a/src/client.h b/src/client.h index 3a133e4..ae80ed9 100644 --- a/src/client.h +++ b/src/client.h @@ -7,6 +7,12 @@ #include #include #include +#include +#include +#include "notification.h" +#include +#include +#include namespace OAuth { class Consumer; @@ -18,21 +24,82 @@ namespace twitter { class client { public: + class stream { + public: + typedef std::function notify_callback; + + stream(client& _client); + + void setNotifyCallback(notify_callback _n); + + bool isRunning() const; + void start(); + void stop(); + + private: + enum class backoff { + none, + network, + http, + rate_limit + }; + + void run(); + int progress(); + int write(char* ptr, size_t size, size_t nmemb); + + friend int client_stream_progress_callback_wrapper(void* stream, curl_off_t, curl_off_t, curl_off_t, curl_off_t); + friend size_t client_stream_write_callback_wrapper(void* ptr, size_t size, size_t nmemb, void* stream); + + client& _client; + notify_callback _notify; + bool _stop = false; + std::thread _thread; + std::mutex _running_mutex; + std::mutex _notify_mutex; + std::mutex _stall_mutex; + std::string _buffer; + time_t _last_write; + bool _established = false; + backoff _backoff_type = backoff::none; + std::chrono::milliseconds _backoff_amount; + }; + client(const auth& _auth); ~client(); - response updateStatus(std::string msg, tweet& result, std::list media_ids = {}); + response updateStatus(std::string msg, tweet& result, tweet in_response_to = tweet(), std::list media_ids = {}); response uploadMedia(std::string media_type, const char* data, long data_length, long& media_id); + response follow(user_id toFollow); + response follow(user toFollow); + + response unfollow(user_id toUnfollow); + response unfollow(user toUnfollow); + + response getFriends(std::set& result); + response getFollowers(std::set& result); + + const user& getUser() const; + + void setUserStreamNotifyCallback(stream::notify_callback callback); + void startUserStream(); + void stopUserStream(); + private: + friend class stream; + OAuth::Consumer* _oauth_consumer; OAuth::Token* _oauth_token; OAuth::Client* _oauth_client; - bool performGet(std::string url, long& response_code, json& result); - bool performPost(std::string url, std::string dataStr, long& response_code, json& result); - bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, json& result); - response codeForError(int httpcode, json errors) const; + user _current_user; + stream _user_stream{*this}; + + bool performGet(std::string url, long& response_code, std::string& result); + bool performPost(std::string url, std::string dataStr, long& response_code, std::string& result); + bool performMultiPost(std::string url, const curl_httppost* fields, long& response_code, std::string& result); + response codeForError(int httpcode, std::string errors) const; }; }; -- cgit 1.4.1