示例#1
0
  @Override
  public synchronized Topic subscribeSharedObject(String name, boolean blocking)
      throws ObjectNotExistsException, OperationForbiddenException, Exception {
    // preparing locker to wait for response
    try {
      JCsyncAbstractOperation operation =
          JCsyncAbstractOperation.get_OP_REQ_SUBSCRIBE(name, this.getNodeInfo().getName());
      // inform Consistency Manager before sending request
      this.dcManager.inititBuffer(name);
      this.dcManager.beforeRequestSend(operation, blocking);
      this.pubsubLayer.getCustomAlgorith().registerSharedObjectName(name);
      this.pubsubLayer.getCustomAlgorith().networkSubscribe(name);
      Short respCode = (Short) this.dcManager.afterRequestSend(operation, blocking);
      if (respCode != null) {
        // if the topic was created
        if (respCode == JCSyncConstans.J_RESP_GENERAL_SUCCESS) {
          return this.pubsubLayer.getTopic(name);
          // if node can't subscribe to this topic
        } else if (respCode == JCSyncConstans.J_ERR_COLLECTION_AUTH_ERROR) {
          throw OperationForbiddenException.instance();
          // if the topic not exists
        } else if (respCode == JCSyncConstans.J_ERR_OBJECT_NOT_EXISTS) {
          throw ObjectNotExistsException.instance();
        } else {
          log.fatal(
              "received UNHANLED respCode for operation: "
                  + operation.getOperationType()
                  + ", objectID:"
                  + operation.getObjectID());
        }
      } else {
        log.fatal(
            "received [null] respCode for operation: "
                + operation.getOperationType()
                + ", objectID:"
                + operation.getObjectID());
      }

    } catch (InterruptedException e) {
      throw new Exception("An error on (subscribeSharedObject)");
    }
    log.error("(subscribeSharedObject) returns Topic = null from unknow reason!");
    return null;
  }
示例#2
0
  @Override
  public synchronized boolean unsubscribeSharedObject(String name, boolean blocking)
      throws ObjectNotExistsException, Exception {
    // preparing locker to wait for response
    try {
      JCsyncAbstractOperation operation =
          JCsyncAbstractOperation.get_OP_REQ_UNSUBSCRIBE(name, this.getNodeInfo().getName());
      // inform Consistency Manager before sending request
      this.dcManager.beforeRequestSend(operation, blocking);
      this.pubsubLayer.getCustomAlgorith().registerSharedObjectName(name);
      this.pubsubLayer.getCustomAlgorith().networkUnsubscribe(name);
      Short respCode = (Short) this.dcManager.afterRequestSend(operation, blocking);
      if (respCode != null) {
        if (respCode == JCSyncConstans.J_RESP_GENERAL_SUCCESS) {
          return true;
          // if the node is not a subscriber
        } else if (respCode == JCSyncConstans.J_ERR_OBJECT_NOT_EXISTS) {
          throw ObjectNotExistsException.instance();
        } else {
          log.fatal(
              "received UNHANLED respCode for operation: "
                  + operation.getOperationType()
                  + ", objectID:"
                  + operation.getObjectID());
        }
      } else {
        log.fatal(
            "received [null] respCode for operation: "
                + operation.getOperationType()
                + ", objectID:"
                + operation.getObjectID());
      }

    } catch (InterruptedException e) {
      throw new Exception("An error on (unsubscribeSharedObject)");
    }
    log.error("(unsubscribeSharedObject) returns false from unknown reason.");
    return false;
  }
示例#3
0
  /**
   * Sends operation as a pub-sub message thought overlay. If the <tt>blocking</tt> value is
   * <tt>true</tt> then working thread will be suspended until the response and indication (if
   * required) will be received from the overlay, if <tt>blocking</tt> value is <tt>false</tt> then
   * working thread will not be suspended and method returns <tt>null</tt>.<br>
   * Typically used to sends requests.
   *
   * @param op operation that will be send
   * @param blocking determines whenever the calling thread will be suspended until the results of
   *     given operation will be received.
   * @return the results of given operation or <tt>null</tt> if the blocking argument value is set
   *     to <tt>false</tt>
   * @throws Exception any error which was occurred during this operation
   */
  @Override
  public Object sendMessage(JCsyncAbstractOperation op, boolean blocking) throws Exception {
    // if the topic for this object is unknown then throw new exception
    try {
      this.request_response_locker.writeLock().lock();
      if (getAssignedTopic(op.getObjectID()) == null) {
        throw ObjectNotExistsException.instance();
      }
      // try to get the consistency manager if there is already selected
      AbstractConsistencyManager acm = getConsistencyManager(op.getObjectID());
      if (acm == null) {
        acm = this.dcManager;
      }
      short type = op.getOperationType();
      if ((type & OP_REQ_GENERIC) == OP_REQ_GENERIC) {
        acm.beforeRequestSend(op, blocking);
        this.pubsubLayer.getCustomAlgorith().registerSharedObjectName(op.getObjectID());
        Transaction t =
            this.pubsubLayer
                .getCustomAlgorith()
                .nextPublishTransaction(op.getObjectID(), op.getOperationType());
        op.setReqestID(t.getID());
        log.trace(
            "(sendMessage) - operation sent (blocking=["
                + blocking
                + "]) operation: "
                + op.toString());
        this.messagesToSend.put(new MessageToSend(op, t));
        this.request_response_locker.writeLock().unlock();
        Object result = acm.afterRequestSend(op, blocking);
        return result;

      } else if ((type & OP_INDICATION_GENERIC) == OP_INDICATION_GENERIC) {

        acm.beforeRequestSend(op, blocking);
        this.pubsubLayer.getCustomAlgorith().registerSharedObjectName(op.getObjectID());
        Transaction t =
            this.pubsubLayer
                .getCustomAlgorith()
                .nextPublishTransaction(op.getObjectID(), op.getOperationType());
        op.setReqestID(t.getID());
        log.trace(
            "(sendMessage) - operation sent (blocking=["
                + blocking
                + "]) operation: "
                + op.toString());
        this.messagesToSend.put(new MessageToSend(op, t));
        this.request_response_locker.writeLock().unlock();
        Object result = acm.afterRequestSend(op, blocking);
        return result;

      } else {
        log.error("Unhandled operation type: " + op.toString());
      }
    } catch (Exception e) {
      throw e;
    } finally {
      if (this.request_response_locker.writeLock().getHoldCount() > 0
          && this.request_response_locker.writeLock().isHeldByCurrentThread()) {
        this.request_response_locker.writeLock().unlock();
      }
    }

    return null;
  }