示例#1
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;
}
示例#2
0
		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");
			}
		}
示例#3
0
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());
	}
}
示例#4
0
/*
 *  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;
}
示例#5
0
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());
	}
}
示例#6
0
		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;
			}
		}
示例#7
0
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;
}
示例#8
0
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;
}
示例#9
0
		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();
			}
		}