Example usage for java.util.concurrent ConcurrentHashMap get

List of usage examples for java.util.concurrent ConcurrentHashMap get

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentHashMap get.

Prototype

public V get(Object key) 

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:hu.sztaki.lpds.pgportal.portlets.workflow.RealWorkflowPortlet.java

/**
 * Rescue a given workflow instance/*from ww w . j a va  2 s .c o m*/
 */
public void doRescue(ActionRequest request, ActionResponse response) throws PortletException {
    if (WorkflowInfo(request, response)) {
        doDetails(request, response);
        //
        Vector errorJobPidList = new Vector();
        String portalID = PropertyLoader.getInstance().getProperty("service.url");
        String userID = request.getRemoteUser();
        String workflowID = request.getParameter("workflow");
        String runtimeID = request.getParameter("rtid");
        //
        String wfStatus = "" + PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID)
                .getRuntime(runtimeID).getStatus();
        if (("7".equals(wfStatus)) || ("28".equals(wfStatus)) || ("23".equals(wfStatus))) {
            //
            // 23 = running/error
            if ("23".equals(wfStatus)) {
                // entering of running workflow status
                PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID).getRuntime(runtimeID)
                        .setStatus("5", 0);
            } else {
                // entering of resuming workflow status
                PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID).getRuntime(runtimeID)
                        .setStatus("29", 0);
            }
            //
            ConcurrentHashMap tmp = PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID)
                    .getRuntime(runtimeID).getJobsStatus();
            Enumeration enm0 = tmp.keys();
            String ts;
            while (enm0.hasMoreElements()) {
                Object key0 = enm0.nextElement();
                Enumeration enm1 = ((ConcurrentHashMap) tmp.get(key0)).keys();
                while (enm1.hasMoreElements()) {
                    Object key1 = enm1.nextElement();
                    ts = "" + ((JobStatusData) ((ConcurrentHashMap) tmp.get(key0)).get(key1)).getStatus();
                    if (ts.equals("25") || ts.equals("22") || ts.equals("21") || ts.equals("7")
                            || ts.equals("15") || ts.equals("13") || ts.equals("12")) {
                        // entering init status
                        // PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID).getRuntime(runtimeID).addJobbStatus((String)key0,(String)key1,"1","",-1);
                        // clearing job from registry
                        PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID)
                                .getRuntime(runtimeID).removeJobStatus((String) key0, (String) key1);
                        // collecting jobID/jobPID for storage cleanup 
                        ComDataBean comDataBean = new ComDataBean();
                        comDataBean.setJobID((String) key0);
                        comDataBean.setJobPID((String) key1);
                        errorJobPidList.addElement(comDataBean);
                    }
                }
            }
            if (UserQuotaUtils.getInstance().userQuotaIsFull(request.getRemoteUser())) {
                request.setAttribute("msg", "portal.RealWorkflowPortlet.quotaisoverfull");
            } else {
                new WorkflowRescueThread(portalID, userID, workflowID, runtimeID, wfStatus, errorJobPidList);
            }
        }
        // request.setAttribute("jsp",mainjsp;
        request.setAttribute("jsp", "/jsp/workflow/wrkinst.jsp");
    }
}

From source file:hu.sztaki.lpds.pgportal.portlets.workflow.RealWorkflowPortlet.java

