예제 #1
0
/* 350 */           while (((Iterator)localObject3).hasNext()) {
/* 351 */             SelectionKey localSelectionKey = (SelectionKey)((Iterator)localObject3).next();
/* 352 */             ((Iterator)localObject3).remove();
/* 353 */             if (localSelectionKey.equals(ServerImpl.this.listenerKey)) {
/* 354 */               if (!ServerImpl.this.terminating)
/*     */               {
/* 357 */                 SocketChannel localSocketChannel = ServerImpl.this.schan.accept();
/*     */ 
/* 360 */                 if (ServerConfig.noDelay()) {
/* 361 */                   localSocketChannel.socket().setTcpNoDelay(true);
/*     */                 }
/*     */ 
/* 364 */                 if (localSocketChannel != null)
/*     */                 {
/* 367 */                   localSocketChannel.configureBlocking(false);
/* 368 */                   localObject4 = localSocketChannel.register(ServerImpl.this.selector, 1);
/* 369 */                   localHttpConnection2 = new HttpConnection();
/* 370 */                   localHttpConnection2.selectionKey = ((SelectionKey)localObject4);
/* 371 */                   localHttpConnection2.setChannel(localSocketChannel);
/* 372 */                   ((SelectionKey)localObject4).attach(localHttpConnection2);
/* 373 */                   ServerImpl.this.requestStarted(localHttpConnection2);
/* 374 */                   ServerImpl.this.allConnections.add(localHttpConnection2);
/*     */                 }
/*     */               }
/*     */             }
/*     */             else {
/*     */               try
/*     */               {
/*     */                 Object localObject4;
/*     */                 HttpConnection localHttpConnection2;
/* 377 */                 if (localSelectionKey.isReadable())
/*     */                 {
/* 379 */                   localObject4 = (SocketChannel)localSelectionKey.channel();
/* 380 */                   localHttpConnection2 = (HttpConnection)localSelectionKey.attachment();
/*     */ 
/* 382 */                   localSelectionKey.cancel();
/* 383 */                   ((SocketChannel)localObject4).configureBlocking(true);
/* 384 */                   if (ServerImpl.this.idleConnections.remove(localHttpConnection2))
/*     */                   {
/* 387 */                     ServerImpl.this.requestStarted(localHttpConnection2);
/*     */                   }
/* 389 */                   handle((SocketChannel)localObject4, localHttpConnection2);
/*     */                 }
/* 391 */                 else if (!$assertionsDisabled) { throw new AssertionError(); }
/*     */               }
/*     */               catch (CancelledKeyException localCancelledKeyException) {
/* 394 */                 handleException(localSelectionKey, null);
/*     */               } catch (IOException localIOException2) {
/* 396 */                 handleException(localSelectionKey, localIOException2);
/*     */               }
/*     */             }
/*     */           }
  private boolean sendPacket(Packet packet, InetSocketAddress address, SelectionKey selectedKey) {

    boolean result = false;

    try {
      lock.lock();

      if (selectedKey == unicastKey) {
        LifxNetworkThrottler.lock(macAsHex);
      } else {
        LifxNetworkThrottler.lock();
      }

      boolean sent = false;

      while (!sent && selector.isOpen()) {
        try {
          selector.selectNow();
        } catch (IOException e) {
          logger.error("An exception occurred while selecting: {}", e.getMessage());
        }

        Set<SelectionKey> selectedKeys = selector.selectedKeys();
        Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

        while (keyIterator.hasNext()) {
          SelectionKey key = keyIterator.next();

          if (key.isValid() && key.isWritable() && key.equals(selectedKey)) {
            SelectableChannel channel = key.channel();
            try {
              if (channel instanceof DatagramChannel) {
                logger.trace(
                    "{} : Sending packet type '{}' from '{}' to '{}' for '{}' with sequence '{}' and source '{}'",
                    new Object[] {
                      macAsHex,
                      packet.getClass().getSimpleName(),
                      ((InetSocketAddress) ((DatagramChannel) channel).getLocalAddress())
                          .toString(),
                      address.toString(),
                      packet.getTarget().getHex(),
                      packet.getSequence(),
                      Long.toString(packet.getSource(), 16)
                    });
                ((DatagramChannel) channel).send(packet.bytes(), address);
                sent = true;
                result = true;
              } else if (channel instanceof SocketChannel) {
                ((SocketChannel) channel).write(packet.bytes());
              }
            } catch (Exception e) {
              logger.error("An exception occurred while writing data : '{}'", e.getMessage());
              break;
            }
          }
        }
      }
    } catch (Exception e) {
      logger.error(
          "An exception occurred while sending a packet to the bulb : '{}'", e.getMessage());
    } finally {

      if (selectedKey == unicastKey) {
        LifxNetworkThrottler.unlock(macAsHex);
      } else {
        LifxNetworkThrottler.unlock();
      }

      lock.unlock();
    }

    return result;
  }