java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/chat/Spojeni.java
author František Kučera <franta-hg@frantovo.cz>
Sun Aug 15 22:20:10 2010 +0200 (2010-08-15)
changeset 151 d7b5099bf65e
parent 149 8238cdb4113a
permissions -rw-r--r--
chat
franta-hg@149
     1
package cz.frantovo.nekurak.chat;
franta-hg@149
     2
franta-hg@149
     3
import java.util.ArrayList;
franta-hg@149
     4
import java.util.Collection;
franta-hg@149
     5
import java.util.logging.Level;
franta-hg@149
     6
import java.util.logging.Logger;
franta-hg@149
     7
import org.jivesoftware.smack.ConnectionConfiguration;
franta-hg@149
     8
import org.jivesoftware.smack.XMPPConnection;
franta-hg@149
     9
import org.jivesoftware.smack.XMPPException;
franta-hg@149
    10
franta-hg@149
    11
/**
franta-hg@149
    12
 *
franta-hg@149
    13
 * @author fiki
franta-hg@149
    14
 */
franta-hg@149
    15
public class Spojeni {
franta-hg@149
    16
franta-hg@149
    17
	private XMPPConnection spojeni;
franta-hg@149
    18
	private Collection<MistnostPripojena> mistnosti = new ArrayList<MistnostPripojena>();
franta-hg@149
    19
	private static final Logger log = Logger.getLogger(Spojeni.class.getSimpleName());
franta-hg@149
    20
franta-hg@149
    21
	public Spojeni(UcetRobota ucet) throws XMPPException {
franta-hg@149
    22
		/** Navážeme spojení */
franta-hg@151
    23
		ConnectionConfiguration nastaveni;
franta-hg@151
    24
		if (ucet.getAdresaServeru() == null || ucet.getPort() == 0) {
franta-hg@151
    25
			nastaveni = new ConnectionConfiguration(ucet.getDomena());
franta-hg@151
    26
		} else {
franta-hg@151
    27
			nastaveni = new ConnectionConfiguration(ucet.getAdresaServeru(), ucet.getPort(), ucet.getDomena());
franta-hg@151
    28
		}
franta-hg@151
    29
franta-hg@149
    30
		spojeni = new XMPPConnection(nastaveni);
franta-hg@149
    31
		spojeni.connect();
franta-hg@149
    32
		spojeni.login(ucet.getUzivatelskeJmeno(), ucet.getUzivatelskeHeslo(), ucet.getProstredek());
franta-hg@149
    33
franta-hg@149
    34
		/** Vstoupíme do místností */
franta-hg@149
    35
		for (Mistnost m : ucet.getMistnosti()) {
franta-hg@149
    36
			MistnostPripojena mp = new MistnostPripojena(m, this);
franta-hg@149
    37
			mistnosti.add(mp);
franta-hg@149
    38
		}
franta-hg@149
    39
	}
franta-hg@149
    40
franta-hg@149
    41
	public void odpoj() {
franta-hg@149
    42
		/** Rozloučíme se ve všech místnostech */
franta-hg@149
    43
		try {
franta-hg@149
    44
			for (MistnostPripojena mp : mistnosti) {
franta-hg@149
    45
				mp.odejdi();
franta-hg@149
    46
			}
franta-hg@149
    47
		} catch (Exception e) {
franta-hg@149
    48
			log.log(Level.WARNING, "Nepodařilo se odejít z místnosti před ukončením spojení.", e);
franta-hg@149
    49
		}
franta-hg@149
    50
franta-hg@149
    51
		/** Ukončíme spojení */
franta-hg@149
    52
		spojeni.disconnect();
franta-hg@149
    53
	}
franta-hg@149
    54
franta-hg@149
    55
	public XMPPConnection getSpojeni() {
franta-hg@149
    56
		return spojeni;
franta-hg@149
    57
	}
franta-hg@149
    58
franta-hg@149
    59
	public Collection<MistnostPripojena> getMistnosti() {
franta-hg@149
    60
		return mistnosti;
franta-hg@149
    61
	}
franta-hg@149
    62
}