001 /* 002 Copyright (C) 2003 Adam Olsen 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU General Public License as published by 006 the Free Software Foundation; either version 1, or (at your option) 007 any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details. 013 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 017 */ 018 019 package com.valhalla.jbother; 020 021 import com.valhalla.jbother.jabber.*; 022 import com.valhalla.jbother.jabber.smack.*; 023 024 import java.util.*; 025 import org.jivesoftware.smack.*; 026 import org.jivesoftware.smack.packet.*; 027 import org.jivesoftware.smack.filter.*; 028 import com.valhalla.jbother.*; 029 030 /** 031 * Collects version info about Jabber users as they sign in. 032 * Runs in the background and collects <pre>jabber:iq:version</pre> about 033 * users as they sign in 034 * 035 * @author Adam Olsen 036 * @version 1.0 037 **/ 038 public class VersionCollectorThread implements Runnable 039 { 040 private Vector buddies = new Vector(); 041 042 /** 043 * Adds a buddy to the queue 044 * @param buddy the buddy to add to the queue 045 **/ 046 public void add( BuddyStatus buddy ) 047 { 048 synchronized( buddies ) 049 { 050 buddies.add( buddy ); 051 } 052 } 053 054 /** 055 * To be called when the thread starts 056 **/ 057 public void run() 058 { 059 while( true ) 060 { 061 try { 062 Thread.sleep( 1500 ); 063 } 064 catch( InterruptedException e ) { } 065 066 getVersions(); 067 } 068 } 069 070 /** 071 * Gets the versions of the buddies in the queue 072 **/ 073 private void getVersions() 074 { 075 synchronized( buddies ) 076 { 077 while( buddies.size() > 0 ) 078 { 079 BuddyStatus buddy = (BuddyStatus)buddies.firstElement(); 080 String resource = buddy.getHighestResource(); 081 String user = buddy.getUser(); 082 083 if( resource != null && !resource.equals( "_no resource_" ) ) user += "/" + resource; 084 XMPPConnection con = BuddyList.getInstance().getConnection(); 085 086 Version request = new Version(); 087 request.setType(IQ.Type.GET); 088 request.setTo( user ); 089 090 // Create a packet collector to listen for a response. 091 PacketCollector collector = con.createPacketCollector( 092 new PacketIDFilter( request.getPacketID() ) ); 093 094 con.sendPacket( request ); 095 096 // Wait up to 5 seconds for a result. 097 IQ result = (IQ)collector.nextResult( 3000 ); 098 if( result != null && result.getType() == IQ.Type.RESULT ) 099 { 100 Version v = (Version)result; 101 buddy.setVersionInfo( v.getName() + " " + v.getVersion() + " / " + v.getOs() ); 102 } 103 104 buddies.remove( buddy ); 105 } 106 } 107 } 108 }