int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "Invalid parameters" << std::endl; return -1; } SimpleEventLoop eventLoop; PlatformDNSSDQuerierFactory factory(&eventLoop); std::shared_ptr<DNSSDQuerier> querier = factory.createQuerier(); querier->start(); if (std::string(argv[1]) == "browse") { std::shared_ptr<DNSSDBrowseQuery> browseQuery = querier->createBrowseQuery(); browseQuery->startBrowsing(); eventLoop.run(); browseQuery->stopBrowsing(); } else if (std::string(argv[1]) == "resolve-service") { if (argc < 5) { std::cerr << "Invalid parameters" << std::endl; return -1; } std::shared_ptr<DNSSDResolveServiceQuery> resolveQuery = querier->createResolveServiceQuery(DNSSDServiceID(argv[2], argv[3], argv[4])); resolveQuery->start(); eventLoop.run(); std::cerr << "Done running" << std::endl; resolveQuery->stop(); } querier->stop(); }
int main(int, char**) { SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories(&eventLoop); EchoBot bot(&networkFactories); eventLoop.run(); return 0; }
int main() { SimpleEventLoop eventLoop; SimpleUserRegistry userRegistry; userRegistry.addUser(JID("remko@localhost"), "remko"); userRegistry.addUser(JID("kevin@localhost"), "kevin"); userRegistry.addUser(JID("*****@*****.**"), "remko"); userRegistry.addUser(JID("*****@*****.**"), "kevin"); Server server(&userRegistry, &eventLoop); eventLoop.run(); return 0; }
int main(int, char**) { SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories(&eventLoop); Client client("*****@*****.**", "mypass", &networkFactories); client.connect(); eventLoop.run(); return 0; }
void testPost() { SimpleEventLoop testling; testling.postEvent(boost::bind(&EventLoopTest::logEvent, this, 1)); testling.postEvent(boost::bind(&EventLoopTest::logEvent, this, 2)); testling.stop(); testling.run(); CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(events_.size())); CPPUNIT_ASSERT_EQUAL(1, events_[0]); CPPUNIT_ASSERT_EQUAL(2, events_[1]); }
int main(int, char**) { SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories(&eventLoop); client = new Client("*****@*****.**", "mypass", &networkFactories); client->onConnected.connect(&handleConnected); client->onMessageReceived.connect(bind(&handleMessageReceived, _1)); client->connect(); eventLoop.run(); delete client; return 0; }
int main(int, char**) { char* jid = getenv("SWIFT_CLIENTTEST_JID"); if (!jid) { std::cerr << "Please set the SWIFT_CLIENTTEST_JID environment variable" << std::endl; return -1; } char* pass = getenv("SWIFT_CLIENTTEST_PASS"); if (!pass) { std::cerr << "Please set the SWIFT_CLIENTTEST_PASS environment variable" << std::endl; return -1; } client = new Swift::Client(JID(jid), std::string(pass), &networkFactories); ClientXMLTracer* tracer = new ClientXMLTracer(client); client->onConnected.connect(&handleConnected); client->setAlwaysTrustCertificates(); client->connect(); { Timer::ref timer = networkFactories.getTimerFactory()->createTimer(60000); timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); timer->start(); eventLoop.run(); } delete tracer; delete client; return !rosterReceived; }
static int sluift_client_get_contacts(lua_State *L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); Lua::Table contactsTable; foreach(const XMPPRosterItem& item, client->getRoster()) { std::string subscription; switch(item.getSubscription()) { case RosterItemPayload::None: subscription = "none"; break; case RosterItemPayload::To: subscription = "to"; break; case RosterItemPayload::From: subscription = "from"; break; case RosterItemPayload::Both: subscription = "both"; break; case RosterItemPayload::Remove: subscription = "remove"; break; } Lua::Value groups(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end())); Lua::Table itemTable = boost::assign::map_list_of ("jid", boost::make_shared<Lua::Value>(item.getJID().toString())) ("name", boost::make_shared<Lua::Value>(item.getName())) ("subscription", boost::make_shared<Lua::Value>(subscription)) ("groups", boost::make_shared<Lua::Value>(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end()))); contactsTable[item.getJID().toString()] = boost::make_shared<Lua::Value>(itemTable); } pushValue(L, contactsTable); return 1; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
static int sluift_sleep(lua_State *L) { try { eventLoop.runOnce(); int timeout = luaL_checknumber(L, 1); Watchdog watchdog(timeout, networkFactories.getTimerFactory()); while (!watchdog.getTimedOut()) { Swift::sleep(std::min(100, timeout)); eventLoop.runOnce(); } return 0; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
void waitConnected() { Watchdog watchdog(globalTimeout, networkFactories.getTimerFactory()); while (!watchdog.getTimedOut() && client->isActive() && !client->isAvailable()) { eventLoop.runUntilEvents(); } if (watchdog.getTimedOut()) { client->disconnect(); throw SluiftException("Timeout while connecting"); } }
std::vector<XMPPRosterItem> getRoster() { if (!rosterReceived) { // If we haven't requested it yet, request it for the first time client->requestRoster(); } while (!rosterReceived) { eventLoop.runUntilEvents(); } return client->getRoster()->getItems(); }
void handleConnected() { boost::shared_ptr<Message> message(new Message()); message->setBody(messageBody); message->setTo(recipient); client->sendMessage(message); exitCode = 0; errorConnection.disconnect(); client->disconnect(); eventLoop.stop(); }
static int sluift_client_cancel_subscription(lua_State* L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); JID jid(luaL_checkstring(L, 2)); client->getClient()->getSubscriptionManager()->cancelSubscription(jid); return 0; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
int main(int, char**) { // Set up the event loop and network classes SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories(&eventLoop); Client client("*****@*****.**", "mypass", &networkFactories); client.setAlwaysTrustCertificates(); client.onConnected.connect([&] { std::cout << "Connected" << std::endl; }); client.onMessageReceived.connect([&] (Message::ref message) { message->setTo(message->getFrom()); message->setFrom(JID()); client.sendMessage(message); }); client.connect(); eventLoop.run(); return 0; }
static int sluift_client_send(lua_State *L) { try { eventLoop.runOnce(); getClient(L)->getClient()->sendData(std::string(luaL_checkstring(L, 2))); lua_pushvalue(L, 1); return 0; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
int main(int argc, char **argv) { #ifdef WIN32 WSADATA wsa_data; WSAStartup(0x0201, &wsa_data); #endif try { SimpleEventLoop loop; //1. listen on PORT HelloWorldListener tcp_listener( &loop); tcp_listener.start_listen_on_tcp( CString("0.0.0.0:%d", PORT).c_str() ); //2. also we handle ctrl-C QuitSignalHandler control_c_handler(&loop); control_c_handler.start_handle_signal( SIGINT ); //3. also listen on a unix domain socket HelloWorldListener un_listener( &loop); un_listener.start_listen_on_un( ".haha" ); //4. the main loop loop.run(); printf("done\n"); } catch (std::exception& ex) { LOG_ERROR("Exception in main(): %s\n", ex.what()); return 1; } catch (...) { LOG_ERROR("Unknown exception in main()\n"); return 1; } return 0; }
static int sluift_client_remove_contact(lua_State* L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); JID jid(luaL_checkstring(L, 2)); RosterPayload::ref roster = boost::make_shared<RosterPayload>(); roster->addItem(RosterItemPayload(JID(luaL_checkstring(L, 2)), "", RosterItemPayload::Remove)); ResponseSink<RosterPayload> sink; SetRosterRequest::ref request = SetRosterRequest::create(roster, client->getClient()->getIQRouter()); boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); request->send(); while (!sink.hasResponse()) { eventLoop.runUntilEvents(); } lua_pushboolean(L, !sink.getResponseError()); return 1; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
static int sluift_client_get_version(lua_State *L) { try { SluiftClient* client = getClient(L); int timeout = -1; if (lua_type(L, 3) != LUA_TNONE) { timeout = luaL_checknumber(L, 3); } ResponseSink<SoftwareVersion> sink; GetSoftwareVersionRequest::ref request = GetSoftwareVersionRequest::create(std::string(luaL_checkstring(L, 2)), client->getClient()->getIQRouter()); boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); request->send(); Watchdog watchdog(timeout, networkFactories.getTimerFactory()); while (!watchdog.getTimedOut() && !sink.hasResponse()) { eventLoop.runUntilEvents(); } ErrorPayload::ref error = sink.getResponseError(); if (error || watchdog.getTimedOut()) { lua_pushnil(L); if (watchdog.getTimedOut()) { lua_pushstring(L, "Timeout"); } else if (error->getCondition() == ErrorPayload::RemoteServerNotFound) { lua_pushstring(L, "Remote server not found"); } // TODO else { lua_pushstring(L, "Error"); } return 2; } else if (SoftwareVersion::ref version = sink.getResponsePayload()) { Lua::Table result = boost::assign::map_list_of ("name", boost::make_shared<Lua::Value>(version->getName())) ("version", boost::make_shared<Lua::Value>(version->getVersion())) ("os", boost::make_shared<Lua::Value>(version->getOS())); Lua::pushValue(L, result); } else { lua_pushnil(L); } return 1; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
static int sluift_client_get_next_event(lua_State *L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); int timeout = -1; if (lua_type(L, 2) != LUA_TNONE) { timeout = lua_tonumber(L, 2); } pushEvent(L, client->getNextEvent(timeout)); return 1; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
boost::optional<std::string> sendQuery(const JID& jid, IQ::Type type, const std::string& data, int timeout) { rawRequestResponse.reset(); RawRequest::ref request = RawRequest::create(type, jid, data, client->getIQRouter()); boost::signals::scoped_connection c = request->onResponse.connect(boost::bind(&SluiftClient::handleRawRequestResponse, this, _1)); request->send(); Watchdog watchdog(timeout, networkFactories.getTimerFactory()); while (!watchdog.getTimedOut() && !rawRequestResponse) { eventLoop.runUntilEvents(); } if (watchdog.getTimedOut()) { return boost::optional<std::string>(); } else { return *rawRequestResponse; } }
int main(int argc, char* argv[]) { if (argc < 5 || argc > 6) { std::cerr << "Usage: " << argv[0] << " <jid> [<connect_host>]<password> <recipient> <message>" << std::endl; return -1; } int argi = 1; std::string jid = argv[argi++]; std::string connectHost = ""; if (argc == 6) { connectHost = argv[argi++]; } client = new Swift::Client(JID(jid), std::string(argv[argi++]), &networkFactories); client->setAlwaysTrustCertificates(); recipient = JID(argv[argi++]); messageBody = std::string(argv[argi++]); ClientXMLTracer* tracer = new ClientXMLTracer(client); client->onConnected.connect(&handleConnected); errorConnection = client->onDisconnected.connect(&handleDisconnected); if (!connectHost.empty()) { client->connect(connectHost); } else { client->connect(); } { Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); timer->start(); eventLoop.run(); } delete tracer; delete client; return exitCode; }
static int sluift_client_set_version(lua_State *L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); luaL_checktype(L, 2, LUA_TTABLE); lua_getfield(L, 2, "name"); const char* rawName = lua_tostring(L, -1); lua_getfield(L, 2, "version"); const char* rawVersion = lua_tostring(L, -1); lua_getfield(L, 2, "os"); const char* rawOS = lua_tostring(L, -1); client->setSoftwareVersion(rawName ? rawName : "", rawVersion ? rawVersion : "", rawOS ? rawOS : ""); lua_pop(L, 3); lua_pushvalue(L, 1); return 1; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
Stanza::ref getNextEvent(int timeout) { if (!pendingEvents.empty()) { Stanza::ref event = pendingEvents.front(); pendingEvents.pop_front(); return event; } Watchdog watchdog(timeout, networkFactories.getTimerFactory()); while (!watchdog.getTimedOut() && pendingEvents.empty() && !client->isActive()) { eventLoop.runUntilEvents(); } if (watchdog.getTimedOut() || !client->isActive()) { return Stanza::ref(); } else if (!pendingEvents.empty()) { Stanza::ref event = pendingEvents.front(); pendingEvents.pop_front(); return event; } else { return Stanza::ref(); } }
int main(int argc, char* argv[]) { if (argc != 5) { std::cerr << "Usage: " << argv[0] << " <jid> <password> <recipient> <file>" << std::endl; return -1; } //Log::setLogLevel(Log::debug); JID sender(argv[1]); JID recipient(argv[3]); FileSender fileSender(sender, std::string(argv[2]), recipient, boost::filesystem::path(argv[4])); fileSender.start(); { Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); timer->start(); eventLoop.run(); } return exitCode; }
void testRemove() { SimpleEventLoop testling; boost::shared_ptr<MyEventOwner> eventOwner1(new MyEventOwner()); boost::shared_ptr<MyEventOwner> eventOwner2(new MyEventOwner()); testling.postEvent(boost::bind(&EventLoopTest::logEvent, this, 1), eventOwner1); testling.postEvent(boost::bind(&EventLoopTest::logEvent, this, 2), eventOwner2); testling.postEvent(boost::bind(&EventLoopTest::logEvent, this, 3), eventOwner1); testling.postEvent(boost::bind(&EventLoopTest::logEvent, this, 4), eventOwner2); testling.removeEventsFromOwner(eventOwner2); testling.stop(); testling.run(); CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(events_.size())); CPPUNIT_ASSERT_EQUAL(1, events_[0]); CPPUNIT_ASSERT_EQUAL(3, events_[1]); }
static int sluift_client_for_event(lua_State *L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); luaL_checktype(L, 2, LUA_TFUNCTION); int timeout = -1; if (lua_type(L, 3) != LUA_TNONE) { timeout = lua_tonumber(L, 3); } while (true) { Stanza::ref event = client->getNextEvent(timeout); if (!event) { // We got a timeout lua_pushnil(L); return 1; } else { // Push the function and event on the stack lua_pushvalue(L, 2); pushEvent(L, event); int oldTop = lua_gettop(L) - 2; lua_call(L, 1, LUA_MULTRET); int returnValues = lua_gettop(L) - oldTop; if (returnValues > 0) { lua_remove(L, -1 - returnValues); return returnValues; } } } } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
/* * Usage: ./MUCListAndJoin <jid> <password> <muc_domain> */ int main(int argc, char* argv[]) { int ret = 0; if (argc != 4) { cout << "Usage: ./" << argv[0] << " <jid> <password> <muc_domain>" << endl; ret = -1; } else { mucJID = JID(argv[3]); client = std::make_shared<Client>(JID(argv[1]), string(argv[2]), &networkFactories); client->setAlwaysTrustCertificates(); // Enable the following line for detailed XML logging // ClientXMLTracer* tracer = new ClientXMLTracer(client.get()); client->onConnected.connect(&handleConnected); client->onDisconnected.connect(&handleDisconnected); client->onMessageReceived.connect(&handleIncomingMessage); cout << "Connecting..." << flush; client->connect(); { Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); Timer::ref disconnectTimer = networkFactories.getTimerFactory()->createTimer(25000); disconnectTimer->onTick.connect(boost::bind(&Client::disconnect, client.get())); timer->start(); disconnectTimer->start(); eventLoop.run(); } } return ret; }
static int sluift_client_add_contact(lua_State* L) { try { eventLoop.runOnce(); SluiftClient* client = getClient(L); RosterItemPayload item; if (lua_type(L, 2) == LUA_TTABLE) { lua_getfield(L, 2, "jid"); const char* rawJID = lua_tostring(L, -1); if (rawJID) { item.setJID(std::string(rawJID)); } lua_getfield(L, 2, "name"); const char* rawName = lua_tostring(L, -1); if (rawName) { item.setName(rawName); } lua_getfield(L, 2, "groups"); if (!lua_isnil(L, -1)) { if (lua_type(L, -1) == LUA_TTABLE) { for (size_t i = 1; i <= lua_objlen(L, -1); ++i) { lua_rawgeti(L, -1, i); const char* rawGroup = lua_tostring(L, -1); if (rawGroup) { item.addGroup(rawGroup); } lua_pop(L, 1); } } else { return luaL_error(L, "Groups should be a table"); } } } else { item.setJID(luaL_checkstring(L, 2)); } client->getRoster(); if (!client->getClient()->getRoster()->containsJID(item.getJID())) { RosterPayload::ref roster = boost::make_shared<RosterPayload>(); roster->addItem(item); ResponseSink<RosterPayload> sink; SetRosterRequest::ref request = SetRosterRequest::create(roster, client->getClient()->getIQRouter()); boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); request->send(); while (!sink.hasResponse()) { eventLoop.runUntilEvents(); } if (sink.getResponseError()) { lua_pushboolean(L, false); return 1; } } client->getClient()->getSubscriptionManager()->requestSubscription(item.getJID()); lua_pushboolean(L, true); return 1; } catch (const SluiftException& e) { return luaL_error(L, e.getReason().c_str()); } }
void disconnect() { client->disconnect(); while (client->isActive()) { eventLoop.runUntilEvents(); } }
void exit(int code) { exitCode = code; eventLoop.stop(); }