aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/ssh/SSHReaderDaemon.java
blob: 2a4cab437919cd7944d57e845510f3f4ec1d742c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * SSHReaderDaemon.java
 *
 * SSH Suport in SIP Communicator - GSoC' 07 Project
 *
 */
package net.java.sip.communicator.impl.protocol.ssh;

import java.io.*;

import net.java.sip.communicator.service.protocol.*;

/**
 *
 * @author Shobhit Jindal
 */
public class SSHReaderDaemon
        extends Thread
{

    /**
     * A Buffer to aggregate replies to be sent as one message
     */
    private StringBuffer replyBuffer;
    
    /**
     * The identifier of Contact representing the remote machine
     */
    private ContactSSHImpl sshContact;
    
    /**
     * The identifier of the message received from server
     */
    private String message;
    
    /**
     * An identifier representing the state of Reader Daemon
     */
    private boolean isActive = false;
    
    /**
     * This OperationSet delivers incoming message
     */
    private OperationSetBasicInstantMessagingSSHImpl instantMessaging;
    
    /**
     * Input Stream of remote user to be read
     */
    private InputStream shellInputStream;
    
    /**
     * Buffered Reader associated with above input stream
     */
    private InputStreamReader shellReader;
    
//    /**
//     * This OperationSet delivers incoming message
//     */
//    private OperationSetPersistentPresenceSSHImpl persistentPresence;
    
    /**
     * Bytes available in Input Stream before reading
     */
    private int bytesAvailable;
    
    private int bytesRead;
    
    int bufferCount;
    
    char buf;
    
    /**
     * Creates a new instance of SSHReaderDaemon
     */
    public SSHReaderDaemon(ContactSSH sshContact)
    {
        this.sshContact = (ContactSSHImpl)sshContact;
        instantMessaging = (OperationSetBasicInstantMessagingSSHImpl) sshContact
            .getProtocolProvider().getOperationSet(
                    OperationSetBasicInstantMessaging.class);
    }
    
    /**
     * Reads the remote machine, updating the chat window as necessary
     * in a background thread
     */
    public void run()
    {
        shellInputStream = sshContact.getShellInputStream();
        shellReader = sshContact.getShellReader();
        replyBuffer = new StringBuffer();

        
        try
        {
            do
            {
                bytesAvailable = shellInputStream.available();
                
                if(bytesAvailable == 0 )
                {
                    // wait if more data is available
                    // for a slower connection this value need to be raised
                    // to avoid splitting of messages
                    Thread.sleep(250);
                    continue;
                }
                
                bufferCount = 0;
                
//                if(replyBuffer > 0)
                
                do
                {
                    // store the responses in a buffer
                    storeMessage(replyBuffer);
                    
                    Thread.sleep(250);
                    
                    bytesAvailable = shellInputStream.available();
                    
                }while(bytesAvailable > 0  && bufferCount<16384);
                
                message = replyBuffer.toString();
                
                if(sshContact.isCommandSent())
                {
                    // if the response is as a result of a command sent
                    sshContact.setMessageType(sshContact
                            .CONVERSATION_MESSAGE_RECEIVED);
                    
                    message = message.substring(message.indexOf('\n') + 1);
                    
                    sshContact.setCommandSent(false);
                }
                else
                {
                    // server sent an asynchronous message to the terminal
                    // display it as a system message
                    sshContact.setMessageType(sshContact
                            .SYSTEM_MESSAGE_RECEIVED);
                    
                    //popup disabled
//                    JOptionPane.showMessageDialog(
//                            null,
//                            message,
//                            "Message from " + sshContact.getDisplayName(),
//                            JOptionPane.INFORMATION_MESSAGE);
                }
                
                instantMessaging.deliverMessage(
                        instantMessaging.createMessage(message),
                        sshContact);
                
                replyBuffer.delete(0, replyBuffer.length());
                
            }while(isActive);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    /**
     * Stores the response from server in a temporary buffer
     * the bytes available are determined before the function is called
     *
     * @param replyBuffer to store the response from server
     *
     * @throws IOException if the network goes down
     */
    private void storeMessage(StringBuffer replyBuffer) throws IOException
    {
        do
        {
            buf = (char)shellInputStream.read();
            
//            System.out.println(String.valueOf(buf)+ " " + (int)buf);
            
            replyBuffer.append(String.valueOf(buf));
            
//                    logger.debug(shellReader.readLine());
            
            bufferCount++;
            
            bytesAvailable--;
            
        }while(bytesAvailable>0 && bufferCount<32700);
    }
    
    public void isActive(boolean isActive)
    {
        this.isActive = isActive;
    }
}