about summary refs log tree commit diff stats
path: root/src/stream.h
blob: b682ce21b5eb903a62066e32f7f6da5979a349ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef STREAM_H_E9146952
#define STREAM_H_E9146952

#include <functional>
#include <list>
#include <string>
#include <chrono>
#include <thread>
#include "bounding_box.h"

namespace twitter {
  
  class client;
  class notification;
  
  class stream {
  public:
    
    typedef std::function<void(notification _notification)> notify_callback;
    
    stream(
      const client& tclient,
      notify_callback callback,
      bool with_followings = true,
      bool receive_all_replies = false,
      std::list<std::string> track = {},
      std::list<bounding_box> locations = {});
      
    ~stream();
    
    stream(const stream& other) = delete;
    stream(stream&& other) = delete;
    stream& operator=(const stream& other) = delete;
    stream& operator=(stream&& other) = delete;
    
  private:
    enum class backoff {
      none,
      network,
      http,
      rate_limit
    };
    
    static std::string generateUrl(
      bool with_followings,
      bool receive_all_replies,
      std::list<std::string> track,
      std::list<bounding_box> locations);
    
    void run(std::string url);
    int progress();
    size_t write(char* ptr, size_t size, size_t nmemb);
    
    const client& _client;
    notify_callback _notify;
    bool _stop = false;
    std::string _buffer;
    time_t _last_write;
    bool _established = false;
    backoff _backoff_type = backoff::none;
    std::chrono::milliseconds _backoff_amount;
    std::thread _thread;
  };
  
}

#endif /* end of include guard: STREAM_H_E9146952 */