📄 communicate.cpp
字号:
#include "Communicate.h"Communicate::Communicate(string type) :m_type(type){ fromCA = fromADT = m_flag = 0; toCA = toADT = 0; if (m_type=="ca") initializeCA(); else if (m_type=="adt") { initializeADT(); }}Communicate::~Communicate(){ if (m_type=="ca") { freeCA(); } else if (m_type=="adt") { freeADT(); }}void Communicate::freeCA(){ msgctl(fromADT, IPC_RMID, 0); msgctl(toADT, IPC_RMID, 0);}void Communicate::freeADT(){ msgctl(fromCA, IPC_RMID, 0); msgctl(toCA, IPC_RMID, 0);}CONTENT Communicate::caGetMessageFromADT(){ return getMessage("ca");}int Communicate::caSendMessageToADT(CONTENT msg){ return sendMessage(msg, "ca");}CONTENT Communicate::ADTGetMessageFromCA(){ return getMessage("adt");}int Communicate::ADTSendMessageToCA(CONTENT msg){ return sendMessage(msg, "adt");}CONTENT Communicate::getMessage(string objectName){ int object = 0; if (objectName=="ca") { object = fromADT; } else if (objectName=="adt") { object = fromCA; } MSG msg; while (1) { if (msgrcv(object, (struct msgbuf*)&msg, SIZE, PKI_SERVICE, IPC_NOWAIT) > 0) { break; } sleep(1); } CONTENT content; char* pt = msg.content; content.type = atol(pt); int pos = 0; while ((*(pt+pos)>=0x30) && (*(pt+pos)<=0x39)) { pos ++; } strcpy(content.buf, pt+pos); return content;}int Communicate::sendMessage(CONTENT msg, string objectName){ int object = 0; if (objectName=="ca") { object = toADT; } else if (objectName=="adt") { object = toCA; } MSG message; message.type = PKI_SERVICE; char type[7]; memset(type, 0, 7); sprintf(type, "%d", msg.type); strcpy(message.content, type); strcat(message.content, msg.buf); errno = 0; if (msgsnd(object, (struct msgbuf*)&message, SIZE, 0)== -1) { cout << "error : "<<strerror(errno)<<endl; } return 1;}void Communicate::initializeADT(){ if (readCAconfig("Communication", "rCACom")=="no") { m_flag=toCA=msgget(ftok(".",'a'), IPC_CREAT|0666); setCAConfigItem("Communication", "rCACom", "yes"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; } else { m_flag=toCA=msgget(ftok(".",'a'), 0666); setCAConfigItem("Communication", "rCACom", "no"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; } if (readCAconfig("Communication", "wCACom")=="no") { m_flag=fromCA = msgget(ftok("/ngg", 'a'), IPC_CREAT|0666); setCAConfigItem("Communication", "wCACom", "yes"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; } else { m_flag=fromCA = msgget(ftok("/ngg", 'a'), 0666); setCAConfigItem("Communication", "wCACom", "no"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; }}void Communicate::initializeCA(){ if (readCAconfig("Communication", "rCACom")=="no") { m_flag=fromADT=msgget(ftok(".",'a'), IPC_CREAT|0666); setCAConfigItem("Communication", "rCACom", "yes"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; } else { m_flag=fromADT=msgget(ftok(".",'a'), 0666); setCAConfigItem("Communication", "rCACom", "no"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; } if (readCAconfig("Communication", "wCACom")=="no") { m_flag=toADT = msgget(ftok("/ngg", 'a'), IPC_CREAT|0666); setCAConfigItem("Communication", "wCACom", "yes"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; } else { m_flag=toADT = msgget(ftok("/ngg", 'a'), 0666); setCAConfigItem("Communication", "wCACom", "no"); if (m_flag==-1) cout<<"msgget error : " <<strerror(errno)<<endl, m_flag=0; }}string Communicate::readCAconfig(string keyword, string objectName){ string result = ""; FILE *fd = fopen(CA_CONFIG_PATH, "r"); assert(fd != NULL); if (fd == NULL) { result.append(""); } else { char *line = (char*)calloc(1, 1*K); string config=""; while (!feof(fd)) { memset(line, 0, 1*K); fgets(line, 1*K, fd); config.append(line); } fclose(fd); NggObject *object = NggObject::fromXML(config); assert( object!=NULL ); for (unsigned int i=0; i<object->m_Contains.size(); i++) { if (object->m_Contains[i]->m_Name==keyword) { if (keyword == "Root" || keyword == "Operator" || keyword == "Administrator"|| keyword=="SerialNumber") { result.append(object->m_Contains[i]->m_Value); } else { NggObject* nggobject = object->m_Contains[i]; assert( nggobject!=NULL ); for (unsigned int ii=0; ii<nggobject->m_Contains.size(); ii++) { if (nggobject->m_Contains[ii]->m_Name==objectName) { result.append(nggobject->m_Contains[ii]->m_Value); break; } } if (nggobject) nggobject=NULL; } break; } } if (object) delete object, object=NULL ; if (line) free(line), line=NULL; } return result;}void Communicate::setCAConfigItem(string keyword, string objectName, string value){ FILE *fd = fopen(CA_CONFIG_PATH, "rb"); assert(fd != NULL); char *line = (char*)calloc(1, 1*K); assert(line != NULL); string config = ""; while (!feof(fd)) { memset(line, 0, 1*K); fgets(line, 1*K, fd); config.append(line); } fclose(fd); NggObject *object = NggObject::fromXML(config); assert(object != NULL); for (unsigned int i=0; i<object->m_Contains.size(); i++) { if (object->m_Contains[i]->m_Name==keyword) { if (keyword == "Root" || keyword == "Operator" || keyword == "Administrator"|| keyword=="SerialNumber") { object->m_Contains[i]->m_Value = value; } else { NggObject *nggobject = object->m_Contains[i]; assert(object != NULL); for (unsigned int ii=0; ii<nggobject->m_Contains.size(); ii++) { if (nggobject->m_Contains[ii]->m_Name==objectName) { nggobject->m_Contains[ii]->m_Value = value; break; } } if (nggobject) nggobject=NULL; } break; } } fd = fopen(CA_CONFIG_PATH, "wb"); assert( fd!=NULL ); fputs(object->toXML().c_str(), fd); if (object) delete object, object=NULL; if (line) free(line), line=NULL; fclose(fd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -