Clojure新手 - 将Java代码重写为Clojure

时间:2014-03-28 13:23:58

标签: clojure clojure-java-interop

我有一些简单的工作java代码,它使用SMACK xmmp库来响应传入的消息并接受多用户聊天邀请。我试图转换这个clojure。机器人登录但不响应消息。

public class bot {

public static void main(String[] args) {

    System.out.println("Starting session...");
    try {
        String server = "chat.hipchat.com";         
        XMPPConnection con = new XMPPConnection(server);
        con.connect();
        String username = "username";
        String password = "password";
        con.login(username, password,"bot");
        System.out.println("Connected");

        ChatManager chatManager = con.getChatManager();
        final MyMessageListener messageListener = new MyMessageListener();

        ChatManagerListener  chatManagerListener = new ChatManagerListener() {
            public void chatCreated(Chat chat, boolean createdLocally) {
                chat.addMessageListener(messageListener);
            }
        };
        chatManager.addChatListener(chatManagerListener);


        MultiUserChat.addInvitationListener(con, new InvitationListener() {

            public void invitationReceived(XMPPConnection con,
                    String room, String arg2, String arg3, String arg4,
                    Message arg5) {

                MultiUserChat muc2 = new MultiUserChat(con, room);
                  try {
                    muc2.join("Bot");
                } catch (XMPPException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        // Thread.currentThread();
        Thread.sleep(10000);
        // Disconnect from the server
        con.disconnect();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Ended session...");
}
}

public class MyMessageListener implements MessageListener {

public void processMessage(Chat chat, Message message) {
    System.out.println("Received message: " + message.getBody());
    try {
        chat.sendMessage("Smack> Message sent via API.");
    } catch (XMPPException e) {
        e.printStackTrace();
    }
}

}


(ns mybot.core
(:import [org.jivesoftware.smack
        Chat ChatManager MessageListener ChatManagerListener XMPPConnection XMPPException]
       [org.jivesoftware.smack.packet
        Message Presence]
       [org.jivesoftware.smackx.muc InvitationListener MultiUserChat] ))

(def con (XMPPConnection. "chat.hipchat.com"))


(defrecord MyMessageListener []
   MessageListener
   (processMessage [this chat message] ( println "Received Message" )))


 (def myMessageListner ( MyMessageListener. ))



 (defn add-chatManagerListener
  [connection]
  (println "Adding a chat manager lister")
  (.addChatListener (.getChatManager connection)
                (proxy [ChatManagerListener] []
                  (chatCreated [chat locally]
                    (.addMessageListner chat myMessageListner )
                    )
                  )))




(defn -main [& args]
  (println "Starting bot")
  (.connect con)
  (.login con "username" "pwd" "bot")
 (println (.isAuthenticated con))
 (add-chatManagerListener con))

2 个答案:

答案 0 :(得分:0)

如果您不需要,我强烈建议您不要使用(def ...)维护全局状态。这总是有点逆火。但也许它有某种个人品味。

Clojure中的记录用作bean,我很确定你不能用它来实现接口上的方法。定义是:(defrecord name [& fields] & opts+specs)其中fields是字段名称列表,而不是方法名称。

要实例化Java接口或Clojure协议,您应该使用reify。应该使用proxy宏来扩展抽象类或覆盖类上的函数。但MessageListenerChatManagerListener只是界面,因此从我的观点来看reify应该是首选。

我附上了我的源代码版本,但我无法正确测试,所以要小心处理。

 (ns mybot.core
   (:import [org.jivesoftware.smack
        Chat ChatManager MessageListener ChatManagerListener XMPPConnection XMPPException]
        [org.jivesoftware.smack.packet
         Message Presence]
        [org.jivesoftware.smackx.muc InvitationListener MultiUserChat] ))

 (defn connect [host username password]
   (doto (XMPPConnection. "chat.hipchat.com")
    (.login username password "bot")))

 (defn add-listener [connection callback]
   (let [chat-manager (.getChatManager connection)
         message-listener (reify
           MessageListener
             (processMessage [_ chat message] (callback chat message))
           )
         chat-listener (reify 
           ChatManagerListener 
             (chatCreated [_ chat _]
               (.addMessageListner chat message-listener)))
     ]
      (.addChatListener chat-manager chat-listener)      
      connection
   ))

 (def -main [& args]
   (let [con (connect "chat.hipchat.com" "username" "pwd")]
     (println (.isAuthenticated con))
     (add-listener con (fn [_ message] (println "Received Message:" message)))

      (Thread/sleep 10000)))

一个常见的Clojure模式是将回调函数作为侦听器传递,我为add-listener函数执行了此操作。这提供了很好的解耦并隐藏了所有讨厌的OO东西。

答案 1 :(得分:0)

虽然不是你要求的,但我建议你使用一个库。

一个无耻的插件,有clj-xmpp,我做了一个叉子 https://github.com/slipset/xmpp-clj/可能符合您的需求。