Nathan Bullock | bdabded | 2015-02-10 20:15:53 -0500 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BUFFET_XMPP_XMPP_CLIENT_H_ |
| 6 | #define BUFFET_XMPP_XMPP_CLIENT_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <base/macros.h> |
| 12 | |
| 13 | #include "buffet/xmpp/xmpp_connection.h" |
| 14 | |
| 15 | namespace buffet { |
| 16 | |
| 17 | class XmppClient final { |
| 18 | public: |
| 19 | // |account| is the robot account for buffet and |access_token| |
| 20 | // it the OAuth token. Note that the OAuth token expires fairly frequently |
| 21 | // so you will need to reset the XmppClient every time this happens. |
| 22 | XmppClient(const std::string& account, |
| 23 | const std::string& access_token, |
| 24 | std::unique_ptr<XmppConnection> connection) |
| 25 | : account_{account}, |
| 26 | access_token_{access_token}, |
| 27 | connection_{std::move(connection)} {} |
| 28 | |
| 29 | int GetFileDescriptor() const { |
| 30 | return connection_->GetFileDescriptor(); |
| 31 | } |
| 32 | |
| 33 | // Needs to be called when new data is available from the connection. |
Nathan Bullock | f12f7f0 | 2015-02-20 14:46:53 -0500 | [diff] [blame] | 34 | bool Read(); |
Nathan Bullock | bdabded | 2015-02-10 20:15:53 -0500 | [diff] [blame] | 35 | |
| 36 | // Start talking to the XMPP server (authenticate, etc.) |
| 37 | void StartStream(); |
| 38 | |
| 39 | // Internal states for the XMPP stream. |
| 40 | enum class XmppState { |
| 41 | kNotStarted, |
| 42 | kStarted, |
| 43 | kAuthenticationStarted, |
Nathan Bullock | f12f7f0 | 2015-02-20 14:46:53 -0500 | [diff] [blame] | 44 | kAuthenticationFailed, |
Nathan Bullock | bdabded | 2015-02-10 20:15:53 -0500 | [diff] [blame] | 45 | kStreamRestartedPostAuthentication, |
| 46 | kBindSent, |
| 47 | kSessionStarted, |
| 48 | kSubscribeStarted, |
| 49 | kSubscribed, |
| 50 | }; |
| 51 | |
| 52 | private: |
| 53 | // Robot account name for the device. |
| 54 | std::string account_; |
| 55 | |
| 56 | // OAuth access token for the account. Expires fairly frequently. |
| 57 | std::string access_token_; |
| 58 | |
| 59 | // The connection to the XMPP server. |
| 60 | std::unique_ptr<XmppConnection> connection_; |
| 61 | |
| 62 | XmppState state_{XmppState::kNotStarted}; |
| 63 | |
| 64 | friend class TestHelper; |
| 65 | DISALLOW_COPY_AND_ASSIGN(XmppClient); |
| 66 | }; |
| 67 | |
| 68 | } // namespace buffet |
| 69 | |
| 70 | #endif // BUFFET_XMPP_XMPP_CLIENT_H_ |
| 71 | |