KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chipchat > Msg


1 /*
2  * Created on 2003. 2. 20.
3  */

4 package chipchat;
5
6 /**
7  * Message.
8  * @author Mr. Lee
9  */

10 public class Msg {
11    // Static constants.
12
/** It is message. */
13    public static final int TYPE_MSG = 0;
14    /** It is custom message. */
15    public static final int TYPE_CUSTOMMSG = 1;
16    /** It is Information. */
17    public static final int TYPE_INFO = 2;
18    /** It is Error. */
19    public static final int TYPE_ERROR = 3;
20    /** It is sending whisper message. */
21    public static final int TYPE_WSPSND = 11;
22    /** It is receiving whisper message. */
23    public static final int TYPE_WSPRCV = 12;
24    /** It is user list. */
25    public static final int TYPE_USERS = 20;
26    /** It is administrator information. */
27    public static final int TYPE_ADMIN = 30;
28    /** It is message tha administrator is changed. */
29    public static final int TYPE_ADMINCHANGE = 31;
30    /** It is message that administrator send to someone to keep quiet. */
31    public static final int TYPE_KEEPQUIET = 40;
32    /** It is message that someone had beed kicked out. */
33    public static final int TYPE_KICKOUT = 41;
34
35    // Member values
36
/**
37     * Receiver.
38     */

39    private final int to;
40    /**
41     * Writer.
42     */

43    private final String JavaDoc writer;
44    /**
45     * Message
46     */

47    private final String JavaDoc msg;
48    /**
49     * Message type.
50     */

51    private final int msgType;
52
53    /**
54     * Constructor.
55     * @param msgType Type of message.
56     * @param to To.
57     * @param msg Message.
58     * @param writer Writer.
59     */

60    public Msg(
61       final int msgType,
62       final int to,
63       final String JavaDoc msg,
64       final String JavaDoc writer) {
65       this.to = to;
66       this.msg = msg;
67       this.writer = writer;
68       this.msgType = msgType;
69    }
70
71    /**
72     * Make full string which will be sent to client.
73     * @param id User id.
74     * @return Maked string.
75     */

76    public final String JavaDoc getString(final int id) {
77       if (to == -1 || to == id || id == -2) {
78          switch (msgType) {
79             case Msg.TYPE_MSG :
80                return ("MSG:" + writer + ">" + msg + "\r\n");
81             case Msg.TYPE_CUSTOMMSG :
82                return ("CUSTOM:" + writer + ">" + msg + "\r\n");
83             case Msg.TYPE_INFO :
84                return ("INFO:" + writer + ">" + msg + "\r\n");
85             case Msg.TYPE_USERS :
86                return ("USERS:" + msg + "\r\n");
87             case Msg.TYPE_ERROR :
88                return ("ERROR:" + msg + "\r\n");
89             case Msg.TYPE_WSPSND :
90                return ("WSPSND:" + writer + ">" + msg + "\r\n");
91             case Msg.TYPE_WSPRCV :
92                return ("WSPRCV:" + writer + ">" + msg + "\r\n");
93             case Msg.TYPE_ADMIN :
94                return ("ADMIN:" + msg + "\r\n");
95             case Msg.TYPE_ADMINCHANGE :
96                return ("ADMCG:" + msg + "\r\n");
97             case Msg.TYPE_KEEPQUIET :
98                return ("KEEPQUIET:" + writer + ">" + msg + "\r\n");
99             case Msg.TYPE_KICKOUT :
100                return ("KICKOUT:" + writer + ">" + msg + "\r\n");
101             default :
102                System.out.println(
103                   "Need Prosess Routin for msgtype :" + msgType);
104                return null;
105          }
106       } else {
107          return null;
108       }
109    }
110 }
111
Popular Tags