Commit 81422908 authored by Ryan Berkheimer's avatar Ryan Berkheimer

finished factoring Endpoint methods into EndpointUtils

parent 56f803f6
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
/** /**
Constructor for the Endpoint object. Constructor for the Endpoint object.
*/ */
EndpointUtils::EndpointUtils(JNIEnv *jvm, ListUtils *listUtils) EndpointUtils::EndpointUtils(JNIEnv *jvm, jobject endpoint, TypeUtils *typeUtils, ListUtils *listUtils)
{ {
this->loadGlobalRefs(jvm, listUtils); this->loadGlobalRefs(jvm, endpoint, typeUtils, listUtils);
this->loadMethodIds(); this->loadMethodIds();
} }
...@@ -22,12 +22,104 @@ EndpointUtils::~EndpointUtils() ...@@ -22,12 +22,104 @@ EndpointUtils::~EndpointUtils()
} }
} }
void EndpointUtils::loadGlobalRefs(JNIEnv *jvm, ListUtils *listUtils) /*Public API*/
struct record *EndpointUtils::getStateContainer()
{
jobject jstatecontainer = this->jvm->CallObjectMethod(this->endpoint, this->getStateContainerMethodId);
struct record *record = (struct record *)malloc(sizeof(struct record) + sizeof(jstatecontainer));
record->jrecord = jstatecontainer;
return record;
}
struct field_list *EndpointUtils::getDefaultFields()
{
jobject jFieldList = this->jvm->CallObjectMethod(this->endpoint, this->getDefaultFieldsMethodId);
int fieldCount = this->listUtils->getListLength(jFieldList);
struct field **fields = (struct field **)malloc(sizeof(struct field *) * fieldCount);
for (int i = 0; i < fieldCount; i++)
{
jobject jfield = static_cast<jobject>(this->jvm->CallObjectMethod(jFieldList, this->listUtils->getListItemMethod(), i));
struct field *field = (struct field *)malloc(sizeof(field) + sizeof(jfield));
field->jfield = (jobject)jfield;
fields[i] = field;
}
struct field_list *field_list = (struct field_list *)malloc(sizeof(struct field_list) + sizeof(fields));
field_list->count = fieldCount;
field_list->fields = fields;
return field_list;
}
struct packet *EndpointUtils::createPacket()
{
jobject jpacket = this->jvm->CallObjectMethod(this->endpoint, this->createPacketMethodId);
struct packet *packet = (struct packet *)malloc(sizeof(struct packet) + sizeof(jpacket));
packet->jpacket = jpacket;
return packet;
}
struct record *EndpointUtils::createRecord()
{
jobject jrecord = this->jvm->CallObjectMethod(this->endpoint, this->createRecordMethodId);
struct record *record = (struct record *)malloc(sizeof(struct record) + sizeof(jrecord));
record->jrecord = jrecord;
return record;
}
struct rejection *EndpointUtils::createRejection(struct record *record, const char *reason)
{
jstring jreason = this->typeUtils->toJavaString(reason);
jobject jrejection = this->jvm->CallObjectMethod(this->endpoint, this->createRejectionMethodId, record->jrecord, jreason);
struct rejection *rejection = (struct rejection *)malloc(sizeof(struct rejection) + sizeof(jrejection));
rejection->jrejection = jrejection;
this->jvm->DeleteLocalRef(jreason);
return rejection;
}
/*Private Methods*/
void EndpointUtils::loadGlobalRefs(JNIEnv *jvm, jobject endpoint, TypeUtils *typeUtils, ListUtils *listUtils)
{ {
this->jvm = jvm; this->jvm = jvm;
this->endpoint = endpoint;
this->typeUtils = typeUtils;
this->listUtils = listUtils; this->listUtils = listUtils;
} }
void EndpointUtils::loadMethodIds() void EndpointUtils::loadMethodIds()
{ {
jclass endpointClass = JniUtils::getObjectClass(this->jvm, this->endpoint);
this->getStateContainerMethodId = JniUtils::getMethod(this->jvm, endpointClass, "getStateContainer", this->getMethodSignature("getStateContainer"), false);
this->getDefaultFieldsMethodId = JniUtils::getMethod(this->jvm, endpointClass, "getDefaultFields", this->getMethodSignature("getDefaultFields"), false);
this->createPacketMethodId = JniUtils::getMethod(this->jvm, endpointClass, "createPacket", this->getMethodSignature("createPacket"), false);
this->createRecordMethodId = JniUtils::getMethod(this->jvm, endpointClass, "createRecord", this->getMethodSignature("createRecord"), false);
this->createRejectionMethodId = JniUtils::getMethod(this->jvm, endpointClass, "createRejection", this->getMethodSignature("createRejection"), false);
this->jvm->DeleteLocalRef(endpointClass);
}
const char *EndpointUtils::getMethodSignature(const char *methodName)
{
if (strcmp(methodName, "getStateContainer") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IRecord;";
}
else if (strcmp(methodName, "getDefaultFields") == 0)
{
return "()Ljava/util/List;";
}
else if (strcmp(methodName, "createPacket") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IPacket;";
}
else if (strcmp(methodName, "createRecord") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IRecord;";
}
else if (strcmp(methodName, "createRejection") == 0)
{
return "(Lgov/noaa/messageapi/interfaces/IRecord;Ljava/lang/String;)Lgov/noaa/messageapi/interfaces/IRejection;";
}
return NULL;
} }
\ No newline at end of file
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "JniUtils.h" #include "JniUtils.h"
#include "ListUtils.h" #include "ListUtils.h"
#include "TypeUtils.h"
/** /**
* This is the header for the EndpointUtils class. * This is the header for the EndpointUtils class.
...@@ -19,6 +20,9 @@ ...@@ -19,6 +20,9 @@
* consume records, evaluate containers, send and/or retrieve data, and create and return * consume records, evaluate containers, send and/or retrieve data, and create and return
* packets containing a return record/rejection set. This set of utils contains all API methods * packets containing a return record/rejection set. This set of utils contains all API methods
* exposed to the user that can manipulate system properties directly managed by endpoints. * exposed to the user that can manipulate system properties directly managed by endpoints.
* Native endpoint connections also have access to a special construct called a state container.
* This state container is a regular record that is persistent through multiple endpoint connection
* calls.
* *
* @author Ryan Berkheimer * @author Ryan Berkheimer
*/ */
...@@ -27,19 +31,34 @@ class EndpointUtils ...@@ -27,19 +31,34 @@ class EndpointUtils
public: public:
/*Default constructor/destructors*/ /*Default constructor/destructors*/
EndpointUtils(JNIEnv *javaEnv, ListUtils *listUtils); EndpointUtils(JNIEnv *javaEnv, jobject endpoint, TypeUtils *typeUtils, ListUtils *listUtils);
~EndpointUtils(); ~EndpointUtils();
/*Endpoint Methods*/
struct record *getStateContainer();
struct field_list *getDefaultFields();
struct packet *createPacket();
struct record *createRecord();
struct rejection *createRejection(struct record *record, const char *reason);
private: private:
/*Vars*/ /*Vars*/
JNIEnv *jvm; JNIEnv *jvm;
jobject endpoint;
TypeUtils *typeUtils;
ListUtils *listUtils; ListUtils *listUtils;
/*Endpoint Methods*/
jmethodID getStateContainerMethodId;
jmethodID getDefaultFieldsMethodId;
jmethodID createPacketMethodId;
jmethodID createRecordMethodId;
jmethodID createRejectionMethodId;
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/ /*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadMethodIds(); void loadMethodIds();
void loadGlobalRefs(JNIEnv *env, ListUtils *listUtils); void loadGlobalRefs(JNIEnv *env, jobject endpoint, TypeUtils *typeUtils, ListUtils *listUtils);
/*Grouped methods for returning the matching method signature string for a given interface*/
const char *getMethodSignature(const char *methodName); const char *getMethodSignature(const char *methodName);
}; };
......
...@@ -19,9 +19,9 @@ MessageApiEndpoint::MessageApiEndpoint(JNIEnv *env, jobject jendpoint, jobject j ...@@ -19,9 +19,9 @@ MessageApiEndpoint::MessageApiEndpoint(JNIEnv *env, jobject jendpoint, jobject j
this->typeUtils = new TypeUtils(this->jvm); this->typeUtils = new TypeUtils(this->jvm);
this->listUtils = new ListUtils(this->jvm, typeUtils); this->listUtils = new ListUtils(this->jvm, typeUtils);
this->endpointUtils = new EndpointUtils(this->jvm, this->endpoint, this->listUtils);
this->packetUtils = new PacketUtils(this->jvm, this->listUtils); this->packetUtils = new PacketUtils(this->jvm, this->listUtils);
this->loadEndpointMethodIds();
this->loadProtocolRecordMethodIds(); this->loadProtocolRecordMethodIds();
this->loadRecordMethodIds(); this->loadRecordMethodIds();
this->loadRejectionMethodIds(); this->loadRejectionMethodIds();
...@@ -33,6 +33,7 @@ MessageApiEndpoint::~MessageApiEndpoint() ...@@ -33,6 +33,7 @@ MessageApiEndpoint::~MessageApiEndpoint()
{ {
try try
{ {
delete this->endpointUtils;
delete this->packetUtils; delete this->packetUtils;
delete this->listUtils; delete this->listUtils;
delete this->typeUtils; delete this->typeUtils;
...@@ -45,6 +46,11 @@ MessageApiEndpoint::~MessageApiEndpoint() ...@@ -45,6 +46,11 @@ MessageApiEndpoint::~MessageApiEndpoint()
} }
} }
EndpointUtils *MessageApiEndpoint::getEndpointUtils()
{
return this->endpointUtils;
}
PacketUtils *MessageApiEndpoint::getPacketUtils() PacketUtils *MessageApiEndpoint::getPacketUtils()
{ {
return this->packetUtils; return this->packetUtils;
...@@ -60,17 +66,6 @@ TypeUtils *MessageApiEndpoint::getTypeUtils() ...@@ -60,17 +66,6 @@ TypeUtils *MessageApiEndpoint::getTypeUtils()
return this->typeUtils; return this->typeUtils;
} }
void MessageApiEndpoint::loadEndpointMethodIds()
{
jclass endpointClass = JniUtils::getObjectClass(this->jvm, this->endpoint);
this->getStateContainerMethodId = JniUtils::getMethod(this->jvm, endpointClass, "getStateContainer", this->getEndpointMethodSignature("getStateContainer"), false);
this->getDefaultFieldsMethodId = JniUtils::getMethod(this->jvm, endpointClass, "getDefaultFields", this->getEndpointMethodSignature("getDefaultFields"), false);
this->createPacketMethodId = JniUtils::getMethod(this->jvm, endpointClass, "createPacket", this->getEndpointMethodSignature("createPacket"), false);
this->createRecordMethodId = JniUtils::getMethod(this->jvm, endpointClass, "createRecord", this->getEndpointMethodSignature("createRecord"), false);
this->createRejectionMethodId = JniUtils::getMethod(this->jvm, endpointClass, "createRejection", this->getEndpointMethodSignature("createRejection"), false);
this->jvm->DeleteLocalRef(endpointClass);
}
void MessageApiEndpoint::loadProtocolRecordMethodIds() void MessageApiEndpoint::loadProtocolRecordMethodIds()
{ {
...@@ -136,40 +131,6 @@ void MessageApiEndpoint::loadConditionMethodIds() ...@@ -136,40 +131,6 @@ void MessageApiEndpoint::loadConditionMethodIds()
this->jvm->DeleteLocalRef(conditionClass); this->jvm->DeleteLocalRef(conditionClass);
} }
/**
* Loads the default value types for MessageApiEndpoint.
* Default types are relatively primitive data types. More complicated 'struct' types
* will require extension of this by user methods.
* The included default types have associated access,modification,and release methods.
* Each also has an associated 'list' method (i.e., string has string_list, byte has byte_list, etc.)
* Default types include the following:
* integer, long, float, double, string, byte, boolean
*/
const char *MessageApiEndpoint::getEndpointMethodSignature(const char *methodName)
{
if (strcmp(methodName, "getStateContainer") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IRecord;";
}
else if (strcmp(methodName,"getDefaultFields") == 0)
{
return "()Ljava/util/List;";
}
else if (strcmp(methodName, "createPacket") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IPacket;";
}
else if (strcmp(methodName, "createRecord") == 0)
{
return "()Lgov/noaa/messageapi/interfaces/IRecord;";
}
else if (strcmp(methodName, "createRejection") == 0)
{
return "(Lgov/noaa/messageapi/interfaces/IRecord;Ljava/lang/String;)Lgov/noaa/messageapi/interfaces/IRejection;";
}
return NULL;
}
const char *MessageApiEndpoint::getProtocolRecordMethodSignature(const char* methodName) const char *MessageApiEndpoint::getProtocolRecordMethodSignature(const char* methodName)
{ {
...@@ -318,60 +279,6 @@ const char * MessageApiEndpoint::getConditionMethodSignature(const char *methodN ...@@ -318,60 +279,6 @@ const char * MessageApiEndpoint::getConditionMethodSignature(const char *methodN
return NULL; return NULL;
} }
struct record *MessageApiEndpoint::getStateContainer()
{
jobject jstatecontainer = this->jvm->CallObjectMethod(this->endpoint, this->getStateContainerMethodId);
struct record *record = (struct record *)malloc(sizeof(struct record) + sizeof(jstatecontainer));
record->jrecord = jstatecontainer;
return record;
}
struct field_list *MessageApiEndpoint::getDefaultFields()
{
jobject jFieldList = this->jvm->CallObjectMethod(this->endpoint, this->getDefaultFieldsMethodId);
int fieldCount = this->listUtils->getListLength(jFieldList);
struct field **fields = (struct field **)malloc(sizeof(struct field *) * fieldCount);
for (int i = 0; i < fieldCount; i++)
{
jobject jfield = static_cast<jobject>(this->jvm->CallObjectMethod(jFieldList, this->listUtils->getListItemMethod(), i));
struct field *field = (struct field *)malloc(sizeof(field) + sizeof(jfield));
field->jfield = (jobject)jfield;
fields[i] = field;
}
struct field_list *field_list = (struct field_list *)malloc(sizeof(struct field_list) + sizeof(fields));
field_list->count = fieldCount;
field_list->fields = fields;
return field_list;
}
struct packet *MessageApiEndpoint::createPacket()
{
jobject jpacket = this->jvm->CallObjectMethod(this->endpoint, this->createPacketMethodId);
struct packet *packet = (struct packet *)malloc(sizeof(struct packet) + sizeof(jpacket));
packet->jpacket = jpacket;
return packet;
}
struct record *MessageApiEndpoint::createRecord()
{
jobject jrecord = this->jvm->CallObjectMethod(this->endpoint, this->createRecordMethodId);
struct record *record = (struct record *)malloc(sizeof(struct record) + sizeof(jrecord));
record->jrecord = jrecord;
return record;
}
struct rejection *MessageApiEndpoint::createRejection(struct record *record, const char *reason)
{
jstring jreason = this->typeUtils->toJavaString(reason);
jobject jrejection = this->jvm->CallObjectMethod(this->endpoint, this->createRejectionMethodId, record->jrecord, jreason);
struct rejection *rejection = (struct rejection *)malloc(sizeof(struct rejection) + sizeof(jrejection));
rejection->jrejection = jrejection;
this->jvm->DeleteLocalRef(jreason);
return rejection;
}
/** /**
* Factored method that handles all wrapped record retrieval methods including getRecords, getRecordsByCollection, getRecordsByTransformation, * Factored method that handles all wrapped record retrieval methods including getRecords, getRecordsByCollection, getRecordsByTransformation,
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
#include "messageapi_structs.h" #include "messageapi_structs.h"
#ifdef __cplusplus #ifdef __cplusplus
#include <iostream>
#include <string>
#include "JniUtils.h" #include "JniUtils.h"
#include "ListUtils.h" #include "ListUtils.h"
#include "PacketUtils.h" #include "PacketUtils.h"
#include <iostream> #include "EndpointUtils.h"
#include <string>
/** /**
* This is the header for the MessageApiEndpoint class - this class is the native side facility * This is the header for the MessageApiEndpoint class - this class is the native side facility
...@@ -32,17 +34,11 @@ public: ...@@ -32,17 +34,11 @@ public:
~MessageApiEndpoint(); ~MessageApiEndpoint();
/*Utils Accessors*/ /*Utils Accessors*/
EndpointUtils *getEndpointUtils();
PacketUtils *getPacketUtils(); PacketUtils *getPacketUtils();
ListUtils *getListUtils(); ListUtils *getListUtils();
TypeUtils *getTypeUtils(); TypeUtils *getTypeUtils();
/*Endpoint Methods*/
struct record *getStateContainer();
struct field_list *getDefaultFields();
struct packet *createPacket();
struct record *createRecord();
struct rejection *createRejection(struct record* record, const char *reason);
/*Protocol Record Methods*/ /*Protocol Record Methods*/
jobject getProtocolRecords(const char *method, const char *key, const char *val); jobject getProtocolRecords(const char *method, const char *key, const char *val);
struct record_list *getRecords(const char *method, const char *key = NULL, const char *val = NULL); struct record_list *getRecords(const char *method, const char *key = NULL, const char *val = NULL);
...@@ -134,13 +130,7 @@ private : ...@@ -134,13 +130,7 @@ private :
TypeUtils *typeUtils; TypeUtils *typeUtils;
ListUtils *listUtils; ListUtils *listUtils;
PacketUtils *packetUtils; PacketUtils *packetUtils;
EndpointUtils *endpointUtils;
/*Endpoint Methods*/
jmethodID getStateContainerMethodId;
jmethodID getDefaultFieldsMethodId;
jmethodID createPacketMethodId;
jmethodID createRecordMethodId;
jmethodID createRejectionMethodId;
/*Protocol Record Methods*/ /*Protocol Record Methods*/
jmethodID getRecordsMethodId; jmethodID getRecordsMethodId;
...@@ -184,7 +174,6 @@ private : ...@@ -184,7 +174,6 @@ private :
/*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/ /*Load method IDS for reuse. MethodIDS do not count against the jref count and do need to be released.*/
void loadEndpointMethodIds();
void loadProtocolRecordMethodIds(); void loadProtocolRecordMethodIds();
void loadRecordMethodIds(); void loadRecordMethodIds();
void loadRejectionMethodIds(); void loadRejectionMethodIds();
...@@ -192,7 +181,6 @@ private : ...@@ -192,7 +181,6 @@ private :
void loadConditionMethodIds(); void loadConditionMethodIds();
/*Grouped methods for returning the matching method signature string for a given interface*/ /*Grouped methods for returning the matching method signature string for a given interface*/
const char *getEndpointMethodSignature(const char *endpointMethodName);
const char *getProtocolRecordMethodSignature(const char *protocolRecordMethodName); const char *getProtocolRecordMethodSignature(const char *protocolRecordMethodName);
const char *getRecordMethodSignature(const char *recordMethodName); const char *getRecordMethodSignature(const char *recordMethodName);
const char *getRejectionMethodSignature(const char *rejectionMethodName); const char *getRejectionMethodSignature(const char *rejectionMethodName);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment