aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/ssh/ContactTimerSSHImpl.java
blob: 4726d2b99593a61eef21f7f2c6acd9c3857e2be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging clitent.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * ContactTimerSSHImpl.java
 *
 * SSH Suport in SIP Communicator - GSoC' 07 Project
 */

package net.java.sip.communicator.impl.protocol.ssh;

import java.io.IOException;
import java.net.*;
import java.util.*;
import net.java.sip.communicator.util.*;

/**
 * Timer Task to update the reachability status of SSH Contact in contact list.
 * (Reachability of remote machine from user's machine)
 * The timer is started at either of the two places
 * - A new contact - OperationSetPersistentPresenceSSHImpl
 *                                                  .createUnresolvedContact
 * - Existing Contact - OperationSetPersistentPresenceSSHImpl.subscribe
 *
 * @author Shobhit Jindal
 */
public class ContactTimerSSHImpl
        extends TimerTask
{
    private static final Logger logger
            = Logger.getLogger(OperationSetFileTransferSSHImpl.class);

    /**
     * The contact ID of the remote machine
     */
    private ContactSSH sshContact;

    /**
     * PersistentPresence Identifer assoiciated with SSH Contact
     */
    private OperationSetPersistentPresenceSSHImpl persistentPresence;

    /**
     * The method which is called at regular intervals to update the status
     * of remote machines
     *
     * Presently only ONLINE and OFFILINE status are checked
     */
    public void run()
    {
        try
        {
            InetAddress remoteMachine = InetAddress.getByName(
                    sshContact.getSSHConfigurationForm().getHostName());

            //check if machine is reachable
            if(remoteMachine.isReachable(
                    sshContact.getSSHConfigurationForm().getUpdateInterval()))
            {
                if (sshContact.getPresenceStatus().equals(SSHStatusEnum.OFFLINE)
                || sshContact.getPresenceStatus().equals(SSHStatusEnum
                        .NOT_AVAILABLE))
                {
                    // change status to online
                    persistentPresence.changeContactPresenceStatus(
                            sshContact, SSHStatusEnum.ONLINE);

                    if (logger.isDebugEnabled())
                        logger.debug("SSH Host " + sshContact
                        .getSSHConfigurationForm().getHostName() + ": Online");
                }

            }
            else throw new IOException();

        }
        catch (IOException ex)
        {
            if (sshContact.getPresenceStatus().equals(SSHStatusEnum.ONLINE)
            || sshContact.getPresenceStatus().equals(
                    SSHStatusEnum.NOT_AVAILABLE))
            {
                persistentPresence.changeContactPresenceStatus(
                        sshContact, SSHStatusEnum.OFFLINE);

                if (logger.isDebugEnabled())
                    logger.debug("SSH Host " + sshContact.getSSHConfigurationForm()
                .getHostName() + ": Offline");
            }
        }
    }
    /**
     * Creates a new instance of ContactTimerSSHImpl
     *
     * @param sshContact the <tt>Contact</tt>
     */
    public ContactTimerSSHImpl(ContactSSH sshContact)
    {
        super();
        this.sshContact = sshContact;
        this.persistentPresence = (OperationSetPersistentPresenceSSHImpl)
            sshContact.getParentPresenceOperationSet();
    }

}