ConnectResult TelepathyAccountHandler::connect() { UT_DEBUGMSG(("TelepathyAccountHandler::connect()\n")); AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager(); UT_return_val_if_fail(pManager, CONNECT_FAILED); UT_return_val_if_fail(m_pTpClient == NULL, CONNECT_INTERNAL_ERROR); // inform telepathy that we can handle incoming AbiCollab tubes GError *error = NULL; TpDBusDaemon* dbus = tp_dbus_daemon_dup (&error); UT_return_val_if_fail(dbus, CONNECT_FAILED); m_pTpClient = tp_simple_handler_new(dbus, TRUE, FALSE, "AbiCollab", FALSE, handle_dbus_channel, this, NULL); tp_base_client_take_handler_filter(m_pTpClient, tp_asv_new ( TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_ROOM, TP_PROP_CHANNEL_TYPE_DBUS_TUBE_SERVICE_NAME, G_TYPE_STRING, INTERFACE, NULL ) ); if (!tp_base_client_register(m_pTpClient, &error)) { UT_DEBUGMSG(("Error registering tube handler: %s", error->message)); UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); } UT_DEBUGMSG(("Tube handler setup, listening for incoming tubes...\n")); // we are connected now, time to start sending out messages (such as events) pManager->registerEventListener(this); // signal all listeners we are logged in AccountOnlineEvent event; pManager->signal(event); return CONNECT_SUCCESS; }
ConnectResult TCPAccountHandler::connect() { UT_DEBUGMSG(("TCPAccountHandler::connect()\n")); AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager(); UT_return_val_if_fail(pManager, CONNECT_INTERNAL_ERROR); UT_return_val_if_fail(!m_pDelegator, CONNECT_INTERNAL_ERROR); UT_return_val_if_fail(!m_bConnected, CONNECT_ALREADY_CONNECTED); UT_return_val_if_fail(!m_thread, CONNECT_INTERNAL_ERROR); m_io_service.reset(); m_thread = new asio::thread(boost::bind(&asio::io_service::run, &m_io_service)); // set up the connection if (getProperty("server") == "") { UT_sint32 port = _getPort(getProperties()); UT_DEBUGMSG(("Start accepting connections on port %d...\n", port)); try { IOServerHandler* pDelegator = new IOServerHandler(port, boost::bind(&TCPAccountHandler::_handleAccept, this, _1, _2), boost::bind(&TCPAccountHandler::handleEvent, this, _1), m_io_service); m_pDelegator = pDelegator; m_bConnected = true; // todo: ask it to the acceptor pDelegator->run(); } catch (asio::system_error se) { UT_DEBUGMSG(("Failed to start accepting connections: %s\n", se.what())); _teardownAndDestroyHandler(); return CONNECT_FAILED; } catch (...) { UT_DEBUGMSG(("Caught unhandled server exception!\n")); _teardownAndDestroyHandler(); return CONNECT_FAILED; } } else { UT_DEBUGMSG(("Connecting to server %s on port %d...\n", getProperty("server").c_str(), _getPort(getProperties()))); try { asio::ip::tcp::resolver resolver(m_io_service); asio::ip::tcp::resolver::query query(getProperty("server"), getProperty("port")); asio::ip::tcp::resolver::iterator iterator(resolver.resolve(query)); bool connected = false; boost::shared_ptr<Session> session_ptr(new Session(m_io_service, boost::bind(&TCPAccountHandler::handleEvent, this, _1))); while (iterator != tcp::resolver::iterator()) { try { UT_DEBUGMSG(("Attempting to connect...\n")); session_ptr->connect(iterator); UT_DEBUGMSG(("Connected!\n")); connected = true; break; } catch (asio::system_error se) { UT_DEBUGMSG(("Connection attempt failed: %s\n", se.what())); // make sure we close the socket after a failed attempt, as it // may have been opened by the connect() call. try { session_ptr->getSocket().close(); } catch(...) {} } iterator++; } if (!connected) { UT_DEBUGMSG(("Giving up to connecting to server!\n")); _teardownAndDestroyHandler(); return CONNECT_FAILED; } session_ptr->asyncReadHeader(); m_bConnected = true; // todo: ask it to the socket // Add a buddy TCPBuddyPtr pBuddy = boost::shared_ptr<TCPBuddy>(new TCPBuddy(this, session_ptr->getRemoteAddress(), boost::lexical_cast<std::string>(session_ptr->getRemotePort()))); addBuddy(pBuddy); m_clients.insert(std::pair<TCPBuddyPtr, boost::shared_ptr<Session> >(pBuddy, session_ptr)); } catch (asio::system_error se) { UT_DEBUGMSG(("Failed to resolve %s:%d: %s\n", getProperty("server").c_str(), _getPort(getProperties()), se.what())); _teardownAndDestroyHandler(); return CONNECT_FAILED; } } if (!m_bConnected) return CONNECT_FAILED; // we are connected now, time to start sending out messages (such as events) pManager->registerEventListener(this); // signal all listeners we are logged in AccountOnlineEvent event; // TODO: fill the event AbiCollabSessionManager::getManager()->signal(event); return CONNECT_SUCCESS; }
bool XMPPAccountHandler::setup() { UT_DEBUGMSG(("XMPPAccountHandler::setup()\n")); UT_return_val_if_fail(m_pConnection, false); AbiCollabSessionManager* pManager = AbiCollabSessionManager::getManager(); UT_return_val_if_fail(pManager, false); // try to request a frame here; note that this might return 0, for example on application startup XAP_Frame *pFrame = XAP_App::getApp()->getLastFocussedFrame(); const std::string server = getProperty("server"); // Register message handler for presence messages m_pPresenceHandler = lm_message_handler_new((LmHandleMessageFunction)presence_handler, reinterpret_cast< gpointer >(this), NULL); lm_connection_register_message_handler(m_pConnection, m_pPresenceHandler, LM_MESSAGE_TYPE_PRESENCE, LM_HANDLER_PRIORITY_NORMAL); // Register message handler for stream error messages m_pStreamErrorHandler = lm_message_handler_new((LmHandleMessageFunction)stream_error_handler, reinterpret_cast< gpointer >(this), NULL); lm_connection_register_message_handler(m_pConnection, m_pStreamErrorHandler, LM_MESSAGE_TYPE_STREAM_ERROR, LM_HANDLER_PRIORITY_NORMAL); // Register message handler for chat messages m_pChatHandler = lm_message_handler_new((LmHandleMessageFunction)chat_handler, reinterpret_cast< gpointer >(this), NULL); lm_connection_register_message_handler(m_pConnection, m_pChatHandler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL); // Send presence message to server GError* error = NULL; LmMessage* m = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_PRESENCE, LM_MESSAGE_SUB_TYPE_NOT_SET); if (!lm_connection_send(m_pConnection, m, &error)) { UT_DEBUGMSG(("Presence message could not be sent: %s", error ? error->message : "")); lm_connection_close(m_pConnection, NULL); lm_connection_unref(m_pConnection); m_pConnection = NULL; // FIXME: unregister the message handlers! // ... if (pFrame) { // inform the user of the sending error // TODO: this shouldn't be here, the caller should handle this UT_UTF8String msg; // TODO: make this localizable UT_UTF8String_sprintf(msg, "Error while connecting to %s: %s\n", server.c_str(), (error ? error->message : "")); pFrame->showMessageBox(msg.utf8_str(), XAP_Dialog_MessageBox::b_O, XAP_Dialog_MessageBox::a_OK); } return false; } lm_message_unref(m); m_bLoggedIn = true; // we are connected now, time to start sending out messages (such as events) pManager->registerEventListener(this); // signal all listeners we are logged in AccountOnlineEvent event; // TODO: fill the event AbiCollabSessionManager::getManager()->signal(event); return true;; }