use of edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket in project GNS by MobilityFirst.
the class AdminListener method handlePacket.
/**
* Handle an incoming admin packet.
*
* @param incomingJSON
* @param incomingSocket
* @param handler
*/
public void handlePacket(JSONObject incomingJSON, Socket incomingSocket, ClientRequestHandlerInterface handler) {
try {
switch(Packet.getPacketType(incomingJSON)) {
case DUMP_REQUEST:
DumpRequestPacket<String> dumpRequestPacket = new DumpRequestPacket<>(incomingJSON, handler.getGnsNodeConfig());
if (dumpRequestPacket.getPrimaryNameServer() == null) {
// OUTGOING - multicast it to all the nameservers
int id = dumpRequestPacket.getId();
ClientCommandProcessorConfig.getLogger().info("ListenerAdmin: Dump request from local server");
JSONObject json = dumpRequestPacket.toJSONObject();
Set<String> serverIds = handler.getNodeConfig().getActiveReplicas();
//Set<NodeIDType> serverIds = handler.getGnsNodeConfig().getNodeIDs();
replicationMap.put(id, serverIds.size());
Packet.multicastTCP(handler.getGnsNodeConfig(), serverIds, json, 2, PortOffsets.SERVER_ADMIN_PORT, null);
ClientCommandProcessorConfig.getLogger().log(Level.INFO, "ListenerAdmin: Multicast out to {0} hosts for {1} --> {2}", new Object[] { serverIds.size(), id, dumpRequestPacket.toString() });
} else {
// INCOMING - send it out to original requester
DumpRequestPacket<String> incomingPacket = new DumpRequestPacket<>(incomingJSON, handler.getGnsNodeConfig());
int incomingId = incomingPacket.getId();
handler.getAdmintercessor().handleIncomingDumpResponsePackets(incomingJSON, handler);
ClientCommandProcessorConfig.getLogger().log(Level.FINEST, "ListenerAdmin: Relayed response for {0} --> {1}", new Object[] { incomingId, dumpRequestPacket.toJSONObject() });
int remaining = replicationMap.get(incomingId);
remaining -= 1;
if (remaining > 0) {
replicationMap.put(incomingId, remaining);
} else {
ClientCommandProcessorConfig.getLogger().log(Level.INFO, "ListenerAdmin: Saw last response for {0}", incomingId);
replicationMap.remove(incomingId);
SentinalPacket sentinelPacket = new SentinalPacket(incomingId);
handler.getAdmintercessor().handleIncomingDumpResponsePackets(sentinelPacket.toJSONObject(), handler);
}
}
break;
case ADMIN_REQUEST:
AdminRequestPacket incomingPacket = new AdminRequestPacket(incomingJSON);
switch(incomingPacket.getOperation()) {
// deliberately nothing here for now
default:
ClientCommandProcessorConfig.getLogger().log(Level.SEVERE, "Unknown admin request in packet: {0}", incomingJSON);
break;
}
break;
case ADMIN_RESPONSE:
// forward any admin response packets recieved from NSs back to client
AdminResponsePacket responsePacket = new AdminResponsePacket(incomingJSON);
handler.getAdmintercessor().handleIncomingAdminResponsePackets(responsePacket.toJSONObject());
break;
default:
ClientCommandProcessorConfig.getLogger().log(Level.SEVERE, "Unknown packet type in packet: {0}", incomingJSON);
break;
}
} catch (JSONException | IllegalArgumentException | SecurityException | ParseException e) {
ClientCommandProcessorConfig.getLogger().log(Level.WARNING, "Ignoring error handling packets: {0}", e);
e.printStackTrace();
}
}
use of edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket in project GNS by MobilityFirst.
the class Admintercessor method sendDumpOutputHelper.
private int sendDumpOutputHelper(String tagName, ClientRequestHandlerInterface handler) {
// send the request out to the local name server
int id = nextDumpRequestID();
ClientCommandProcessorConfig.getLogger().log(Level.INFO, "Sending dump request id = {0}", id);
try {
handOffAdminPacket(new DumpRequestPacket<String>(id, new InetSocketAddress(handler.getNodeAddress().getAddress(), handler.getGnsNodeConfig().getCollatingAdminPort(handler.getActiveReplicaID())), tagName).toJSONObject(), handler);
} catch (JSONException e) {
ClientCommandProcessorConfig.getLogger().log(Level.WARNING, "Ignoring error sending DUMP request for id {0} : {1}", new Object[] { id, e.getMessage() });
return -1;
} catch (IOException e) {
return -1;
}
return id;
}
use of edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket in project GNS by MobilityFirst.
the class Admintercessor method handleIncomingDumpResponsePackets.
/**
* Processes incoming Dump packets.
*
* @param json
* @param handler
*/
public void handleIncomingDumpResponsePackets(JSONObject json, ClientRequestHandlerInterface handler) {
try {
switch(Packet.getPacketType(json)) {
case DUMP_REQUEST:
try {
DumpRequestPacket<String> dumpResponse = new DumpRequestPacket<>(json, handler.getGnsNodeConfig());
int id = dumpResponse.getId();
// grab or make a new recordsMap
Map<String, TreeSet<NameRecord>> recordsMap = dumpStorage.get(id);
if (recordsMap == null) {
recordsMap = new TreeMap<>();
dumpStorage.put(id, recordsMap);
}
// pull the records out of the dump response and put them in dumpStorage
JSONArray jsonArray = dumpResponse.getJsonArray();
String serverID = dumpResponse.getPrimaryNameServer();
TreeSet<NameRecord> records = new TreeSet<>();
for (int i = 0; i < jsonArray.length(); i++) {
records.add(new NameRecord(null, jsonArray.getJSONObject(i)));
}
recordsMap.put(serverID, records);
} catch (JSONException e) {
ClientCommandProcessorConfig.getLogger().log(Level.WARNING, "JSON error during dump reponse processing: {0}", e);
}
break;
case SENTINAL:
// put the records in dumpResult and let the folks waiting know they are ready
try {
SentinalPacket sentinal = new SentinalPacket(json);
int id = sentinal.getId();
ClientCommandProcessorConfig.getLogger().log(Level.FINER, "Processing sentinel for {0}", id);
synchronized (dumpMonitor) {
dumpResult.put(id, dumpStorage.get(id));
dumpStorage.remove(id);
dumpMonitor.notifyAll();
}
} catch (JSONException e) {
ClientCommandProcessorConfig.getLogger().log(Level.WARNING, "JSON error during dump sentinel processing: {0}", e);
}
break;
}
} catch (JSONException e) {
ClientCommandProcessorConfig.getLogger().log(Level.WARNING, "JSON error while getting packet type: {0}", e);
}
}
use of edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket 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();
}
}
}
Aggregations