#ifndef __CPPWRAPPER_H #define __CPPWRAPPER_H #include "time.h" #include "value.h" #include "fstream" #include "process.h" // ProcId #include "model.h" //MachineId #include "portlist.h" #include "root.h" //#include "message.h" #include #include #include #include #include //#define WITH_PCDPP #define ECHO_MESSAGE_COUNT 50 #define LOCAL_MESSAGE_COUNT 50 typedef map< int , int , less > ProcMachineIDs; typedef map< string , int, less > ZonePartitions; enum { WRAPPER_ORIGINATED = 1, CDPP_ORIGINATED = 2 } ; enum { CURRENT_SIMULATION_TIME_REQUEST = 1, CURRENT_SIMULATION_TIME_RESPONSE = 2, INSERT_EXTERNAL_EVENT_REQUEST = 3, MACHINE_FOR_MODEL_REQUEST = 4, MACHINE_FOR_MODEL_RESPONSE = 5, SIMULATION_STOP = 6, GET_MACHINE_ID_REQUEST = 7, GET_MACHINE_ID_RESPONSE = 8, INIT_MESSAGE = 9, DONE_MESSAGE = 10, COLLECT_MESSAGE = 11, INTERNAL_MESSAGE = 12, EXTERNAL_MESSAGE = 13, OUTPUT_MESSAGE = 14, ADD_ZONE_PARTITION = 15, ECHO_MESSAGE = 16 } ; struct messageBuffer { long mtype; long specific_type; int session_id; long longParam1; long longParam2; long longParam3; long longParam4; long longParam5; long longParam6; long longParam7; bool boolParam1; double doubleParam1; char charParam1[80]; char charParam2[80]; char charParam3[80]; } ; class EchoMessage; class CPPWrapper { public: CPPWrapper(); ~CPPWrapper(); CPPWrapper& getCurrentSimTime() ; CPPWrapper& insertExternalEvent(messageBuffer *); CPPWrapper& setJniParams(JNIEnv *,jobject); void setSessionLogFileName(string); ofstream& sessionLogStream(); ostream& debugLogStream(); CPPWrapper& saveDebugInfo(); CPPWrapper& setSessionID(int); int getSessionID() const; CPPWrapper& setPath(string); string getPath() const ; CPPWrapper& initializeMessageQueues(); CPPWrapper& startMessageMonitor(); int machineForModel(const string) ; CPPWrapper& stop(bool calledByRoot = false); int getMachineID(); const ProcMachineIDs &getProcMachineIds() const; const MachineId machineForProcId(const ProcId &) const ; CPPWrapper &setMachineForProcId(const ProcId &,const MachineId &); CPPWrapper &printMachineForProc(); CPPWrapper &sendRemoteMessage(const InitMessage &, const MachineId&, const ProcId& ); CPPWrapper &sendRemoteMessage(const DoneMessage &, const MachineId& , const ProcId& ); CPPWrapper &sendRemoteMessage(const CollectMessage &, const MachineId& , const ProcId& ); CPPWrapper &sendRemoteMessage(const InternalMessage &, const MachineId&, const ProcId& ); CPPWrapper &sendRemoteMessage(const ExternalMessage &, const MachineId&, const ProcId& ); CPPWrapper &sendRemoteMessage(const OutputMessage &, const MachineId&, const ProcId& ); CPPWrapper &sendEchoMessage(const EchoMessage & , const MachineId &); CPPWrapper &receiveEchoMessage(const messageBuffer &); CPPWrapper &receiveRemoteMessage(const messageBuffer &); CPPWrapper &addPortToList(const PortId&, Port*) ; Port* getPortFromList(const PortId &); const ZonePartitions &getZonePartitions() const { return zonePartitions ; } CPPWrapper &addZonePartition(const char* zoneChar , const MachineId &); const bool isMessageMonitorStarted() const { return messageMonitorStarted ; } CPPWrapper &isMessageMonitorStarted(bool mmStarted) { messageMonitorStarted = mmStarted; return *this; } CPPWrapper &setFCId(const ProcId&); const ProcId &getFCId() const { return fcId; } const list &getMessageTransferDelays() const { return messageTransferDelays ; } const list &getCPPTransferDelays() const { return cppTransferDelays ; } timeval &getMessageSendTime() { return messageSendTime ; } timeval &getMessageReceiveTime() { return messageReceiveTime ; } CPPWrapper& calSOAPMessageDelay(); CPPWrapper& calCPPMessageDelay(); CPPWrapper& addCPPTransferDelay(); private: JNIEnv * jni_env; jobject jni_obj; string sessionLogFileName; ofstream sessionLog; ofstream debugLog; int sessionID; string cdppPath; int send_queue_id; int receive_queue_id; pthread_t messageMonitorThread; ProcMachineIDs procMachineIds; int machineId ; PortList allPorts; ZonePartitions zonePartitions; bool messageMonitorStarted; list messageTransferDelays; list SOAPTransferDelays; list cppTransferDelays; struct timeval messageSendTime ; struct timeval messageReceiveTime ; ProcId fcId; }; class SingleCPPWrapper { public: static CPPWrapper& Instance(); private: static CPPWrapper* instance; }; inline CPPWrapper& SingleCPPWrapper::Instance() { if(instance ==0) instance = new CPPWrapper(); return *instance; } inline int open_queue(key_t keyVal) { int qid; if((qid = msgget(keyVal, IPC_CREAT | 0660 )) == -1) { cout << "CPPWrapper::open_queue failed to open queue for keyVal" << keyVal << endl; return -1; } return(qid); } inline int read_message(int qid, long msg_type, struct messageBuffer* qbuf) { int result, length; length = sizeof(struct messageBuffer) - sizeof(long); if((result = msgrcv(qid, qbuf, length, msg_type, 0 )) == -1) { return -1; } return result; } inline int send_message(int qid, struct messageBuffer* qbuf) { int result, length; length = sizeof(struct messageBuffer) - sizeof(long); if ((result = msgsnd(qid, qbuf, length, 0)) == -1) { return -1; } return result; } inline void * messageMonitor(void * qid) { int queue_id = *((int *) &qid); messageBuffer msg; int result = -1; cout << "liscening on queue " << queue_id << endl; SingleCPPWrapper::Instance().isMessageMonitorStarted( true ); while(true) { if((result = read_message(queue_id, WRAPPER_ORIGINATED, &msg)) != -1) { switch(msg.specific_type) { case CURRENT_SIMULATION_TIME_REQUEST: SingleCPPWrapper::Instance().getCurrentSimTime(); break; case INSERT_EXTERNAL_EVENT_REQUEST: SingleCPPWrapper::Instance().insertExternalEvent(&msg); break; case SIMULATION_STOP: SingleCPPWrapper::Instance().stop(); break; case INIT_MESSAGE: case DONE_MESSAGE: case COLLECT_MESSAGE: case INTERNAL_MESSAGE: case EXTERNAL_MESSAGE: case OUTPUT_MESSAGE: SingleCPPWrapper::Instance().receiveRemoteMessage(msg); break; case ECHO_MESSAGE: SingleCPPWrapper::Instance().receiveEchoMessage(msg); break; case ADD_ZONE_PARTITION: // cout << "MessageMonitor:: received add-zone-partiton " << msg.charParam1 << " : " << msg.longParam1 << endl; SingleCPPWrapper::Instance().addZonePartition(msg.charParam1, msg.longParam1); break; } } } } #endif