Exemplo n.º 1
0
 @Override
 public boolean onReadable() {
   if (!mReady) {
     try {
       mOutput.source().register(AdbServer.server().selector(), 0, this);
     } catch (ClosedChannelException e) {
       e.printStackTrace();
       return false;
     }
   }
   mOutputBuffer.clear();
   int result = -1;
   try {
     result = mOutput.source().read(mOutputBuffer);
   } catch (IOException e) {
     e.printStackTrace();
   }
   if (result < 0) {
     // Pipe is closed.
     return false;
   } else if (result > 0) {
     mOutputBuffer.flip();
     AdbMessage message = new AdbMessage();
     message.data = new byte[result];
     mOutputBuffer.get(message.data);
     mPeer.enqueue(message);
     mReady = false;
   }
   return true;
 }
Exemplo n.º 2
0
 protected void processRegistrationQueue() {
   // add any elements in queue
   for (Iterator<Object[]> it = registrationQueue.iterator(); it.hasNext(); ) {
     Object[] args = it.next();
     SelectableChannel ch = (SelectableChannel) args[0];
     try {
       ch.register(selector, (Integer) args[1], args[2]);
     } catch (ClosedChannelException e) {
       e.printStackTrace();
     }
     it.remove();
   }
 }
Exemplo n.º 3
0
  public void ready() {
    if (mInitialized == false) {
      mThread.start();
      mInitialized = true;
    }

    try {
      mOutput.source().register(AdbServer.server().selector(), SelectionKey.OP_READ, this);
      mReady = true;
    } catch (ClosedChannelException e) {
      e.printStackTrace();
      close();
    }
  }
Exemplo n.º 4
0
  /** This is the body of the listening thread */
  public void run() {
    SocketAddress sender;

    checkBuffer();

    try {
      listen:
      while (isListening) {
        try {
          byteBuf.clear();

          // temporary until we work out a better way...
          // Fixed buffer size limits the maximum size of received packets
          // Seems there are always limitations and a default going back to the constant
          // DEFAULTBUFSIZE
          byte[] buffer = new byte[getBufferSize()];
          DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

          ms.receive(dp);
          byteBuf.put(buffer);
          sender = dp.getSocketAddress();

          if (!isListening) break listen;
          if ((target != null)) continue listen;

          flipDecodeDispatch(sender);
        } catch (ClosedChannelException e1) { // bye bye, we have to quit
          if (isListening) {
            //							System.err.println( e1 );
            System.err.println(
                "OSCReceiver.run : " + e1.getClass().getName() + " : " + e1.getLocalizedMessage());
          }
          return;
        } catch (IOException e1) {
          if (isListening) {
            System.err.println(
                "OSCReceiver.run : " + e1.getClass().getName() + " : " + e1.getLocalizedMessage());
            //							System.err.println( new OSCException( OSCException.RECEIVE, e1.toString() ));
          }
        }
      } // while( isListening )
    } finally {
      synchronized (threadSync) {
        thread = null;
        threadSync.notifyAll(); // stopListening() might be waiting
      }
    }
  }
Exemplo n.º 5
0
    @JRubyMethod
    public IRubyObject register(ThreadContext context, IRubyObject io, IRubyObject interests) {
      Ruby runtime = context.getRuntime();
      Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();

      if (!this.selector.isOpen()) {
        throw context.getRuntime().newIOError("selector is closed");
      }

      if (!(rawChannel instanceof SelectableChannel)) {
        throw runtime.newArgumentError("not a selectable IO object");
      }

      SelectableChannel channel = (SelectableChannel) rawChannel;

      try {
        channel.configureBlocking(false);
      } catch (IOException ie) {
        throw runtime.newIOError(ie.getLocalizedMessage());
      }

      int interestOps = Nio4r.symbolToInterestOps(runtime, channel, interests);
      SelectionKey key;

      key = this.cancelledKeys.remove(channel);

      if (key != null) {
        key.interestOps(interestOps);
      } else {
        try {
          key = channel.register(this.selector, interestOps);
        } catch (java.lang.IllegalArgumentException ia) {
          throw runtime.newArgumentError("mode not supported for this object: " + interests);
        } catch (java.nio.channels.ClosedChannelException cce) {
          throw context.runtime.newIOError(cce.getLocalizedMessage());
        }
      }

      RubyClass monitorClass = runtime.getModule("NIO").getClass("Monitor");
      Monitor monitor = (Monitor) monitorClass.newInstance(context, io, interests, this, null);
      monitor.setSelectionKey(key);

      return monitor;
    }
