Kamis, 06 Oktober 2011

Source code Chat Java SE

Chating adalah salah satu media komunikasi yang cukup populer pada saat ini gan, dari anak SMP sampai kalagan tuapun mengunakan media ini untuk berkomunikasi dan mencari teman atau relasi. Nah mungkin yang paling populer adalah Yahoo Masangger walaupun banyak lagi jenisnya seperti Camfrog, Skype, BBM, Mic33, MIRc, dan lain sebagainya gan.
Media chating tersebut dibuat mengunakan bahasa pemrograman tergantung kebutuhan dan minat dari pembuatnya gan. Dalam kesempatan kali saya akan memberikan sedikit referensi untuk para pengunjung ne bagai mana membuat chat mengunakan bahasa Java SE yang jelas untuk desktop gan.
Nah saya berikan ne source code chat silahkan diaplikasikan gan semoga bermanfaat untuk kalian semua:



001:        package org.claros.chat.threads;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collections;
005:        import java.util.List;
006:
007:        import org.apache.commons.logging.Log;
008:        import org.apache.commons.logging.LogFactory;
009:        import org.claros.chat.controllers.QueueController;
010:        import org.jivesoftware.smack.PacketListener;
011:        import org.jivesoftware.smack.XMPPConnection;
012:        import org.jivesoftware.smack.filter.PacketFilter;
013:        import org.jivesoftware.smack.packet.Message;
014:        import org.jivesoftware.smack.packet.Packet;
015:        import org.jivesoftware.smack.packet.Presence;
016:
 
017:        /**
018:         * @author Umut Gokbayrak
019:         */
020:        public class ChatListener extends Thread {
021:            private static Log log = LogFactory.getLog(ChatListener.class);
022:            private static List subsRequest;
023:            private String user;
024:            private XMPPConnection conn;
025:            private MyListener listener;
026:            private MyFilter filter;
027:
028:            /**
029:             * Do not use this constructor
030:             *
031:             */
032:            private ChatListener() {
033:                super ();
034:            }
035:
036:            /**
037:             * Constructor method.
038:             * @param user
039:             * @param conn
040:             */
041:            public ChatListener(String user, XMPPConnection conn) {
042:                if (subsRequest == null) {
043:                    subsRequest = Collections.synchronizedList(new ArrayList());
044:                }
045:                this .user = user;
046:                this .conn = conn;
047:                listener = new MyListener();
048:                filter = new MyFilter();
049:            }
050:
051:            /* (non-Javadoc)
052:             * @see java.lang.Thread#run()
053:             */
054:            public void run() {
055:                conn.addPacketListener(listener, filter);
056:            }
057:
058:            /**
059:             * method to call to stop this thread
060:             *
061:             */
062:            public void terminate() {
063:                try {
064:                    conn.removePacketListener(listener);
065:                } catch (RuntimeException e) {
066:                    e.printStackTrace();
067:                }
068:            }
069:
070:            public List getUnreadMessages() {
071:                List msgs = null;
072:                try {
073:                    msgs = QueueController.fetchUserMessages(user,
074:                            QueueController.QUEUE_IN);
075:                } catch (Exception e) {
076:                    log.error("unable to get unread messages", e);
077:                }
078:                return msgs;
079:            }
080:
081:            public synchronized List getNewSubscriptions() {
082:     // TODO: mesajlardakinin aynısını queue yapısı bunda yapmalıyız.
083:
084:                ArrayList outList = new ArrayList();
085:                ArrayList toDelete = new ArrayList();
086:
087:                // look if new messages are waiting
088:                Presence tmp = null;
089:                String tmpTo = null;
090:                int tmpPos = -1;
091:                for (int i = 0; i < subsRequest.size(); i++) {
092:                    tmp = (Presence) subsRequest.get(i);
093:                    tmpTo = tmp.getTo();
094:                    tmpPos = tmpTo.indexOf("/");
095:                    if (tmpPos > -1) {
096:                        tmpTo = tmpTo.substring(0, tmpPos);
097:                    }
098:                    if (tmpTo.equals(user)) {
099:                        toDelete.add(new Integer(i));
100:                        outList.add(tmp);
101:                    }
102:                }
103:
104:                // remove the fetched messages
105:                for (int i = toDelete.size() - 1; i >= 0; i--) {
106:                    try {
107:                        subsRequest.remove(((Integer) toDelete.get(i))
108:                                .intValue());
109:                    } catch (RuntimeException e) {
110:                        e.printStackTrace();
111:                    }
112:                }
113:                return outList;
114:            }
115:
116:            /**
117:             * Listener class implementation
118:             * 
119:             * @author Umut Gokbayrak
120:             */
121:            class MyListener implements  PacketListener {
122:
123:          /* (non-Javadoc)
124:           * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
125:           */
126:                public void processPacket(Packet arg0) {
127:                    if (arg0 instanceof  Message) {
128:                        Message msg = (Message) arg0;
129:
130:                        if (msg.getFrom().indexOf("/") > -1) {
131:                            msg.setFrom(msg.getFrom().substring(0,
132:                                    msg.getFrom().indexOf("/")));
133:                        }
134:
135:                        if (msg.getTo().indexOf("/") > -1) {
136:                            msg.setTo(msg.getTo().substring(0,
137:                                    msg.getTo().indexOf("/")));
138:                        }
139:
140:                        // pushing the message to db.
141:                        QueueController.push(msg.getFrom(), msg.getTo(), msg
142:                                .getBody(), QueueController.QUEUE_IN);
143:
144:                        log.debug("new message written to queue, from:"
145:                                + msg.getFrom() + " to: " + msg.getTo()
146:                                + " body: " + msg.getBody());
147:                    } else if (arg0 instanceof  Presence) {
148:                        Presence prs = (Presence) arg0;
149:                        if (prs.getType().equals(Presence.Type.SUBSCRIBE)) {
150:                            subsRequest.add(arg0);
151:                        }
152:                    }
153:                }
154:            }
155:
156:            /**
157:             * Listener packet filter implementation.
158:             * 
159:             * @author Umut Gokbayrak
160:             */
161:            class MyFilter implements  PacketFilter {
162:
163:          /* (non-Javadoc)
164:           * @see org.jivesoftware.smack.filter.PacketFilter#accept(org.jivesoftware.smack.packet.Packet)
165:           */
166:                public boolean accept(Packet arg0) {
167:                    // TODO: ignored users should be handled here.
168:                    return true;
169:                }
170:            }
171:
172:            /**
173:             * @return
174:             */
175:            public String getUser() {
176:                return user;
177:            }
178:
179:            /**
180:             * @param string
181:             */
182:            public void setUser(String string) {
183:                user = string;
184:            }
185:
186:        }

Nah demikian dari saya gan semoga bermanfaat ^_^.....

Tidak ada komentar:

Posting Komentar