Search in sources :

Example 1 with I2PSessionListener

use of net.i2p.client.I2PSessionListener in project i2p.i2p-bote by i2p.

the class Ping method listen.

private void listen() throws Exception {
    System.out.println("Listening for pings on I2P destination " + Util.toBase32(i2pSession.getMyDestination()) + ".b32.i2p");
    i2pSession.addSessionListener(new I2PSessionListener() {

        private int count;

        @Override
        public void reportAbuse(I2PSession session, int severity) {
        }

        @Override
        public void messageAvailable(I2PSession session, int msgId, long size) {
            try {
                count++;
                byte[] payload = session.receiveMessage(msgId);
                String sender = new String(Arrays.copyOf(payload, 60));
                System.out.println("#" + count + " " + new Date() + " sender=" + sender + " length=" + payload.length + " bytes");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void errorOccurred(I2PSession session, String message, Throwable error) {
            error.printStackTrace();
        }

        @Override
        public void disconnected(I2PSession session) {
            System.err.println("Disconnected!");
        }
    }, I2PSession.PROTO_DATAGRAM, I2PSession.PORT_ANY);
    // wait forever
    new CountDownLatch(1).await();
}
Also used : I2PSession(net.i2p.client.I2PSession) CountDownLatch(java.util.concurrent.CountDownLatch) I2PSessionListener(net.i2p.client.I2PSessionListener) Date(java.util.Date) I2PSessionException(net.i2p.client.I2PSessionException)

Aggregations

Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 I2PSession (net.i2p.client.I2PSession)1 I2PSessionException (net.i2p.client.I2PSessionException)1 I2PSessionListener (net.i2p.client.I2PSessionListener)1