KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dcc > ProtocolDCC


1 package rero.dcc;
2
3 import rero.ircfw.*;
4 import rero.ircfw.interfaces.*;
5
6 import java.io.*;
7 import java.net.*;
8 import java.util.*;
9
10 public abstract class ProtocolDCC
11 {
12    public static final int DCC_SEND = 001;
13    public static final int DCC_RECEIVE = 002;
14    public static final int DCC_CHAT = 003;
15
16    public static final int STATE_WAIT = 401;
17    public static final int STATE_OPEN = 402; // 3 different states of a dcc connection.
18
public static final int STATE_CLOSED = 403;
19
20    public static final int DCC_TIMEOUT = 60 * 1000 * 2; // 2 minutes
21

22    protected String JavaDoc nickname;
23    protected Socket socket;
24
25    protected long idleTime;
26    protected long startTime;
27
28    protected HashMap eventData = new HashMap();
29    protected ProtocolDispatcher dispatcher;
30
31    protected int state = STATE_WAIT; // by default we are in a waiting state.
32

33    /** return the type of DCC based on a constant */
34    public abstract int getTypeOfDCC();
35
36    public void close()
37    {
38       try
39       {
40          socket.close();
41          state = STATE_CLOSED;
42       }
43       catch (Exception JavaDoc ex)
44       {
45          ex.printStackTrace();
46       }
47    }
48
49    public int getState()
50    {
51       if (state == STATE_OPEN && !isConnected())
52       {
53          state = STATE_CLOSED;
54       }
55       return state;
56    }
57
58    public String JavaDoc getRemoteAddress()
59    {
60       return socket.getInetAddress().getHostAddress();
61    }
62
63    public int getLocalPort()
64    {
65       return socket.getLocalPort();
66    }
67
68    public int getPort()
69    {
70       return socket.getPort();
71    }
72
73    /** tells the protocol implementation class that we are ready to rock and roll, Socket is assumed to be connected. */
74    public void announceFramework(ChatFramework f)
75    {
76       dispatcher = f.getProtocolDispatcher();
77    }
78
79    public abstract void run();
80
81    /** returns wether or not the socket is connected */
82    public boolean isConnected()
83    {
84        return socket != null && socket.isConnected();
85    }
86
87    /** returns the nickname of who we are having a *chat* with */
88    public String JavaDoc getNickname()
89    {
90        return nickname;
91    }
92
93    /** returns the number of milliseconds since this chat has been active */
94    public long getIdleTime()
95    {
96       return (System.currentTimeMillis() - idleTime);
97    }
98
99    /** return time that this chat started */
100    public long getStartTime()
101    {
102       return startTime;
103    }
104  
105    /** return total amount of time this chat has been active (in milliseconds) */
106    public long getTotalTime()
107    {
108       return System.currentTimeMillis() - startTime;
109    }
110
111    public void setDCCSocket(Socket _socket)
112    {
113       socket = _socket;
114
115       startTime = System.currentTimeMillis();
116       idleTime = System.currentTimeMillis();
117
118       state = STATE_OPEN;
119    }
120
121    public abstract void fireError(String JavaDoc text);
122   
123 }
124
Popular Tags