private void doAbort(String userID, String workflowID, String runtimeID) {
    try {//from  w w w  .  j av a 2s.c om
        if (PortalCacheService.getInstance().getUser(userID) != null) {
            if (PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID) != null) {
                if (PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID)
                        .getRuntime(runtimeID) != null) {
                    String wfStatus = "" + PortalCacheService.getInstance().getUser(userID)
                            .getWorkflow(workflowID).getRuntime(runtimeID).getStatus();
                    if (("5".equals(wfStatus)) || ("23".equals(wfStatus)) || ("2".equals(wfStatus))) {
                        // PortalCacheService.getInstance().getUser(userID).getWorkflow(request.getParameter("workflow")).getRuntime(request.getParameter("rtid")).setStatus("22", 0);
                        // suspending workflow status beallitasa...
                        PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID)
                                .getRuntime(runtimeID).setStatus("28", 0);
                        ConcurrentHashMap tmp = PortalCacheService.getInstance().getUser(userID)
                                .getWorkflow(workflowID).getRuntime(runtimeID).getJobsStatus();
                        Enumeration enm0 = tmp.keys();
                        while (enm0.hasMoreElements()) {
                            Object key0 = enm0.nextElement();
                            Enumeration enm1 = ((ConcurrentHashMap) tmp.get(key0)).keys();
                            while (enm1.hasMoreElements()) {
                                Object key1 = enm1.nextElement();
                                // System.out.println("--"+key0+"/"+key1+"="+((JobStatusData)((Hashtable)tmp.get(key0)).get(key1)).getStatus());
                                String ts = "" + ((JobStatusData) ((ConcurrentHashMap) tmp.get(key0)).get(key1))
                                        .getStatus();
                                if (!(ts.equals("6") || ts.equals("7") || ts.equals("21") || ts.equals("1"))) {
                                    PortalCacheService.getInstance().getUser(userID).getWorkflow(workflowID)
                                            .getRuntime(runtimeID)
                                            .addJobbStatus((String) key0, (String) key1, "22", "", -1);
                                }
                            }
                        }
                        new WorkflowAbortThread(userID, workflowID, runtimeID);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.apache.ambari.controller.Clusters.java

private void validateClusterDefinition(String clusterName, ClusterDefinition cdef) throws Exception {
    /*/*from  www  .j  a  v  a2 s  .c  o m*/
     * Check if name is not empty or null
     */
    if (cdef.getName() == null || cdef.getName().equals("")) {
        String msg = "Cluster Name must be specified and must be non-empty string";
        throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
    }

    if (!cdef.getName().equals(clusterName)) {
        String msg = "Cluster Name specified in URL and cluster definition are not same";
        throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
    }

    if (cdef.getNodes() == null || cdef.getNodes().equals("")) {
        String msg = "Cluster node range must be specified and must be non-empty string";
        throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
    }

    if (cdef.getStackName() == null || cdef.getStackName().equals("")) {
        String msg = "Cluster stack must be specified and must be non-empty string";
        throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
    }

    if (cdef.getStackRevision() == null || cdef.getStackRevision().equals("")) {
        String msg = "Cluster stack revision must be specified";
        throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
    }

    /*
     * Check if the cluster stack and its parents exist
     * getStack would throw exception if it does not find the stack
     */
    Stack bp = stacks.getStack(cdef.getStackName(), Integer.parseInt(cdef.getStackRevision()));
    while (bp.getParentName() != null) {
        bp = stacks.getStack(bp.getParentName(), bp.getParentRevision());
    }

    /*
     * Check if nodes requested for cluster are not already allocated to other clusters
     */
    ConcurrentHashMap<String, Node> all_nodes = nodes.getNodes();
    List<String> cluster_node_range = new ArrayList<String>();
    cluster_node_range.addAll(getHostnamesFromRangeExpressions(cdef.getNodes()));
    List<String> preallocatedhosts = new ArrayList<String>();
    for (String n : cluster_node_range) {
        if (all_nodes.containsKey(n) && (all_nodes.get(n).getNodeState().getClusterName() != null
                || all_nodes.get(n).getNodeState().getAllocatedToCluster())) {
            /* 
             * Following check is for a very specific case 
             * When controller starts w/ no persistent data in data store, it adds default clusters
             * and down the road restart recovery code re-validates the cluster definition when
             * it finds nodes already allocated. 
            if (all_nodes.get(n).getNodeState().getClusterName() != null && 
            all_nodes.get(n).getNodeState().getClusterName().equals(clusterName)) { 
            continue; 
            } */
            preallocatedhosts.add(n);
        }
    }

    if (!preallocatedhosts.isEmpty()) {
        String msg = "Some of the nodes specified for the cluster roles are allocated to other cluster: ["
                + preallocatedhosts + "]";
        throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.CONFLICT)).get());
    }

    /*
     * Check if all the nodes explicitly specified in the RoleToNodesMap belong the cluster node range specified 
     */
    if (cdef.getRoleToNodesMap() != null) {
        List<String> nodes_specified_using_role_association = new ArrayList<String>();
        for (RoleToNodes e : cdef.getRoleToNodesMap()) {
            List<String> hosts = getHostnamesFromRangeExpressions(e.getNodes());
            nodes_specified_using_role_association.addAll(hosts);
            // TODO: Remove any duplicate nodes from nodes_specified_using_role_association
        }

        nodes_specified_using_role_association.removeAll(cluster_node_range);
        if (!nodes_specified_using_role_association.isEmpty()) {
            String msg = "Some nodes explicityly associated with roles using RoleToNodesMap do not belong in the "
                    + "golbal node range specified for the cluster : [" + nodes_specified_using_role_association
                    + "]";
            throw new WebApplicationException((new ExceptionResponse(msg, Response.Status.BAD_REQUEST)).get());
        }
    }

}

From source file:com.starit.diamond.client.impl.DefaultDiamondSubscriber.java

private void checkDiamondServerConfigInfo() {
    Set<String> updateDataIdGroupPairs = checkUpdateDataIds(diamondConfigure.getReceiveWaitTime());
    if (null == updateDataIdGroupPairs || updateDataIdGroupPairs.size() == 0) {
        log.debug("DataID");
        return;/*from w  w  w  .j a  v  a  2  s .c  o  m*/
    }
    // ???DataID??
    for (String freshDataIdGroupPair : updateDataIdGroupPairs) {
        int middleIndex = freshDataIdGroupPair.indexOf(WORD_SEPARATOR);
        if (middleIndex == -1)
            continue;
        String freshDataId = freshDataIdGroupPair.substring(0, middleIndex);
        String freshGroup = freshDataIdGroupPair.substring(middleIndex + 1);

        ConcurrentHashMap<String, CacheData> cacheDatas = cache.get(freshDataId);
        if (null == cacheDatas) {
            continue;
        }
        CacheData cacheData = cacheDatas.get(freshGroup);
        if (null == cacheData) {
            continue;
        }
        receiveConfigInfo(cacheData);
    }
}

From source file:com.starit.diamond.client.impl.DefaultDiamondSubscriber.java

void getConfigInfoAndPopToClient(String dataId, String group) {
    ConcurrentHashMap<String, CacheData> cacheDatas = cache.get(dataId);
    if (null == cacheDatas) {
        log.info("[pushit client] cacheDatas == null");
        return;//from  www . j a va2  s.c  o m
    }
    CacheData cacheData = cacheDatas.get(group);
    if (null == cacheData) {
        log.info("[pushit client] cacheData == null");
        return;
    }
    log.info("[pushit client] diamond-client?diamond-server?");
    receiveConfigInfo(cacheData);
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

public void addDataId(String dataId, String group) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd   HH:mm:ss");
    log.info("diamond client start:" + formatter.format(new Date(System.currentTimeMillis())));
    if (null == group) {
        group = Constants.DEFAULT_GROUP;
    }/*from   w w  w . j  a v  a2  s  .co  m*/

    ConcurrentHashMap<String, CacheData> cacheDatas = this.cache.get(dataId);
    if (null == cacheDatas) {
        ConcurrentHashMap<String, CacheData> newCacheDatas = new ConcurrentHashMap<String, CacheData>();
        ConcurrentHashMap<String, CacheData> oldCacheDatas = this.cache.putIfAbsent(dataId, newCacheDatas);
        if (null != oldCacheDatas) {
            cacheDatas = oldCacheDatas;
        } else {
            cacheDatas = newCacheDatas;
        }
    }
    CacheData cacheData = cacheDatas.get(group);
    if (null == cacheData) {
        cacheDatas.putIfAbsent(group, new CacheData(dataId, group));
        if (log.isInfoEnabled()) {
            log.info("DataID[" + dataId + "]Group" + group);
        }
        this.start();
    }
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

public void addDataId(String dataId, String group) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd   HH:mm:ss");
    log.info("diamond client start:" + formatter.format(new Date(System.currentTimeMillis())));
    if (null == group) {
        group = Constants.DEFAULT_GROUP;
    }/*from   w  w  w  .j  a v  a 2  s.  c o  m*/

    ConcurrentHashMap<String, CacheData> cacheDatas = this.cache.get(dataId);
    if (null == cacheDatas) {
        ConcurrentHashMap<String, CacheData> newCacheDatas = new ConcurrentHashMap<String, CacheData>();
        ConcurrentHashMap<String, CacheData> oldCacheDatas = this.cache.putIfAbsent(dataId, newCacheDatas);
        if (null != oldCacheDatas) {
            cacheDatas = oldCacheDatas;
        } else {
            cacheDatas = newCacheDatas;
        }
    }
    CacheData cacheData = cacheDatas.get(group);
    if (null == cacheData) {
        cacheDatas.putIfAbsent(group, new CacheData(dataId, group));
        if (log.isInfoEnabled()) {
            log.info("DataID[" + dataId + "]Group" + group);
        }
        this.start();
    }
}

