Exemple #1
0
 public void close() {
   try {
     this.pool.removeHandler(this);
     socket.close();
   } catch (Exception e) {
     ErrorControl.logException(e);
   }
 }
  private SocketWorker create() {
    SocketWorker instance = null;

    try {
      instance = new SocketWorker(this, this.protocol);
    } catch (Exception e) {
      ErrorControl.logException(e);
    }

    return instance;
  }
 public void addResponse(SocketHandler handler, Future<byte[]> response) {
   if (response.isDone()) {
     try {
       ArrayBlockingQueue<byte[]> mailbox = this.mailboxes.get(handler);
       mailbox.add(response.get());
     } catch (Exception e) {
       ErrorControl.logException(e);
     }
   } else {
     this.futures.add(new Pair<SocketHandler, Future<byte[]>>(handler, response));
   }
 }
Exemple #4
0
  public SocketHandler(Socket socket, SocketWorkerPool pool, ClusterProtocol protocol) {
    this.socket = socket;
    this.pool = pool;
    this.protocol = protocol;

    this.inbytes = new byte[IO_SIZE];

    this.responses = new ArrayBlockingQueue<byte[]>(QUEUE_SIZE);
    this.pool.addHandler(this, this.responses);

    try {
      this.input = new BufferedInputStream(socket.getInputStream());
      this.output = new BufferedOutputStream(socket.getOutputStream());
    } catch (Exception e) {
      ErrorControl.logException(e);
    }
  }
  @SuppressWarnings("unchecked")
  private HBaseComponentStreamer<?> generateStreamer(Key<?> key) {
    HBaseComponentStreamer<?> instance = null;

    try {
      Class<?> type = key.getType();
      ParameterizedType subType = key.getSubType();

      Class<? extends HBaseComponentStreamer<?>> cls =
          this.entityService.getStreamer(HBaseConnection.class, HBaseComponentStreamer.class, type);
      Constructor<? extends HBaseComponentStreamer<?>> ctor =
          cls.getConstructor(EntityService.class, Class.class, ParameterizedType.class);
      instance = ctor.newInstance(this.entityService, type, subType);
    } catch (Exception e) {
      ErrorControl.logException(e);
    }

    return instance;
  }
Exemple #6
0
  public boolean handle() {
    boolean doneWork = false;

    try {
      int available = this.input.available();
      while (available > 0) {
        if (this.expected < 0 && available >= 4) {
          this.expected = this.input.read();
          available -= 4;
        } else {
          break;
        }

        if (this.expected > 0) {
          this.inputOffset +=
              this.input.read(this.inbytes, this.inputOffset, this.expected - this.inputOffset);
        }

        if (this.inputOffset == this.expected) {
          byte[] request = new byte[this.expected];
          System.arraycopy(this.inbytes, 0, request, 0, this.expected);
          this.pool.addRequest(this, request);
        }

        available = this.input.available();
        doneWork = true;
      }

      while (!this.responses.isEmpty()) {
        byte[] response = this.responses.poll();
        this.output.write(response.length);
        this.output.write(response);
        doneWork = true;
      }

      this.output.flush();
    } catch (Exception e) {
      ErrorControl.logException(e);
    }

    return doneWork;
  }
  @SuppressWarnings("unchecked")
  private void setupLayers() {
    Iterator<Class<?>> itr = new ClassWalker(ClassWalkerFilter.extending(ClusterLayer.class));

    while (itr.hasNext()) {
      Class<? extends ClusterLayer> cls = (Class<? extends ClusterLayer>) itr.next();

      this.logger.info(String.format("Found cluster layer: %s", cls.getName()));

      try {
        ClusterLayer instance = cls.newInstance();
        this.layerByClass.put(cls, instance);
      } catch (Exception e) {
        ErrorControl.logException(e);
      }
    }

    Class<? extends ClusterLayer>[] orderedLayers =
        (Class<? extends ClusterLayer>[])
            this.layerByClass.keySet().toArray(new Class<?>[this.layerByClass.size()]);
    Arrays.sort(
        orderedLayers,
        new Comparator<Class<? extends ClusterLayer>>() {

          @Override
          public int compare(
              Class<? extends ClusterLayer> arg0, Class<? extends ClusterLayer> arg1) {
            return arg0.getName().compareTo(arg1.getName());
          }
        });

    for (int i = 0; i < orderedLayers.length; i++) {
      Class<? extends ClusterLayer> layer = orderedLayers[i];
      this.ordinalByClass.put(layer, i);
      this.classByOrdinal.put(i, layer);
      this.layerByOrdinal.put(i, this.layerByClass.get(layer));
    }
  }