Search in sources :

Example 1 with AbstractRecordCursor

use of edu.umass.cs.gnsserver.database.AbstractRecordCursor in project GNS by MobilityFirst.

the class Select method getJSONRecordsForSelect.

private static JSONArray getJSONRecordsForSelect(SelectRequestPacket request, GNSApplicationInterface<String> ar) throws FailedDBOperationException {
    JSONArray jsonRecords = new JSONArray();
    // actually only need name and values map... fix this
    AbstractRecordCursor cursor = null;
    switch(request.getSelectOperation()) {
        case EQUALS:
            cursor = NameRecord.selectRecords(ar.getDB(), request.getKey(), request.getValue());
            break;
        case NEAR:
            if (request.getValue() instanceof String) {
                cursor = NameRecord.selectRecordsNear(ar.getDB(), request.getKey(), (String) request.getValue(), Double.parseDouble((String) request.getOtherValue()));
            } else {
                break;
            }
            break;
        case WITHIN:
            if (request.getValue() instanceof String) {
                cursor = NameRecord.selectRecordsWithin(ar.getDB(), request.getKey(), (String) request.getValue());
            } else {
                break;
            }
            break;
        case QUERY:
            LOGGER.log(Level.FINE, "NS{0} query: {1} {2}", new Object[] { ar.getNodeID(), request.getQuery(), request.getProjection() });
            cursor = NameRecord.selectRecordsQuery(ar.getDB(), request.getQuery(), request.getProjection());
            break;
        default:
            break;
    }
    // number of records in it and the ability to fetch more
    while (cursor != null && cursor.hasNext()) {
        JSONObject record = cursor.nextJSONObject();
        LOGGER.log(Level.FINE, "NS{0} record returned: {1}", new Object[] { ar.getNodeID(), record });
        jsonRecords.put(record);
    }
    return jsonRecords;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AbstractRecordCursor(edu.umass.cs.gnsserver.database.AbstractRecordCursor)

Example 2 with AbstractRecordCursor

use of edu.umass.cs.gnsserver.database.AbstractRecordCursor in project GNS by MobilityFirst.

the class AppAdminServer method run.

/**
   * Start executing the thread.
   */
@Override
public void run() {
    int numRequest = 0;
    GNSConfig.getLogger().log(Level.INFO, "NS Node {0} starting Admin Request Server on port {1}", new Object[] { app.getNodeID(), serverSocket.getLocalPort() });
    while (true) {
        try {
            //Read the packet from the input stream
            try (Socket socket = serverSocket.accept()) {
                //Read the packet from the input stream
                JSONObject incomingJSON = Packet.getJSONObjectFrame(socket);
                switch(Packet.getPacketType(incomingJSON)) {
                    case DUMP_REQUEST:
                        DumpRequestPacket<String> dumpRequestPacket = new DumpRequestPacket<>(incomingJSON, gnsNodeConfig);
                        dumpRequestPacket.setPrimaryNameServer(app.getNodeID());
                        JSONArray jsonArray = new JSONArray();
                        // if there is an argument it is a TAGNAME we return all the records that have that tag
                        if (dumpRequestPacket.getArgument() != null) {
                            String tag = dumpRequestPacket.getArgument();
                            AbstractRecordCursor cursor = NameRecord.getAllRowsIterator(app.getDB());
                            while (cursor.hasNext()) {
                                NameRecord nameRecord = null;
                                JSONObject json = cursor.nextJSONObject();
                                try {
                                    nameRecord = new NameRecord(app.getDB(), json);
                                } catch (JSONException e) {
                                    GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing json into NameRecord: {0} JSON is {1}", new Object[] { e, json.toString() });
                                }
                                if (nameRecord != null) {
                                    try {
                                        if (nameRecord.containsUserKey(AccountAccess.GUID_INFO)) {
                                            GuidInfo userInfo = new GuidInfo(nameRecord.getValuesMap().getJSONObject(AccountAccess.GUID_INFO));
                                            //GuidInfo userInfo = new GuidInfo(nameRecord.getUserKeyAsArray(AccountAccess.GUID_INFO).toResultValueString());
                                            if (userInfo.containsTag(tag)) {
                                                jsonArray.put(nameRecord.toJSONObject());
                                            }
                                        }
                                    } catch (FieldNotFoundException e) {
                                        GNSConfig.getLogger().log(Level.SEVERE, "FieldNotFoundException. Field Name =  {0}", e.getMessage());
                                        //To change body of catch statement use File | Settings | File Templates.
                                        e.printStackTrace();
                                    }
                                }
                            }
                        // OTHERWISE WE RETURN ALL THE RECORD
                        } else {
                            //for (NameRecord nameRecord : NameServer.getAllNameRecords()) {
                            AbstractRecordCursor cursor = NameRecord.getAllRowsIterator(app.getDB());
                            while (cursor.hasNext()) {
                                NameRecord nameRecord = null;
                                JSONObject json = cursor.nextJSONObject();
                                try {
                                    nameRecord = new NameRecord(app.getDB(), json);
                                } catch (JSONException e) {
                                    GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing record cursor into NameRecord: {0} JSON is {1}", new Object[] { e, json.toString() });
                                }
                                if (nameRecord != null) {
                                    jsonArray.put(nameRecord.toJSONObject());
                                }
                            }
                        }
                        GNSConfig.getLogger().log(Level.FINER, "AppAdmin for {0} is {1}", new Object[] { app.getNodeID(), jsonArray.toString() });
                        dumpRequestPacket.setJsonArray(jsonArray);
                        Packet.sendTCPPacket(dumpRequestPacket.toJSONObject(), dumpRequestPacket.getReturnAddress());
                        GNSConfig.getLogger().log(Level.FINEST, "AppAdmin: Response to id:{0} --> {1}", new Object[] { dumpRequestPacket.getId(), dumpRequestPacket.toString() });
                        break;
                    case ADMIN_REQUEST:
                        AdminRequestPacket adminRequestPacket = new AdminRequestPacket(incomingJSON);
                        switch(adminRequestPacket.getOperation()) {
                            case CLEARCACHE:
                                GNSConfig.getLogger().log(Level.WARNING, "NSListenerAdmin ({0}) : Ignoring CLEARCACHE request", app.getNodeID());
                                break;
                            case DUMPCACHE:
                                GNSConfig.getLogger().log(Level.WARNING, "NSListenerAdmin ({0}) : Ignoring DUMPCACHE request", app.getNodeID());
                                break;
                        }
                        break;
                }
            }
        } catch (IOException | JSONException | FailedDBOperationException | ParseException | IllegalArgumentException | SecurityException e) {
            if (serverSocket.isClosed()) {
                GNSConfig.getLogger().warning("NS Admin shutting down.");
                // close this thread
                return;
            }
            e.printStackTrace();
        }
    }
}
Also used : JSONArray(org.json.JSONArray) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) AdminRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.admin.AdminRequestPacket) JSONException(org.json.JSONException) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) IOException(java.io.IOException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DumpRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractRecordCursor(edu.umass.cs.gnsserver.database.AbstractRecordCursor) ParseException(java.text.ParseException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Aggregations

AbstractRecordCursor (edu.umass.cs.gnsserver.database.AbstractRecordCursor)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)1 GuidInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo)1 AdminRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.AdminRequestPacket)1 DumpRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket)1 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)1 IOException (java.io.IOException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 ParseException (java.text.ParseException)1 JSONException (org.json.JSONException)1