Package org.apache.mina.core.session

Examples of org.apache.mina.core.session.IoSession


     * {@inheritDoc}
     */
    @Override
    public void proxySessionOpened(IoSession session) throws Exception {
        logger.debug("CLIENT - Session opened: " + session);
        final IoSession _session = session;
        // Enter typing loop
        new Thread(new Runnable() {
            public void run() {
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);

                while (!_session.isClosing()) {
                    try {
                        String line = br.readLine();
                        if (line != null) {
                            _session.write(line);
                        }
                    } catch (Exception e) {
                        break;
                    }
                }

                _session.close(true);
            }

        }).start();
    }
View Full Code Here


                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                        .forName("UTF-8"), delim, delim)));

        connector.setHandler(new TelnetSessionHandler());

        IoSession session;
        for (;;) {
            try {
                ConnectFuture future = connector.connect();
                future.awaitUninterruptibly();
                session = future.getSession();
                break;
            } catch (RuntimeIoException e) {
                System.err.println("Failed to connect. Retrying in 5 secs ...");
                Thread.sleep(5000);
            }
        }

        // Wait until done
        if (session != null) {
            session.getCloseFuture().awaitUninterruptibly();
        }
        connector.dispose();
        System.exit(0);
    }
View Full Code Here

        connector.connect(remoteAddress).addListener(new IoFutureListener<ConnectFuture>() {
            public void operationComplete(ConnectFuture future) {
                try {
                    future.getSession().setAttribute(OTHER_IO_SESSION, session);
                    session.setAttribute(OTHER_IO_SESSION, future.getSession());
                    IoSession session2 = future.getSession();
                    session2.resumeRead();
                    session2.resumeWrite();
                } catch (RuntimeIoException e) {
                    // Connect failed
                    session.close(true);
                } finally {
                  session.resumeRead();
View Full Code Here

    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        if (session.getAttribute( OTHER_IO_SESSION ) != null) {
            IoSession sess = (IoSession) session.getAttribute(OTHER_IO_SESSION);
            sess.setAttribute(OTHER_IO_SESSION, null);
            sess.close(false);
            session.setAttribute(OTHER_IO_SESSION, null);
        }
    }
View Full Code Here

        String cmd = "GET " + url.toExternalForm() + " HTTP/1.0"
                + HttpProxyConstants.CRLF + HttpProxyConstants.CRLF;

        connector.setHandler(new ClientSessionHandler(cmd));

        IoSession session;
        for (;;) {
            try {
                ConnectFuture future = connector.connect();
                future.awaitUninterruptibly();
                session = future.getSession();
                break;
            } catch (RuntimeIoException e) {
                System.err.println("Failed to connect. Retrying in 5 secs ...");
                Thread.sleep(5000);
            }
        }

        // Wait until done
        if (session != null) {
            session.getCloseFuture().awaitUninterruptibly();
        }
        connector.dispose();
        System.exit(0);
    }
View Full Code Here

    tblSession.addSelectionListener(new SelectionListener() {
     
      @Override
      public void widgetSelected(SelectionEvent e) {
        int idx = tblSession.getSelectionIndex();
        IoSession session = (IoSession)tblSession.getItem(idx).getData("session");
        currSession = session;
        lblInfo.setText("Session ID:" + session.getId());
        txtSessionID.setText("" + session.getId());
        byte [] bytes = getRecvData();
        String charset = cboCharsetOfRecv.getText();
        String recvData = "";
        try {
          recvData = new String(bytes, charset);
        } catch (UnsupportedEncodingException e1) {
          e1.printStackTrace();
        }
        txtRecv.setText(recvData);
      }
     
      @Override
      public void widgetDefaultSelected(SelectionEvent e) {
       
      }
    });
   
    DropTargetListener dtl = new DropTargetListener() {

      @Override
      public void dropAccept(DropTargetEvent event) {
      }

      @Override
      public void drop(DropTargetEvent event) {
        if (FileTransfer.getInstance().isSupportedType(
            event.currentDataType)) {
            String[] files = (String[]) event.data;
          //for (int i = 0; i < files.length; i++) {
          for (int i = 0; i < 1; i++) {
            File fd = new File(files[i]);
            FileInputStream is = null;
            try {
              is = new FileInputStream(fd);
              byte byteData[] = new byte[(int) fd.length()];
              is.read(byteData);
             
              IoBuffer buff = IoBuffer.allocate(byteData.length);
              buff.put(byteData);
              buff.flip();
             
              currSession.write(buff);
            } catch (Exception e) {
              e.printStackTrace();
            } finally{
              if(is != null){
                try {
                  is.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
           
          }
        }
      }

      @Override
      public void dragOver(DropTargetEvent event) {
      }

      @Override
      public void dragOperationChanged(DropTargetEvent event) {
      }

      @Override
      public void dragLeave(DropTargetEvent event) {
      }

      @Override
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail = DND.DROP_COPY;
          } else {
            event.detail = DND.DROP_NONE;
          }
        }
      }
    };
    DropTarget dropTarget = new DropTarget(txtSend, DND.DROP_COPY
        | DND.DROP_DEFAULT);
    Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() };

    dropTarget.setTransfer(transfers);
    dropTarget.addDropListener(dtl);

   

    client.setHandler(new IoHandler() {
     
      @Override
      public void sessionOpened(IoSession session) throws Exception {
       
      }
     
      @Override
      public void sessionIdle(IoSession session, IdleStatus idle) throws Exception {
       
      }
     
      @Override
      public void sessionCreated(final IoSession session) throws Exception {
        long id = session.getId();
       
        System.out.println("[id:" + id + "]sessionCreated");
        session.setAttribute("buffer", IoBuffer.allocate(1024));
        parent.getDisplay().syncExec(new Runnable() {
          @Override
          public void run() {
            InetSocketAddress remoteAddr = (InetSocketAddress)session.getRemoteAddress();
            InetSocketAddress localAddr = (InetSocketAddress)session.getLocalAddress();
            String sessionID = "" + session.getId();
            String remoteIP = remoteAddr.getAddress().getHostAddress();
            String remotePort = "" + remoteAddr.getPort();
            String localPort = "" + localAddr.getPort();
           
            TableItem item = new TableItem(tblSession, SWT.NONE);
            item.setText(0, sessionID);
            item.setText(1, remoteIP);
            item.setText(2, remotePort);
            item.setText(3, localPort);
            item.setData("session", session);
          }
        });
      }
     
      @Override
      public void sessionClosed(final IoSession session) throws Exception {
        long id = session.getId();
        System.out.println("[id:" + id + "]sessionClosed");
       
       
        parent.getDisplay().syncExec(new Runnable() {
          @Override
          public void run() {
            TableItem [] items = tblSession.getItems();
            for(int i=0; i<items.length; i++){
              TableItem item = items[i];
              IoSession sessionOfTable = (IoSession)item.getData("session");
              if(session.getId() == sessionOfTable.getId()){
                tblSession.remove(i);
                break;
              }
            }
View Full Code Here

    tblSession.addSelectionListener(new SelectionListener() {
     
      @Override
      public void widgetSelected(SelectionEvent e) {
        int idx = tblSession.getSelectionIndex();
        IoSession session = (IoSession)tblSession.getItem(idx).getData("session");
        currSession = session;
        lblInfo.setText("Session ID:" + session.getId());
        txtSessionID.setText("" + session.getId());
        byte [] bytes = getRecvData();
        String charset = cboCharsetOfRecv.getText();
        String recvData = "";
        try {
          recvData = new String(bytes, charset);
        } catch (UnsupportedEncodingException e1) {
          e1.printStackTrace();
        }
        txtRecv.setText(recvData);
      }
     
      @Override
      public void widgetDefaultSelected(SelectionEvent e) {
       
      }
    });
   
    DropTargetListener dtl = new DropTargetListener() {

      @Override
      public void dropAccept(DropTargetEvent event) {
      }

      @Override
      public void drop(DropTargetEvent event) {
        if (FileTransfer.getInstance().isSupportedType(
            event.currentDataType)) {
            String[] files = (String[]) event.data;
          //for (int i = 0; i < files.length; i++) {
          for (int i = 0; i < 1; i++) {
            File fd = new File(files[i]);
            FileInputStream is = null;
            try {
              is = new FileInputStream(fd);
              byte byteData[] = new byte[(int) fd.length()];
              is.read(byteData);
             
              IoBuffer buff = IoBuffer.allocate(byteData.length);
              buff.put(byteData);
              buff.flip();
             
              currSession.write(buff);
            } catch (Exception e) {
              e.printStackTrace();
            } finally{
              if(is != null){
                try {
                  is.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
           
          }
        }
      }

      @Override
      public void dragOver(DropTargetEvent event) {
      }

      @Override
      public void dragOperationChanged(DropTargetEvent event) {
      }

      @Override
      public void dragLeave(DropTargetEvent event) {
      }

      @Override
      public void dragEnter(DropTargetEvent event) {
        if (event.detail == DND.DROP_DEFAULT) {
          if ((event.operations & DND.DROP_COPY) != 0) {
            event.detail = DND.DROP_COPY;
          } else {
            event.detail = DND.DROP_NONE;
          }
        }
      }
    };
    DropTarget dropTarget = new DropTarget(txtSend, DND.DROP_COPY
        | DND.DROP_DEFAULT);
    Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() };

    dropTarget.setTransfer(transfers);
    dropTarget.addDropListener(dtl);

   

    serv.setHandler(new IoHandler() {
     
      @Override
      public void sessionOpened(IoSession session) throws Exception {
       
      }
     
      @Override
      public void sessionIdle(IoSession session, IdleStatus idle) throws Exception {
       
      }
     
      @Override
      public void sessionCreated(final IoSession session) throws Exception {
        long id = session.getId();
       
        System.out.println("[id:" + id + "]sessionCreated");
        session.setAttribute("buffer", IoBuffer.allocate(1024));
        parent.getDisplay().syncExec(new Runnable() {
          @Override
          public void run() {
            InetSocketAddress remoteAddr = (InetSocketAddress)session.getRemoteAddress();
            InetSocketAddress localAddr = (InetSocketAddress)session.getLocalAddress();
            String sessionID = "" + session.getId();
            String remoteIP = remoteAddr.getAddress().getHostAddress();
            String remotePort = "" + remoteAddr.getPort();
            String localPort = "" + localAddr.getPort();
           
            TableItem item = new TableItem(tblSession, SWT.NONE);
            item.setText(0, sessionID);
            item.setText(1, remoteIP);
            item.setText(2, remotePort);
            item.setText(3, localPort);
            item.setData("session", session);
          }
        });
      }
     
      @Override
      public void sessionClosed(final IoSession session) throws Exception {
        long id = session.getId();
        System.out.println("[id:" + id + "]sessionClosed");
       
       
        parent.getDisplay().syncExec(new Runnable() {
          @Override
          public void run() {
            TableItem [] items = tblSession.getItems();
            for(int i=0; i<items.length; i++){
              TableItem item = items[i];
              IoSession sessionOfTable = (IoSession)item.getData("session");
              if(session.getId() == sessionOfTable.getId()){
                tblSession.remove(i);
                break;
              }
            }
View Full Code Here

            }
        });

        ConnectFuture future = connector.connect(
                new InetSocketAddress("127.0.0.1", port)).awaitUninterruptibly();
        IoSession session = future.getSession();
        assertNotNull(session);

        Thread.sleep((INTERVAL + TIMEOUT + 1) * 1000);

        assertFalse("got an exception on the client", gotException.get());

        session.close(true);
        connector.dispose();
    }
View Full Code Here

        testConnector(connector, true);
    }

    private void testConnector(IoConnector connector, boolean useLocalAddress)
            throws Exception {
        IoSession session = null;
        if (!useLocalAddress) {
            ConnectFuture future = connector.connect(new InetSocketAddress(
                    "127.0.0.1", port));
            future.awaitUninterruptibly();
            session = future.getSession();
        } else {
            int clientPort = port;
            for (int i = 0; i < 65536; i++) {
                clientPort = AvailablePortFinder
                        .getNextAvailable(clientPort + 1);
                try {
                    ConnectFuture future = connector.connect(
                            new InetSocketAddress("127.0.0.1", port),
                            new InetSocketAddress(clientPort));
                    future.awaitUninterruptibly();
                    session = future.getSession();
                    break;
                } catch (RuntimeIoException e) {
                    // Try again until we succeed to bind.
                }
            }

            if (session == null) {
                Assert.fail("Failed to find out an appropriate local address.");
            }
        }

        // Run a basic connector test.
        testConnector0(session);

        // Send closeNotify to test TLS closure if it is TLS connection.
        if (useSSL) {
            connectorSSLFilter.stopSsl(session).awaitUninterruptibly();

            System.out
                    .println("-------------------------------------------------------------------------------");
            // Test again after we finished TLS session.
            testConnector0(session);

            System.out
                    .println("-------------------------------------------------------------------------------");

            // Test if we can enter TLS mode again.
            //// Send StartTLS request.
            handler.readBuf.clear();
            IoBuffer buf = IoBuffer.allocate(1);
            buf.put((byte) '.');
            buf.flip();
            session.write(buf).awaitUninterruptibly();

            //// Wait for StartTLS response.
            waitForResponse(handler, 1);

            handler.readBuf.flip();
            Assert.assertEquals(1, handler.readBuf.remaining());
            Assert.assertEquals((byte) '.', handler.readBuf.get());

            // Now start TLS connection
            Assert.assertTrue(connectorSSLFilter.startSsl(session));
            testConnector0(session);
        }

        session.close(true).awaitUninterruptibly();
    }
View Full Code Here

        H handle = boundHandles.get(localAddress);
        if (handle == null) {
            throw new IllegalArgumentException("Unknown local address: " + localAddress);
        }

        IoSession session;
        IoSessionRecycler sessionRecycler = getSessionRecycler();
        synchronized (sessionRecycler) {
            session = sessionRecycler.recycle(localAddress, remoteAddress);
            if (session != null) {
                return session;
            }

            // If a new session needs to be created.
            T newSession = newSession(processor, handle, remoteAddress);
            getSessionRecycler().put(newSession);
            session = newSession;
        }

        finishSessionInitialization(session, null, null);

        try {
            this.getFilterChainBuilder().buildFilterChain(session.getFilterChain());
            getListeners().fireSessionCreated(session);
        } catch (Throwable t) {
            ExceptionMonitor.getInstance().exceptionCaught(t);
        }
View Full Code Here

TOP

Related Classes of org.apache.mina.core.session.IoSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.