aboutsummaryrefslogtreecommitdiffstats
path: root/lib/aclog/receiver/collector_connection.rb
blob: c1258deda3b98d77206e3759c9c50f826751db2b (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Aclog
  module Receiver
    class CollectorConnection < EM::Connection
      def initialize(channel, connections)
        @channel = channel
        @connections = connections

        @worker_number = nil
        @unpacker = MessagePack::Unpacker.new(symbolize_keys: true)
      end

      def send_account(account)
        send_object(type: "account",
                    id: account.id,
                    consumer_key: Settings.consumer.key,
                    consumer_secret: Settings.consumer.secret,
                    oauth_token: account.oauth_token,
                    oauth_token_secret: account.oauth_token_secret,
                    user_id: account.user_id)
        log(:debug, "send: #{account.id}/#{account.user_id}")
      end

      def send_stop_account(account)
        send_object(type: "stop",
                    id: account.id)
        log(:debug, "send stop: #{account.id}/#{account.user_id}")
      end

      def post_init
      end

      def unbind
        @connections.reject! {|k, v| v == self }
        log(:info, "connection closed")
      end

      def receive_data(data)
        @unpacker.feed_each(data) do |msg|
          unless msg.is_a?(Hash) && msg[:type]
            log(:error, "unknown data: #{msg}")
            send_object(type: "fatal", message: "unknown data")
            close_connection_after_writing
            return
          end

          unless @authorized
            if msg[:type] == "auth"
              auth(msg)
            else
              log(:warn, "not authorized client: #{msg}")
              send_object(type: "fatal", message: "You aren't authorized")
              close_connection_after_writing
            end
            return
          end

          case msg[:type]
          when "unauthorized"
            @channel << -> {
              log(:warn, "unauthorized: ##{msg[:id]}/#{msg[:user_id]}")
            }
          when "tweet"
            @channel << -> {
              log(:debug, "receive tweet: #{msg[:id]}")
              Tweet.from_json(msg)
            }
          when "favorite"
            @channel << -> {
              log(:debug, "receive favorite: #{msg[:source][:id]} => #{msg[:target_object][:id]}")
              if f = Favorite.from_json(msg)
                f.tweet.notify_favorite
              end
            }
          when "unfavorite"
            @channel << -> {
              log(:debug, "receive unfavorite: #{msg[:source][:id]} => #{msg[:target_object][:id]}")
              Favorite.where(user_id: msg[:source][:id], tweet_id: msg[:target_object][:id]).destroy_all
            }
          when "retweet"
            @channel << -> {
              log(:debug, "receive retweet: #{msg[:user][:id]} => #{msg[:retweeted_status][:id]}")
              Retweet.from_json(msg)
            }
          when "delete"
            @channel << -> {
              log(:debug, "receive delete: #{msg[:id]}")
              Tweet.where(id: msg[:id]).destroy_all
              Retweet.where(id: msg[:id]).destroy_all
            }
          when "quit"
            log(:info, "receive quit: #{msg[:reason]}")
            send_data(type: "quit", message: "Bye")
            close_connection_after_writing
          else
            log(:warn, "unknown message: #{msg[:type]}")
            send_object(type: "error", message: "Unknown message type")
          end
        end
      end

      private
      def log(level, message)
        text = "[RECEIVER"
        text << ":#{@worker_number}" if @worker_number
        text << "] #{message}"
        Rails.logger.__send__(level, text)
      end

      def send_object(data)
        send_data(data.to_msgpack)
      end

      def auth(msg)
        secret_key = msg[:secret_key]
        unless secret_key == Settings.collector.secret_key
          log(:warn, "Invalid secret_key: \"#{secret_key}\"")
          send_object(type: "fatal", message: "invalid secret_key")
          close_connection_after_writing
          return
        end

        worker_number = (Settings.collector.count.times.to_a - @connections.keys).sort.first
        if worker_number == nil
          log(:warn, "all connection alive")
          send_object(type: "error", message: "all connection alive")
          close_connection_after_writing
          return
        end

        @connections[worker_number] = self
        @worker_number = worker_number
        @authorized = true
        log(:info, "connect")
        send_object(type: "ok", message: "connected")

        Account.set_of_collector(@worker_number).each do |account|
          send_account(account)
        end
      end
    end
  end
end