buffet: Start XMPP immediately after registration

TEST=manually and FEATURES=test emerge-${BOARD} buffet
BUG=brillo:287

Change-Id: I26f59e2b12a6ca5b49569efd7495e920e58aefe5
Reviewed-on: https://chromium-review.googlesource.com/251045
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Nathan Bullock <nathanbullock@google.com>
Commit-Queue: Nathan Bullock <nathanbullock@google.com>
Tested-by: Nathan Bullock <nathanbullock@google.com>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index a4e30e1..01ff4fc 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -331,22 +331,39 @@
   LOG(INFO) << "Access token is refreshed for additional " << expires_in
             << " seconds.";
 
-  // If no MessageLoop assume we're in unittests.
-  if (base::MessageLoop::current()) {
-    std::unique_ptr<XmppConnection> connection(new XmppConnection());
-    CHECK(connection->Initialize()) << "Failed to connect to XMPP server";
-    xmpp_client_.reset(new XmppClient(device_robot_account_, access_token_,
-                                      std::move(connection)));
-    CHECK(base::MessageLoopForIO::current()->WatchFileDescriptor(
-        xmpp_client_->GetFileDescriptor(), true,
-        base::MessageLoopForIO::WATCH_READ, &fd_watcher_, this))
-        << "Failed to watch XMPP file descriptor";
-    xmpp_client_->StartStream();
-  }
+  StartXmpp();
 
   return true;
 }
 
+void DeviceRegistrationInfo::StartXmpp() {
+  // If no MessageLoop assume we're in unittests.
+  if (!base::MessageLoop::current()) {
+    LOG(INFO) << "No MessageLoop, not starting XMPP";
+    return;
+  }
+
+  if (!fd_watcher_.StopWatchingFileDescriptor()) {
+    LOG(WARNING) << "Failed to stop the previous watcher";
+    return;
+  }
+
+  std::unique_ptr<XmppConnection> connection(new XmppConnection());
+  if (!connection->Initialize()) {
+    LOG(WARNING) << "Failed to connect to XMPP server";
+    return;
+  }
+  xmpp_client_.reset(new XmppClient(device_robot_account_, access_token_,
+                                    std::move(connection)));
+  if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
+          xmpp_client_->GetFileDescriptor(), true /* persistent */,
+          base::MessageLoopForIO::WATCH_READ, &fd_watcher_, this)) {
+    LOG(WARNING) << "Failed to watch XMPP file descriptor";
+    return;
+  }
+  xmpp_client_->StartStream();
+}
+
 std::unique_ptr<base::DictionaryValue>
 DeviceRegistrationInfo::BuildDeviceResource(chromeos::ErrorPtr* error) {
   std::unique_ptr<base::DictionaryValue> commands =
@@ -498,6 +515,7 @@
                              base::TimeDelta::FromSeconds(expires_in);
 
   Save();
+  StartXmpp();
 
   // We're going to respond with our success immediately and we'll StartDevice
   // shortly after.