From source file:com.android.exchange.ExchangeService.java

/**
 * Sent by services indicating that their thread is finished; action depends on the exitStatus
 * of the service.//from w w  w.j  a  v  a  2 s.c  o  m
 *
 * @param svc the service that is finished
 */
static public void done(AbstractSyncService svc) {
    ExchangeService exchangeService = INSTANCE;
    if (exchangeService == null)
        return;
    synchronized (sSyncLock) {
        long mailboxId = svc.mMailboxId;
        // If we're no longer the syncing thread for the mailbox, just return
        if (!exchangeService.isRunningInServiceThread(mailboxId)) {
            return;
        }
        exchangeService.releaseMailbox(mailboxId);

        ConcurrentHashMap<Long, SyncError> errorMap = exchangeService.mSyncErrorMap;
        SyncError syncError = errorMap.get(mailboxId);

        int exitStatus = svc.mExitStatus;
        Mailbox m = Mailbox.restoreMailboxWithId(exchangeService, mailboxId);
        if (m == null)
            return;

        if (exitStatus != AbstractSyncService.EXIT_LOGIN_FAILURE) {
            long accountId = m.mAccountKey;
            Account account = Account.restoreAccountWithId(exchangeService, accountId);
            if (account == null)
                return;
            if (exchangeService.releaseSyncHolds(exchangeService, AbstractSyncService.EXIT_LOGIN_FAILURE,
                    account)) {
                new AccountServiceProxy(exchangeService).notifyLoginSucceeded(accountId);
            }
        }

        switch (exitStatus) {
        case AbstractSyncService.EXIT_DONE:
            if (svc.hasPendingRequests()) {
                // TODO Handle this case
            }
            errorMap.remove(mailboxId);
            // If we've had a successful sync, clear the shutdown count
            synchronized (ExchangeService.class) {
                sClientConnectionManagerShutdownCount = 0;
            }
            break;
        // I/O errors get retried at increasing intervals
        case AbstractSyncService.EXIT_IO_ERROR:
            if (syncError != null) {
                syncError.escalate();
                log(m.mDisplayName + " held for " + syncError.holdDelay + "ms");
            } else {
                errorMap.put(mailboxId, exchangeService.new SyncError(exitStatus, false));
                log(m.mDisplayName + " added to syncErrorMap, hold for 15s");
            }
            break;
        // These errors are not retried automatically
        case AbstractSyncService.EXIT_LOGIN_FAILURE:
            new AccountServiceProxy(exchangeService).notifyLoginFailed(m.mAccountKey);
            // Fall through
        case AbstractSyncService.EXIT_SECURITY_FAILURE:
        case AbstractSyncService.EXIT_ACCESS_DENIED:
        case AbstractSyncService.EXIT_EXCEPTION:
            errorMap.put(mailboxId, exchangeService.new SyncError(exitStatus, true));
            break;
        }
        kick("sync completed");
    }
}

From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java

