001: package test;
002:
003: import dalma.endpoints.irc.Channel;
004: import dalma.endpoints.irc.IRCEndPoint;
005: import dalma.endpoints.irc.Message;
006: import dalma.endpoints.irc.NewSessionListener;
007: import dalma.endpoints.irc.PrivateChat;
008: import dalma.endpoints.irc.Buddy;
009: import dalma.spi.ConversationSPI;
010: import dalma.test.Launcher;
011:
012: import java.io.Serializable;
013: import java.io.IOException;
014:
015: /**
016: * @author Kohsuke Kawaguchi
017: */
018: public class IRCDemo extends Launcher {
019: private IRCEndPoint iep;
020:
021: public IRCDemo(String[] args) throws Exception {
022: super (args);
023: }
024:
025: protected void setUpEndPoints() throws Exception {
026: iep = new IRCEndPoint("irc1",/*"irc.blessed.net"*/
027: "irc.central.sun.com", "dalma");
028: iep.setNewSessionListener(new NewSessionListener() {
029: public void onNewPrivateChat(PrivateChat chat) {
030: try {
031: createConversation(ConversationImpl.class, iep,
032: chat);
033: } catch (Exception e) {
034: e.printStackTrace();
035: }
036: }
037:
038: public void onInvite(Buddy sender, Channel channel) {
039: try {
040: createConversation(ChannelConversationImpl.class,
041: iep, channel);
042: } catch (Exception e) {
043: e.printStackTrace();
044: }
045: }
046: });
047: engine.addEndPoint(iep);
048: }
049:
050: public static void main(String[] args) throws Exception {
051: new IRCDemo(args);
052: }
053:
054: public static final class ConversationImpl implements Runnable,
055: Serializable {
056: private final PrivateChat chat;
057: private final IRCEndPoint iep;
058:
059: public ConversationImpl(IRCEndPoint iep, PrivateChat chat) {
060: this .iep = iep;
061: this .chat = chat;
062: }
063:
064: public void run() {
065: chat.send("started");
066: while (true) {
067: Message msg = chat.waitForNextMessage();
068: String text = msg.getText();
069: if (text.equals("bye")) {
070: chat.send("bye!");
071: chat.close();
072: return;
073: }
074: if (text.startsWith("join ")) {
075: text = text.substring(5);
076: try {
077: ConversationSPI.currentConversation()
078: .getEngine().createConversation(
079: new ChannelConversationImpl(
080: iep,
081: iep.getChannel(text)));
082: } catch (IOException e) {
083: e.printStackTrace();
084: }
085: chat.send("OK.");
086: continue;
087: }
088: chat.send("You said " + text);
089: }
090: }
091: }
092:
093: public static final class ChannelConversationImpl implements
094: Runnable, Serializable {
095: private final Channel channel;
096: private final IRCEndPoint iep;
097:
098: public ChannelConversationImpl(IRCEndPoint iep, Channel channel) {
099: this .iep = iep;
100: this .channel = channel;
101: }
102:
103: public void run() {
104: channel.join();
105: channel.send("joined");
106: while (true) {
107: Message msg = channel.waitForNextMessage();
108: String text = msg.getText();
109: if (text.equals("bye")) {
110: channel.send("bye!");
111: channel.close();
112: return;
113: }
114: channel.send("You said " + text);
115: }
116: }
117: }
118: }
|