use of org.jivesoftware.smack.filter.FromMatchesFilter in project ecf by eclipse.
the class MultiUserChat method join.
/**
* Joins the chat room using the specified nickname and password. If already joined
* using another nickname, this method will first leave the room and then
* re-join using the new nickname.<p>
*
* To control the amount of history to receive while joining a room you will need to provide
* a configured DiscussionHistory object.<p>
*
* A password is required when joining password protected rooms. If the room does
* not require a password there is no need to provide one.<p>
*
* If the room does not already exist when the user seeks to enter it, the server will
* decide to create a new room or not.
*
* @param nickname the nickname to use.
* @param password the password to use.
* @param history the amount of discussion history to receive while joining a room.
* @param timeout the amount of time to wait for a reply from the MUC service(in milleseconds).
* @throws XMPPException if an error occurs joining the room. In particular, a
* 401 error can occur if no password was provided and one is required; or a
* 403 error can occur if the user is banned; or a
* 404 error can occur if the room does not exist or is locked; or a
* 407 error can occur if user is not on the member list; or a
* 409 error can occur if someone is already in the group chat with the same nickname.
*/
public synchronized void join(String nickname, String password, DiscussionHistory history, long timeout) throws XMPPException {
if (nickname == null || nickname.equals("")) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
// nickname.
if (joined) {
leave();
}
// We join a room by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(room + "/" + nickname);
// Indicate the the client supports MUC
MUCInitialPresence mucInitialPresence = new MUCInitialPresence();
if (password != null) {
mucInitialPresence.setPassword(password);
}
if (history != null) {
mucInitialPresence.setHistory(history.getMUCHistory());
}
joinPresence.addExtension(mucInitialPresence);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
}
// Wait for a presence packet back from the server.
PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room + "/" + nickname), new PacketTypeFilter(Presence.class));
PacketCollector response = null;
Presence presence;
try {
response = connection.createPacketCollector(responseFilter);
// Send join packet.
connection.sendPacket(joinPresence);
// Wait up to a certain number of seconds for a reply.
presence = (Presence) response.nextResult(timeout);
} finally {
// Stop queuing results
if (response != null) {
response.cancel();
}
}
if (presence == null) {
throw new XMPPException("No response from server.");
} else if (presence.getError() != null) {
throw new XMPPException(presence.getError());
}
this.nickname = nickname;
joined = true;
userHasJoined();
}
use of org.jivesoftware.smack.filter.FromMatchesFilter in project Openfire by igniterealtime.
the class ThrottleTestReader method main.
/**
* Starts the throttle test reader client.
*
* @param args application arguments.
*/
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: java ThrottleTestReader [server] [username] [password]");
System.exit(0);
}
String server = args[0];
String username = args[1];
String password = args[2];
try {
// Connect to the server, without TLS encryption.
ConnectionConfiguration config = new ConnectionConfiguration(server);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
final XMPPConnection con = new XMPPConnection(config);
System.out.print("Connecting to " + server + "... ");
con.connect();
con.login(username, password, "reader");
System.out.print("success.");
System.out.println("");
// Get the "real" server address.
server = con.getServiceName();
final String writerAddress = username + "@" + server + "/writer";
String readerAddress = username + "@" + server + "/reader";
System.out.println("Registered as " + readerAddress);
// Look for the reader process.
System.out.print("Waiting for " + writerAddress + "...");
PacketCollector collector = con.createPacketCollector(new AndFilter(new FromMatchesFilter(writerAddress), new PacketTypeFilter(Time.class)));
Time timeRequest = (Time) collector.nextResult();
Time timeReply = new Time(Calendar.getInstance());
timeReply.setPacketID(timeRequest.getPacketID());
timeReply.setType(IQ.Type.RESULT);
timeReply.setTo(timeRequest.getFrom());
con.sendPacket(timeReply);
System.out.println(" found writer. Now in reading mode.");
// Track how many packets we've read.
con.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
packetCount.getAndIncrement();
}
}, new PacketTypeFilter(Message.class));
while (!done) {
Thread.sleep(5000);
int count = packetCount.getAndSet(0);
System.out.println("Packets per second: " + (count / 5));
}
// Sleep while we're reading packets.
Thread.sleep(Integer.MAX_VALUE);
} catch (Exception e) {
System.out.println("\nError: " + e.getMessage());
}
}
Aggregations