@Override
public void run() {
    Calendar calendar_int = Calendar.getInstance();

    //0-365//from  w  w w  . j  a va 2s .  c o m
    today_ = calendar_int.get(Calendar.DAY_OF_YEAR);

    ConcurrentHashMap<Region, ArrayList<Volume>> localVolumeTime;

    localVolumeTime = awsAccount_.getVolumeTime_Copy();

    for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
        Region region = entry.getKey();
        splitFactorDay_.put(region, 10);
        HashSet<Date> newHashSet = new HashSet<>();
        didMySnapshotDay_.put(entry.getKey(), newHashSet);
    }

    addAlreadyDoneTodaySnapshots(localVolumeTime);

    while (true) {
        try {
            //Reset my stuff
            if (isItTomorrow(today_)) {
                calendar_int = Calendar.getInstance();

                today_ = calendar_int.get(Calendar.DAY_OF_YEAR);
                resetDidMySnapshotDay();

            }

            localVolumeTime = awsAccount_.getVolumeTime_Copy();
            for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
                Region region = entry.getKey();

                if (localVolumeTime.get(region).isEmpty()) {
                    continue;
                }

                timeDay_.put(region, extractRunAt(localVolumeTime.get(region)));

            }

            for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
                Region region = entry.getKey();

                if (localVolumeTime.get(region).isEmpty()) {
                    continue;
                }

                timeDay_.get(region).keySet().removeAll(didMySnapshotDay_.get(region));
                Calendar calendar = Calendar.getInstance();
                Date now = calendar.getTime();
                now = dayFormat_.parse(dayFormat_.format(now));

                List<Date> lessThanNow = findLessThanNow(timeDay_.get(region).keySet(), now);

                if (!lessThanNow.isEmpty()) {
                    for (Date date : lessThanNow) {
                        ArrayList<Volume> volumes = timeDay_.get(region).get(date);
                        List<List<Volume>> listOfLists = Lists.partition(volumes, splitFactorDay_.get(region));

                        if (localVolumeTimeListDay_.get(region) == null
                                || localVolumeTimeListDay_.get(region).isEmpty()) {
                            localVolumeTimeListDay_.put(region, listsToArrayLists(listOfLists));
                        } else {
                            try {
                                localVolumeTimeListDay_.get(region).add(listsToArrayLists(listOfLists).get(0));
                            } catch (Exception e) {
                            }
                        }

                        ArrayList<SnapshotVolumeTime> threads = new ArrayList<>();

                        for (ArrayList<Volume> vols : listsToArrayLists(listOfLists)) {
                            threads.add(new SnapshotVolumeTime(awsAccount_.getAwsAccessKeyId(),
                                    awsAccount_.getAwsSecretKey(), awsAccount_.getUniqueAwsAccountIdentifier(),
                                    awsAccount_.getMaxApiRequestsPerSecond(),
                                    ApplicationConfiguration.getAwsCallRetryAttempts(), region, vols));

                        }

                        didMySnapshotDay_.get(region).add(date);

                        EideticSubThreads_.put(region, threads);

                    }

                }
            }
            //localVolumeTimeListDay now has hashmaps of regions with keys of arrays of arrays of volumes to take snapshots of.

            HashMap<Region, Integer> secsSlept = new HashMap<>();
            HashMap<Region, Boolean> allDead = new HashMap<>();

            for (Map.Entry<Region, ArrayList<Volume>> entry : localVolumeTime.entrySet()) {
                Region region = entry.getKey();

                if (localVolumeTimeListDay_.get(region) == null
                        || localVolumeTimeListDay_.get(region).isEmpty()) {
                    continue;
                }

                //Initializing content
                secsSlept.put(region, 0);

                //Initializing content
                allDead.put(region, false);

                Threads.threadExecutorFixedPool(EideticSubThreads_.get(region), splitFactorDay_.get(region),
                        300, TimeUnit.MINUTES);
            }

            //LETS SEE IF THEY'RE DEAD
            Boolean ejection = false;
            Boolean theyreDead;
            while (true) {
                for (Map.Entry<Region, ArrayList<SnapshotVolumeTime>> entry : EideticSubThreads_.entrySet()) {
                    Region region = entry.getKey();

                    if (areAllThreadsDead(EideticSubThreads_.get(region))) {
                        allDead.put(region, true);
                    } else {
                        secsSlept.replace(region, secsSlept.get(region), secsSlept.get(region) + 1);
                        if (secsSlept.get(region) > 1800) {
                            splitFactorDay_.replace(region, splitFactorDay_.get(region),
                                    splitFactorDay_.get(region) + 1);
                            logger.info(
                                    "Event=\"increasing_splitFactor\", Monitor=\"SnapshotVolumeTime\", splitFactor=\""
                                            + Integer.toString(splitFactorDay_.get(region))
                                            + "\", VolumeTimeSize=\""
                                            + Integer.toString(localVolumeTime.get(region).size()) + "\"");
                            ejection = true;
                            break;
                        }

                    }

                }

                //I dont like this
                theyreDead = true;
                for (Map.Entry<Region, ArrayList<SnapshotVolumeTime>> entry : EideticSubThreads_.entrySet()) {
                    Region region = entry.getKey();

                    //If any of them have false
                    if (!allDead.get(region)) {
                        theyreDead = false;
                    }
                }

                if (ejection || theyreDead) {
                    break;
                }

                Threads.sleepSeconds(1);
            }

            //See if decrease splitfactor
            for (Map.Entry<Region, ArrayList<SnapshotVolumeTime>> entry : EideticSubThreads_.entrySet()) {
                Region region = entry.getKey();

                int timeRemaining = 1800 - secsSlept.get(region);

                if ((splitFactorDay_.get(region) > 5) & (timeRemaining > 60)) {
                    splitFactorDay_.replace(region, splitFactorDay_.get(region),
                            splitFactorDay_.get(region) - 1);
                    logger.info("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                            + "\",Event=\"decreasing_splitFactor\", Monitor=\"SnapshotVolumeNoTime\", splitFactor=\""
                            + Integer.toString(splitFactorDay_.get(region)) + "\", VolumeNoTimeSize=\""
                            + Integer.toString(localVolumeTime.get(region).size()) + "\"");
                }
            }

            localVolumeTimeListDay_.clear();
            EideticSubThreads_.clear();

            Threads.sleepSeconds(30);

        } catch (Exception e) {
            logger.error("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier()
                    + "\",Error=\"MonitorSnapshotVolumeTimeFailure\", stacktrace=\"" + e.toString()
                    + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\"");
        }
    }

}

From source file:cc.osint.graphd.server.GraphCommandExecutor.java