Exemplo n.º 6
0
 static {
   SSLENGINE_CLOSED.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
   HANDSHAKE_TIMED_OUT.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
   CHANNEL_CLOSED.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
 }
  public MessageDistributor(Element element, boolean senderIsMailUser) {

    // Captures the info from the message
    getMessageParams(element);
    // Gets the sender info
    SocketInfo sender = getSenderInfo(senderIsMailUser);

    // Getting the destination list for this message
    Vector<SocketInfo> usersVector = getDestinationList(element, senderIsMailUser);
    int groupSize = usersVector.size();

    String many = "";
    if (groupSize > 1 || groupSize == 0) {
      many = "s";
    }

    LogWriter.write("INFO: Enviando mensaje a " + groupSize + " usuario" + many);

    // In this cycle, the message is sent to every user in the destination list
    for (SocketInfo destination : usersVector) {
      SocketChannel sock =
          destination.getSock() != null ? destination.getSock().getChannel() : null;

      // If the POS user is online
      if (sock != null) {
        Element message = new Element("Message");
        Element root = new Element("root");

        message.addContent(root);
        root.addContent(addColumn(dateString));
        root.addContent(addColumn(hourString));
        root.addContent(addColumn(sender.getGroupName()));
        root.addContent(addColumn(subject));
        root.addContent(addColumn(body));
        root.addContent(addColumn("f"));

        Document doc = new Document((Element) message.clone());
        // Sending message to POS user online
        try {
          SocketWriter.write(sock, doc);
          LogWriter.write(
              "INFO: [Envio a Punto de Venta] Remitente {"
                  + sender.getLogin()
                  + "} - Destino: "
                  + destination.getLogin()
                  + " - Asunto: "
                  + subject);
        } catch (ClosedChannelException e) {
          LogWriter.write(
              "INFO: El colocador " + destination.getLogin() + " no se encuentra en linea.");
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      // Sending message to pop users (admin users)
      if (!senderIsMailUser) {
        EmailSender mail = new EmailSender();
        mail.setFrom(Pop3Handler.getUser() + "@" + Pop3Handler.getHost());
        mail.setSender(destination.getEmail());
        mail.setSubject(sender.getLogin() + "," + subject);
        mail.setDate(date);
        mail.setMessage(body);
        mail.setSenderFullName(sender.getNames());
        mail.setWorkStation(sender.getWsName());
        mail.send();
        LogWriter.write(
            "INFO: [Envio a Cuenta de Correo] Remitente {"
                + sender.getLogin()
                + "} - Destino: "
                + destination.getLogin()
                + " - Asunto: "
                + subject);
      }

      // if destination user is offline
      if (!control || (control && (sock != null))) {
        String isValid = groupSize > 0 ? "true" : "false";
        String[] argsArray = {
          String.valueOf(destination.getUid()),
          String.valueOf(sender.getUid()),
          dateString,
          hourString,
          subject.trim(),
          body.trim(),
          isValid,
          String.valueOf(ConfigFileHandler.getMessageLifeTimeForClients()),
          String.valueOf(control),
          String.valueOf(lifeTime)
        };

        QueryRunner qRunner = null;
        try {
          LogWriter.write(
              "INFO: Almacenando registro de mensaje en la base de datos ["
                  + destination.getLogin()
                  + "]");
          qRunner = new QueryRunner("INS0003", argsArray);
          qRunner.setAutoCommit(false);
          qRunner.executeSQL();
          qRunner.commit();
        } catch (SQLNotFoundException e) {
          qRunner.rollback();
          LogWriter.write(
              "ERROR: No se pudo almacenar mensaje en la base de datos. Instruccion SQL no encontrada");
          e.printStackTrace();
        } catch (SQLBadArgumentsException e) {
          qRunner.rollback();
          LogWriter.write(
              "ERROR: No se pudo almacenar mensaje en la base de datos. Argumentos Invalidos");
          e.printStackTrace();
        } catch (SQLException e) {
          qRunner.rollback();
          LogWriter.write("ERROR: No se pudo almacenar mensaje en la base de datos. Excepcion SQL");
          e.printStackTrace();
        } finally {
          qRunner.closeStatement();
          qRunner.setAutoCommit(true);
        }
      }
    }
  }