⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 helloinitiator.java

📁 javaP2P技术内幕课程0789的源代码
💻 JAVA
字号:
import org.beepcore.beep.core.*;
import org.beepcore.beep.lib.NullReplyListener;
import org.beepcore.beep.transport.tcp.AutomatedTCPSessionCreator;
import org.beepcore.beep.transport.tcp.TCPSession;
import java.io.BufferedReader;
import java.io.InputStreamReader;


/**
*This is a BEEP Peer which initiates a session with the listener peer.
*The host name of the listener peer is contained in the HOST variable.
*Currently it points to the same host as the initiator.This can be
*changed to point to any host.
*
*/
public class HelloInitiator {
    /**
    *Host name of the listener
    */
    public static final String HOST = "localhost";

    /**
    *The main method creates an instance of the
    *<code>HelloListener</code>
    *The initiator is made to connect to the listener and send a
    *message.It exits after a small sleep interval.
    *
    */
    public static void main(String[] args) {
        try {
            //initiate a connection to the listener running on HOST
            TCPSession session = AutomatedTCPSessionCreator.initiate(HOST, 
                                                                     HelloListener.PORT, 
                                                                     new ProfileRegistry());

            //Start a new Channel with a profile as by
            //the PROFILE_STRING uri.
            Channel channel = session.startChannel(HelloListener.PROFILE_STRING);

            //Create a new StringDataStream
            DataStream messageData = new StringDataStream("Hello Listener !!");


            //send the message
            channel.sendMSG(messageData, new HelloReplyListener());


            //sleep for a small delay and exit.
            Thread.sleep(2000);
        } catch (BEEPException beepException) {
            log("Unable to send a successful Message.Reason is : " + 
                beepException.getMessage());
        } catch (InterruptedException excp) {
            //NO OP
        }
    }

    /**
    *A listener implementing the <code>ReplyListener</code>interface.
    *A reply listener can be used per message sent.
    *
    */
    static class HelloReplyListener implements ReplyListener {
        /**
        *Called when a reply is received .
        *
        */
        public void receiveRPY(Message message) {
            try {
                //Get a data stream from the message.
                DataStream stream = message.getDataStream();

                //Create a buffered reader for reading the data.
                BufferedReader reader = new BufferedReader(
                                                new InputStreamReader(
                                                        stream.getInputStream()));


                //Print the recieved message to console.
                log("Message from listener = > " + reader.readLine());


                //Close the reader
                reader.close();
                ((FrameDataStream) message.getDataStream()).close();
            } catch (Exception excp) {
                log("Message from Exception : " + excp.getMessage());
            }
        }

        /**
        *Called when the underlying a reply of type ERR is received.
        *Not used in this example.
        *
        */
        public void receiveERR(Message message) {
            ((FrameDataStream) message.getDataStream()).close();
        }

        /**
        *Called when the underlying a reply of type ANS is received.
        *Not used in this example.
        *
        */
        public void receiveANS(Message message) {
            ((FrameDataStream) message.getDataStream()).close();
        }

        /**
        *Called when the underlying a reply of type NUL is received.
        *Not used in this example.
        *
        */
        public void receiveNUL(Message message) {
        }
    }

    //private method to log messages to console
    private static void log(String message) {
        System.out.println(message);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -