View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.net.msg.client;
21  
22  import java.io.IOException;
23  import java.util.Hashtable;
24  
25  import org.xnap.XNap;
26  import org.xnap.plugin.opennap.net.msg.ExceptionListener;
27  import org.xnap.plugin.opennap.net.msg.Message;
28  import org.xnap.plugin.opennap.util.OpenNapServerVersion;
29  
30  public class ClientMessage extends Message implements Comparable {
31  
32      //--- Constant(s) ---
33  
34      /***
35       * Priority for urgent messages.
36       */
37      public static int PRIORITY_HIGH = 3;
38      
39      /***
40       * Priority for normal messages.
41       */
42      public static int PRIORITY_NORMAL = 2;
43  
44      /***
45       * Priority for not so urgent messages.
46       */
47      public static int PRIORITY_LOW = 1;
48      
49      //--- Data Field(s) ---
50  
51      public String data;
52      public Hashtable ht;
53      public ExceptionListener listener;
54  
55      //--- Constructor(s) ---
56  
57      protected ClientMessage(int type, String data) 
58      {
59  		super(type);
60  
61  		this.data = data;
62      }
63  
64      protected ClientMessage(int type) 
65      {
66  		this(type, "");
67      }
68  
69      //--- Method(s) ---
70  
71      public void add(OpenNapServerVersion sv, String d)
72      {
73  		if (ht == null) {
74  			ht = new Hashtable();
75  		}
76  		ht.put(sv, d);
77      }
78  
79      public int compareTo(Object o)
80      {
81  		return getPriority() - ((ClientMessage)o).getPriority();
82      }
83      
84      /***
85       * Notifies the exception listener that the message could not
86       * be sent.
87       */
88      public void failed()
89      {
90  		if (listener != null) {
91  			listener.exceptionThrown
92  				(new IOException(XNap.tr("Server disconnected")));
93  		}
94      }
95  
96      public ExceptionListener getExceptionListener()
97      {
98  		return listener;
99      }
100 
101     public void setExceptionListener(ExceptionListener newValue)
102     {
103 		listener = newValue;
104     }
105 
106     public String getData(OpenNapServerVersion version)
107     {
108 		if (ht != null) {
109 			OpenNapServerVersion sv = version.getNextVersion();
110 			while (sv != null) {
111 				Object d = ht.get(sv);
112 				if (d != null) {
113 					return (String)d;
114 				}
115 				else {
116 					sv = sv.getNextVersion();
117 				}
118 			}
119 		}
120 
121 		return data;
122     }
123 
124     public String getData()
125     {
126 		return data;
127     }
128 
129     public int getPriority()
130     {
131 		return PRIORITY_NORMAL;
132     }
133     
134     public int getType()
135     {
136 		return type;
137     }
138 
139 }