protected String execute(Channel responseChannel, String clientId,
        ConcurrentHashMap<String, String> clientState, InboundChannelProcess inboundChannelProcess,
        String request, String cmd, String[] args) throws Exception {

    StringBuffer rsb = new StringBuffer();

    // all operations require a db to be selected (via GraphServerProtocol.CMD_USE)

    if (null == clientState.get(GraphServerHandler.ST_DB)) {
        rsb.append(GraphServerProtocol.R_ERR);
        rsb.append(" REQUIRE_USE_DB");
        return rsb.toString();
    }//from   w ww.j  a  v a 2 s  .  c om

    Graph gr = graphRef.get();
    if (null == gr) {
        GraphCommand killCmd = new GraphCommand();
        killCmd.poisonPill = true;
        graphCommandQueue.clear();
        graphCommandQueue.put(killCmd);
        return GraphServerProtocol.R_ERR + GraphServerProtocol.SPACE
                + " GRAPH_NO_LONGER_EXISTS :queue cleared & poison pill sent";
    }

    /*
     * handle dynamic <<query>> expansion, replacement & execution, e.g.
     * 
     *   [...] <<_type:v _key:m*>> [...]
     *
     * TODO: take the responseChannel direct-send hack out of this and think about
     *  how to either queue the commands (& add an "echo-batch-ok" or equiv to the tail to
     *  signify end of the batched command execution) or move this up to the thread loop?
     *
    */
    if (request.indexOf("<<") != -1 && request.indexOf(">>") != -1) {
        String query = request.substring(request.indexOf("<<") + 2, request.indexOf(">>"));
        String prefix = request.substring(0, request.indexOf("<<")).trim();
        String suffix = request.substring(request.indexOf(">>") + 2).trim();
        log.info("executing selector: [" + prefix + "] " + query + " [" + suffix + "]:");
        List<JSONObject> selectorResults = gr.queryGraphIndex(query);
        for (JSONObject selectorResult : selectorResults) {
            String batchRequestKey = selectorResult.getString(Graph.KEY_FIELD);
            String batchRequest = prefix + " " + batchRequestKey + " " + suffix;
            String batchCmd;
            String[] batchArgs;

            if (batchRequest.indexOf(GraphServerProtocol.SPACE) != -1) {
                batchCmd = batchRequest.substring(0, batchRequest.indexOf(GraphServerProtocol.SPACE)).trim()
                        .toLowerCase();
                batchArgs = batchRequest.substring(batchRequest.indexOf(GraphServerProtocol.SPACE)).trim()
                        .split(GraphServerProtocol.SPACE);
            } else {
                batchCmd = batchRequest.trim().toLowerCase();
                batchArgs = new String[0];
            }

            log.info("batchRequest: " + batchRequest);

            String batchCmdResponse = execute(responseChannel, clientId, clientState, inboundChannelProcess,
                    batchRequest, batchCmd, batchArgs);
            String responseParts[] = batchCmdResponse.split(GraphServerProtocol.NL);
            for (String responsePart : responseParts) {
                if (responsePart.charAt(0) != '-') {
                    responseChannel.write(responsePart.trim() + GraphServerProtocol.NL);
                }
            }
        }
        return GraphServerProtocol.R_BATCH_OK;
    }

    /*
     * graph commands
     *
    */

    // CREATE VERTEX: cvert <key> <json>
    if (cmd.equals(GraphServerProtocol.CMD_CVERT)) {
        String key = args[0];
        String json = request.substring(request.indexOf(GraphServerProtocol.SPACE + key) + (key.length() + 1))
                .trim(); // remainder of line
        JSONObject jo = null;
        try {
            jo = new JSONObject(json);
            gr.addVertex(key, jo);
            rsb.append(GraphServerProtocol.R_OK);
        } catch (org.json.JSONException jsonEx) {
            jsonEx.printStackTrace();
            rsb.append(GraphServerProtocol.R_ERR);
            rsb.append(" BAD_JSON ");
            rsb.append(jsonEx.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            rsb.append(GraphServerProtocol.R_ERR);
            rsb.append(GraphServerProtocol.SPACE);
            rsb.append(ex.getMessage());
        }

        // CREATE EDGE: cedge <key> <vFromKey> <vToKey> <rel> <json>
        // OR:          cedge <key> <vFromKey> <vToKey> <rel> <weight> <json>
    } else if (cmd.equals(GraphServerProtocol.CMD_CEDGE)) {
        String key = args[0];
        String vFromKey = args[1];
        String vToKey = args[2];
        String rel = args[3];
        double weight = 1.0;
        String json;

        if (args[4].charAt(0) == '{') {
            json = request.substring(request.indexOf(GraphServerProtocol.SPACE + rel) + (rel.length() + 1))
                    .trim(); // remainder of line
        } else {
            weight = Double.parseDouble(args[4]);
            json = request
                    .substring(request.indexOf(GraphServerProtocol.SPACE + args[4]) + (args[4].length() + 1))
                    .trim(); // remainder of line
        }
        JSONObject jo = null;
        try {
            jo = new JSONObject(json);
            jo.put(Graph.EDGE_SOURCE_FIELD, vFromKey);
            jo.put(Graph.EDGE_TARGET_FIELD, vToKey);
            jo.put(Graph.WEIGHT_FIELD, weight);
            jo.put(Graph.RELATION_FIELD, rel);
            gr.addEdge(key, jo, vFromKey, vToKey, rel, weight);
            rsb.append(GraphServerProtocol.R_OK);
        } catch (org.json.JSONException jsonEx) {
            jsonEx.printStackTrace();
            rsb.append(GraphServerProtocol.R_ERR);
            rsb.append(" BAD_JSON ");
            rsb.append(jsonEx.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            rsb.append(GraphServerProtocol.R_ERR);
            rsb.append(GraphServerProtocol.SPACE);
            rsb.append(ex.getMessage());
        }

        // DELETE OBJECT: del <key>
    } else if (cmd.equals(GraphServerProtocol.CMD_DEL)) {
        String key = args[0];
        JSONObject obj = gr.getGraphObject(key);
        if (null == obj) {
            rsb.append(GraphServerProtocol.R_NOT_FOUND);
        } else {
            String _type = obj.getString(Graph.TYPE_FIELD);
            if (_type.equals(Graph.VERTEX_TYPE)) {
                JSONVertex jv = gr.getVertex(key);
                gr.removeVertex(jv);
                rsb.append(GraphServerProtocol.R_OK);

            } else if (_type.equals(Graph.EDGE_TYPE)) {
                JSONEdge je = gr.getEdge(key);
                gr.removeEdge(je);
                rsb.append(GraphServerProtocol.R_OK);

            } else {
                rsb.append(GraphServerProtocol.R_ERR);
                rsb.append(GraphServerProtocol.SPACE);
                rsb.append(GraphServerProtocol.R_UNKNOWN_OBJECT_TYPE);
            }
        }

        // OBJECT EXISTS: exists <key>
    } else if (cmd.equals(GraphServerProtocol.CMD_EXISTS)) {
        String key = args[0];
        JSONObject jo = new JSONObject();
        jo.put("key", key);
        jo.put("exists", gr.exists(key));
        rsb.append(jo);
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);

        // GET OBJECT: get <key>
    } else if (cmd.equals(GraphServerProtocol.CMD_GET)) {
        String key = args[0];
        JSONObject jo = gr.getGraphObject(key);
        if (jo == null) {
            rsb.append(GraphServerProtocol.R_NOT_FOUND);
        } else {
            rsb.append(jo);
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // QUERY GRAPH OBJECTS: q <query>
    } else if (cmd.equals(GraphServerProtocol.CMD_Q)) {
        String q = request.substring(request.indexOf(GraphServerProtocol.SPACE)).trim(); // remainder of line past "q "
        List<JSONObject> results = gr.queryGraphIndex(q);
        JSONArray ja = new JSONArray();
        for (JSONObject jo : results) {
            ja.put(jo);
        }
        JSONObject res = new JSONObject();
        res.put("results", ja);

        rsb.append(res);
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);

        // DEPRECATED
        // QUERY PROCESSES: qp <query>
    } else if (cmd.equals(GraphServerProtocol.CMD_QP)) {
        String q = request.substring(request.indexOf(GraphServerProtocol.SPACE)).trim(); // remainder of line past "qp "

        /*
        List<JSONObject> results = gr.queryProcessIndex(q);
        JSONArray ja = new JSONArray();
        for(JSONObject jo: results) {
        ja.put(jo);
        }
        JSONObject res = new JSONObject();
        res.put("results", ja);
                
        rsb.append(res);
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);
        */

        // DIJKSTRA SHORTEST PATH: spath <from> <to> <radius>
    } else if (cmd.equals(GraphServerProtocol.CMD_SPATH)) {
        String vFromKey = args[0];
        String vToKey = args[1];
        double radius = Double.POSITIVE_INFINITY;
        if (args.length == 3) {
            radius = Double.parseDouble(args[2]);
        }
        JSONObject result = gr.getShortestPath(vFromKey, vToKey, radius);
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result);
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // SET VERTEX/EDGE ATTRIBUTE: set <key> <attr> <value>
    } else if (cmd.equals(GraphServerProtocol.CMD_SET)) {
        String key = args[0];
        String attr = args[1];
        String val;

        if (args.length == 2) {
            // clear value
            val = null;
        } else {
            val = request
                    .substring(request.indexOf(GraphServerProtocol.SPACE + args[1]) + (args[1].length() + 1))
                    .trim(); // remainder of line
        }

        if (attr.startsWith("_") && !attr.equals(Graph.WEIGHT_FIELD)) {
            rsb.append(GraphServerProtocol.R_ERR);
            rsb.append(" CANNOT_SET_RESERVED_PROPERTY");
        } else {
            JSONObject obj = gr.getGraphObject(key);
            if (null == obj) {
                rsb.append(GraphServerProtocol.R_NOT_FOUND);
            } else {
                String _type = obj.getString(Graph.TYPE_FIELD);
                if (_type.equals(Graph.VERTEX_TYPE)) {

                    JSONVertex jv = gr.getVertex(key);
                    if (null != val) {
                        jv.put(attr, val);
                    } else {
                        jv.remove(attr);
                    }
                    gr.indexObject(key, _type, jv);
                    rsb.append(GraphServerProtocol.R_OK);
                } else if (_type.equals(Graph.EDGE_TYPE)) {

                    JSONEdge je = gr.getEdge(key);

                    if (null != val) {
                        je.put(attr, val);
                    } else {
                        je.remove(attr);
                    }
                    if (attr.equals(Graph.WEIGHT_FIELD)) {
                        gr.setEdgeWeight(je, Double.parseDouble(val));
                    }

                    gr.indexObject(key, _type, je.asJSONObject().getJSONObject(Graph.DATA_FIELD));
                    rsb.append(GraphServerProtocol.R_OK);
                } else {
                    rsb.append(GraphServerProtocol.R_ERR);
                    rsb.append(GraphServerProtocol.R_UNKNOWN_OBJECT_TYPE);
                }
            }
        }

        // INCREASE EDGE WEIGHT: incw <edge_key> <amount>
    } else if (cmd.equals(GraphServerProtocol.CMD_INCW)) {
        String key = args[0];
        double w_amt = Double.parseDouble(args[1]);
        JSONEdge je = gr.getEdge(key);
        if (null == je) {
            rsb.append(GraphServerProtocol.R_NOT_FOUND);
        } else {
            double weight = gr.getEdgeWeight(je);
            weight += w_amt;
            gr.setEdgeWeight(je, weight);
            je.put(Graph.WEIGHT_FIELD, "" + weight);
            gr.indexObject(key, Graph.EDGE_TYPE, je.asJSONObject().getJSONObject(Graph.DATA_FIELD));
            rsb.append(GraphServerProtocol.R_OK);
        }

        // DUMP INTERNAL REPRESENTATION OF VERTEX/EDGE: spy <key>
    } else if (cmd.equals(GraphServerProtocol.CMD_SPY)) {
        String key = args[0];

        JSONObject obj = gr.getGraphObject(key);
        if (null == obj) {
            rsb.append(GraphServerProtocol.R_NOT_FOUND);
        } else {
            String _type = obj.getString(Graph.TYPE_FIELD);
            if (_type.equals(Graph.EDGE_TYPE)) {
                JSONEdge je = gr.getEdge(key);
                if (null == je) {
                    rsb.append(GraphServerProtocol.R_NOT_FOUND);
                } else {
                    rsb.append(je.asClientJSONObject().toString() + GraphServerProtocol.NL);
                    rsb.append(GraphServerProtocol.R_OK);
                }
            } else if (_type.equals(Graph.VERTEX_TYPE)) {
                JSONVertex jv = gr.getVertex(key);
                if (null == jv) {
                    rsb.append(GraphServerProtocol.R_NOT_FOUND);
                } else {
                    rsb.append(jv.toString() + GraphServerProtocol.NL);
                    rsb.append(GraphServerProtocol.R_OK);
                }
            } else {
                rsb.append(GraphServerProtocol.R_ERR);
                rsb.append(GraphServerProtocol.R_UNKNOWN_OBJECT_TYPE);
            }
        }

        // K-SHORTEST-PATHS: kspath <from> <to> <k> <optional:maxHops>
    } else if (cmd.equals(GraphServerProtocol.CMD_KSPATH)) {
        String vFromKey = args[0];
        String vToKey = args[1];
        int k = Integer.parseInt(args[2]);
        int maxHops = 0;
        if (args.length > 3) {
            maxHops = Integer.parseInt(args[3]);
        }
        JSONObject result = gr.getKShortestPaths(vFromKey, vToKey, k, maxHops);
        rsb.append(result);
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);

        // HAMILTONIAN CYCLE: hc
    } else if (cmd.equals(GraphServerProtocol.CMD_HC)) {
        List<JSONVertex> results = gr.getHamiltonianCycle();
        if (null == results) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            JSONObject res = new JSONObject();
            JSONArray cycle = new JSONArray();
            for (JSONVertex jo : results) {
                cycle.put(jo);
            }
            res.put("cycle", cycle);
            rsb.append(res);
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // EULERIAN CIRCUIT: ec
    } else if (cmd.equals(GraphServerProtocol.CMD_EC)) {
        List<JSONVertex> results = gr.getEulerianCircuit();
        if (null == results) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            JSONObject res = new JSONObject();
            JSONArray circuit = new JSONArray();
            for (JSONVertex jo : results) {
                circuit.put(jo);
            }
            res.put("circuit", circuit);
            rsb.append(res);
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // EDWARDS KARP MAXIMUM FLOW: ekmf <from> <to>
    } else if (cmd.equals(GraphServerProtocol.CMD_EKMF)) {
        String vSourceKey = args[0];
        String vSinkKey = args[1];
        JSONObject flowResult = gr.getEKMF(vSourceKey, vSinkKey);
        if (null == flowResult) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(flowResult.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // CHROMATIC NUMBER: cn
    } else if (cmd.equals(GraphServerProtocol.CMD_CN)) {
        JSONObject result = new JSONObject();
        result.put("chromatic_number", gr.getChromaticNumber());
        rsb.append(result.toString());
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);

        // KRUSKAL'S MINIMUM SPANNING TREE: kmst
    } else if (cmd.equals(GraphServerProtocol.CMD_KMST)) {
        JSONObject result = gr.getKMST();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // VERTEX COVER: GREEDY: vcg
    } else if (cmd.equals(GraphServerProtocol.CMD_VCG)) {
        JSONObject result = gr.getGreedyVertexCover();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // VERTEX COVER: 2-APPROXIMATION: vc2a
    } else if (cmd.equals(GraphServerProtocol.CMD_VC2A)) {
        JSONObject result = gr.get2ApproximationVertexCover();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // MAXIMALLY CONNECTED SET BY VERTEX: csetv <key>
    } else if (cmd.equals(GraphServerProtocol.CMD_CSETV)) {
        String key = args[0];
        JSONVertex v = gr.getVertex(key);
        if (null == v) {
            rsb.append(GraphServerProtocol.R_NOT_FOUND);
        } else {
            JSONObject result = gr.getConnectedSetByVertex(v);
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // ALL MAXIMALLY CONNECTED SETS: csets
    } else if (cmd.equals(GraphServerProtocol.CMD_CSETS)) {
        JSONObject result = gr.getConnectedSets();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // CONNECTEDNESS TEST: iscon
    } else if (cmd.equals(GraphServerProtocol.CMD_ISCON)) {
        rsb.append("" + gr.isConnected());
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);

        // UNDIRECTED PATH EXISTENCE TEST: upathex <from> <to>
    } else if (cmd.equals(GraphServerProtocol.CMD_UPATHEX)) {
        JSONVertex vFrom = gr.getVertex(args[0]);
        JSONVertex vTo = gr.getVertex(args[1]);
        if (null == vFrom || null == vTo) {
            rsb.append(GraphServerProtocol.R_NOT_FOUND);
        } else {
            rsb.append("" + gr.pathExists(vFrom, vTo));
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // FIND ALL MAXIMAL CLIQUES: Bron Kerosch Clique Finder
    } else if (cmd.equals(GraphServerProtocol.CMD_FAMC)) {
        JSONObject result = gr.getAllMaximalCliques();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // FIND BIGGEST MAXIMAL CLIQUES: Bron Kerosch Clique Finder
    } else if (cmd.equals(GraphServerProtocol.CMD_FBMC)) {
        JSONObject result = gr.getBiggestMaximalCliques();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

    } else if (cmd.equals(GraphServerProtocol.CMD_ASPV)) {
        JSONObject result = gr.getAllShortestPathsFrom(gr.getVertex(args[0]));
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

    } else if (cmd.equals(GraphServerProtocol.CMD_GCYC)) {
        JSONObject result = gr.getGraphCycles();
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

    } else if (cmd.equals(GraphServerProtocol.CMD_VCYC)) {
        JSONObject result = gr.getGraphCyclesContainingVertex(gr.getVertex(args[0]));
        if (null == result) {
            rsb.append(GraphServerProtocol.R_NOT_EXIST);
        } else {
            rsb.append(result.toString());
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        /*
         * TRAVERSAL
        */

        // custom traversal code
        // TRAV <start_vertex> <traversal_type> ( - | <udf_key> ) [channel]
        //
        // <traversal_type> values:
        //  breadth_first depth_first closest_first topological
        //
        // the following <traversal_type> may be specified with a suffix
        //  denoting a maximum radius from <start_vertex>:
        //  closest_first[:radius]
        //  
        // if parameter 3 is "-", events will be sent to argument 4 [channel]
        // if parameter 3 is not "-", it will be interpreted as a server-side udf
        //   that implements TraversalEvent functions
        // 
        // NOTE: topological <traversal_type> ignores the value of <start_vertex>
        //       but some value must be specified
        //
    } else if (cmd.equals(GraphServerProtocol.CMD_TRAV)) {
        String startVertex = args[0];
        String traversalType = args[1];
        String udfKey = args[2];
        String channel = null;
        JSONArray pipelineEventPassList = null;
        boolean isPipelined = false;
        if (udfKey.equals("-")) {
            isPipelined = true;
            channel = args[3];
            if (args.length == 5) {
                String peplStr = request.substring(
                        request.lastIndexOf(GraphServerProtocol.SPACE + channel) + (channel.length() + 1))
                        .trim(); // remainder of line
                pipelineEventPassList = new JSONArray(peplStr);
            }
        }
        double radius = 0.0;
        if (traversalType.indexOf(":") != -1) {
            radius = Double.parseDouble(traversalType.substring(traversalType.indexOf(":") + 1));
            traversalType = traversalType.substring(0, traversalType.indexOf(":"));
        }

        JSONVertex v = gr.getVertex(startVertex);
        if (null == v) {
            rsb.append(GraphServerProtocol.R_ERR);
            rsb.append(GraphServerProtocol.SPACE);
            rsb.append("startVertex does not exist");
        } else {
            if (isPipelined) {
                gr.pipelinedTraversal(traversalType, v, channel, radius, pipelineEventPassList);
                rsb.append(GraphServerProtocol.R_OK);
            } else {
                gr.scriptedTraversal(traversalType, v, udfKey, radius);
                rsb.append(GraphServerProtocol.R_OK);
            }
        }

        /*
         * USER-DEFINED FUNCTIONS
        */

        // DEFINE A UDF
    } else if (cmd.equals(GraphServerProtocol.CMD_DEFINE_UDF)) {
        String udfKey = args[0];
        String udfType = args[1];
        String udfURL = args[2];
        gr.defineUDF(udfKey, udfType, udfURL);
        rsb.append(GraphServerProtocol.R_OK);

        /*
         * ANONYMOUS BLOCKS
        */

        // block_begin <block_key>
    } else if (cmd.equals(GraphServerProtocol.CMD_BLOCK_BEGIN)) {
        throw new Exception("not implemented");

        // block_end <block_key>
    } else if (cmd.equals(GraphServerProtocol.CMD_BLOCK_END)) {
        throw new Exception("not implemented");

        // block_get <block_key>
    } else if (cmd.equals(GraphServerProtocol.CMD_BLOCK_GET)) {
        throw new Exception("not implemented");

        // block_remove <block_key>
    } else if (cmd.equals(GraphServerProtocol.CMD_BLOCK_REMOVE)) {
        throw new Exception("not implemented");

        /*
         * SIMULATION
        */

        // query simulation index: qsim <query> e.g., _type:udf udf_name:test*
    } else if (cmd.equals(GraphServerProtocol.CMD_QSIM)) {
        String q = request.substring(request.indexOf(GraphServerProtocol.SPACE)).trim(); // remainder of line past "q "
        List<JSONObject> results = gr.querySimIndex(q);
        JSONArray ja = new JSONArray();
        for (JSONObject jo : results) {
            ja.put(jo);
        }
        JSONObject res = new JSONObject();
        res.put("results", ja);
        rsb.append(res);
        rsb.append(GraphServerProtocol.NL);
        rsb.append(GraphServerProtocol.R_OK);

        // START A UDF-BACKED PROCESS
    } else if (cmd.equals(GraphServerProtocol.CMD_SPROC)) {
        String objKey = args[0];
        String udfKey = args[1];
        String processName;
        if (args.length == 3) {
            processName = args[2];
        } else {
            processName = objKey + "-" + udfKey;
        }
        gr.startProcess(objKey, udfKey, processName);
        rsb.append(GraphServerProtocol.R_OK);

        // EMIT A MESSAGE TO A RUNNING SIMULATION PROCESS: emit <key> <process_name> <json_msg>
    } else if (cmd.equals(GraphServerProtocol.CMD_EMIT)) {
        String key = args[0];
        String processName = args[1];
        String json = request
                .substring(
                        request.indexOf(GraphServerProtocol.SPACE + processName) + (processName.length() + 1))
                .trim(); // remainder of line
        JSONObject jo = null;
        jo = new JSONObject(json);
        gr.emit(key, processName, jo);
        rsb.append(GraphServerProtocol.R_OK);

        /*
         * CHANNEL MESSAGING
        */

        // create a channel: cchan <channel_name>
    } else if (cmd.equals(GraphServerProtocol.CMD_CCHAN)) {
        String channelName = args[0];
        String pid = gr.createEndpointChannel(channelName);
        if (null == pid) {
            rsb.append(GraphServerProtocol.R_ALREADY_EXIST);
        } else {
            rsb.append(pid);
            rsb.append(GraphServerProtocol.NL);
            rsb.append(GraphServerProtocol.R_OK);
        }

        // publish a message to a channel: publish <channel_name> <json_msg>
    } else if (cmd.equals(GraphServerProtocol.CMD_PUBLISH)) {
        String channelName = args[0];
        String json = request
                .substring(
                        request.indexOf(GraphServerProtocol.SPACE + channelName) + (channelName.length() + 1))
                .trim(); // remainder of line
        gr.publishToEndpointByName(channelName, new JSONObject(json));
        rsb.append(GraphServerProtocol.R_OK);

        // subscribe to a channel: subscribe <channel_name>
    } else if (cmd.equals(GraphServerProtocol.CMD_SUBSCRIBE)) {
        String channelName = args[0];
        /*
        log.info("channelName = " + channelName);
        log.info("inboundChannelProcess = " + inboundChannelProcess);
        log.info("inboundChannelProcess = " + 
        inboundChannelProcess);
        log.info("inboundChannelProcess.getChannel() = " + 
        inboundChannelProcess.getChannel());
        */
        gr.subscribeToEndpointByName(channelName, inboundChannelProcess.getChannel());
        rsb.append(GraphServerProtocol.R_OK);

        // unsubscribe from a channel: unsubscribe <channel_name>
    } else if (cmd.equals(GraphServerProtocol.CMD_UNSUBSCRIBE)) {
        String channelName = args[0];
        gr.unsubscribeToEndpointByName(channelName, inboundChannelProcess.getChannel());
        rsb.append(GraphServerProtocol.R_OK);
    } else {
        log.info("GraphServerProtocol.R_UNK: " + cmd);
        rsb.append(GraphServerProtocol.R_UNK);
        rsb.append(GraphServerProtocol.SPACE);
        rsb.append(cmd);
    }

    return rsb.toString();
}