00001 #include "PythonCCS-client.h"
00002
00003 PythonExecute::PythonExecute(char *_code, bool _persistent, bool _highlevel, CmiUInt4 _interp) {
00004 magic = sizeof(*this) ^ localmagic;
00005 codeLength = strlen(_code);
00006 code.code = strdup(_code);
00007 methodNameLength = 0;
00008 methodName.methodName = 0;
00009 infoSize = 0;
00010 info.info = 0;
00011 flags = 0;
00012 if (_persistent) {
00013 flags |= FLAG_PERSISTENT;
00014 flags |= FLAG_KEEPPRINT;
00015 }
00016 if (_highlevel) flags |= FLAG_HIGHLEVEL;
00017 interpreter = _interp;
00018 }
00019
00020 PythonExecute::PythonExecute(char *_code, char *_method, PythonIterator *_info, bool _persistent, bool _highlevel, CmiUInt4 _interp) {
00021 magic = sizeof(*this) ^ localmagic;
00022 codeLength = strlen(_code);
00023 code.code = strdup(_code);
00024 methodNameLength = strlen(_method);
00025 methodName.methodName = strdup(_method);
00026 infoSize = _info->size();
00027 info.info = (PythonIterator *)_info->pack();
00028 flags = 0;
00029 if (_persistent) {
00030 flags |= FLAG_PERSISTENT;
00031 flags |= FLAG_KEEPPRINT;
00032 }
00033 if (_highlevel) flags |= FLAG_HIGHLEVEL;
00034 flags |= FLAG_ITERATE;
00035 interpreter = _interp;
00036 }
00037
00038 PythonExecute::~PythonExecute() {
00039 if (code.code) free(code.code);
00040 if (methodName.methodName) free(methodName.methodName);
00041 if (info.info) free(info.info);
00042 }
00043
00044 void PythonExecute::setCode(char *_set) {
00045 codeLength = strlen(_set);
00046 code.code = strdup(_set);
00047 }
00048
00049 void PythonExecute::setMethodName(char *_set) {
00050 methodNameLength = strlen(_set);
00051 methodName.methodName = strdup(_set);
00052 }
00053
00054 void PythonExecute::setIterator(PythonIterator *_set) {
00055 infoSize = _set->size();
00056 info.info = (PythonIterator *)_set->pack();
00057 }
00058
00059 void PythonExecute::setPersistent(bool _set) {
00060 if (_set) flags |= FLAG_PERSISTENT;
00061 else flags &= ~FLAG_PERSISTENT;
00062 }
00063
00064 void PythonExecute::setIterate(bool _set) {
00065 if (_set) flags |= FLAG_ITERATE;
00066 else flags &= ~FLAG_ITERATE;
00067 }
00068
00069 void PythonExecute::setHighLevel(bool _set) {
00070 if (_set) flags |= FLAG_HIGHLEVEL;
00071 else flags &= ~FLAG_HIGHLEVEL;
00072 }
00073
00074 void PythonExecute::setKeepPrint(bool _set) {
00075 if (_set) flags |= FLAG_KEEPPRINT;
00076 else flags &= ~FLAG_KEEPPRINT;
00077 }
00078
00079 void PythonExecute::setWait(bool _set) {
00080 if (_set) flags |= FLAG_WAIT;
00081 else flags &= ~FLAG_WAIT;
00082 }
00083
00084 void PythonExecute::setNoCheck(bool _set) {
00085 if (_set) flags |= FLAG_NOCHECK;
00086 else flags &= ~FLAG_NOCHECK;
00087 }
00088
00089 int PythonExecute::size() {
00090 return sizeof(PythonExecute)+codeLength+1+methodNameLength+1+infoSize;
00091 }
00092
00093 char *PythonExecute::pack() {
00094 void *memory = malloc(size());
00095 memcpy (memory, (void*) this, sizeof(PythonExecute));
00096 char *ptr = (char*)memory+sizeof(PythonExecute);
00097 if (codeLength) {
00098 memcpy (ptr, code.code, codeLength+1);
00099 ptr += codeLength+1;
00100 }
00101 if (methodNameLength) {
00102 memcpy (ptr, methodName.methodName, methodNameLength+1);
00103 ptr += methodNameLength+1;
00104 }
00105 if (infoSize) {
00106 memcpy (ptr, (void*)info.info, infoSize);
00107 }
00108
00109 ((PythonAbstract*)memory)->magic = htonl(magic);
00110
00111 ((PythonExecute*)memory)->codeLength = htonl(codeLength);
00112 ((PythonExecute*)memory)->methodNameLength = htonl(methodNameLength);
00113 ((PythonExecute*)memory)->infoSize = htonl(infoSize);
00114
00115 return (char*)memory;
00116 }
00117
00118 void PythonExecute::unpack() {
00119
00120 interpreter = ntohl(interpreter);
00121 codeLength = ntohl(codeLength);
00122 methodNameLength = ntohl(methodNameLength);
00123 infoSize = ntohl(infoSize);
00124 if (codeLength) code.code = (char*)this + sizeof(PythonExecute);
00125 if (methodNameLength) methodName.methodName = code.code + codeLength+1;
00126 if (infoSize) info.info = (PythonIterator*) (methodName.methodName + methodNameLength+1);
00127 }
00128
00129 void PythonAbstract::unpack() {
00130 magic = ntohl(magic);
00131 }
00132
00133 bool PythonAbstract::isFinished() {
00134 return (magic == (sizeof(PythonFinished) ^ PythonFinished::localmagic));
00135 }
00136
00137 bool PythonAbstract::isExecute() {
00138 return (magic == (sizeof(PythonExecute) ^ PythonExecute::localmagic));
00139 }
00140
00141 bool PythonAbstract::isPrint() {
00142 return (magic == (sizeof(PythonPrint) ^ PythonPrint::localmagic));
00143 }
00144
00145 PythonPrint::PythonPrint(CmiUInt4 _interp, bool Wait, bool Kill) {
00146 magic = sizeof(*this) ^ localmagic;
00147 interpreter = _interp;
00148 flags = 0;
00149 if (Wait) flags |= FLAG_WAIT;
00150 if (Kill) flags |= FLAG_KILL;
00151 }
00152
00153 void PythonPrint::setWait(bool _set) {
00154 if (_set) flags |= FLAG_WAIT;
00155 else flags &= ~FLAG_WAIT;
00156 }
00157
00158 void PythonPrint::setKill(bool _set) {
00159 if (_set) flags |= FLAG_KILL;
00160 else flags &= ~FLAG_KILL;
00161 }
00162
00163 PythonFinished::PythonFinished(CmiUInt4 _interp, bool Wait) {
00164 magic = sizeof(*this) ^ localmagic;
00165 interpreter = _interp;
00166 flags = 0;
00167 if (Wait) flags |= FLAG_WAIT;
00168 }
00169
00170 void PythonFinished::setWait(bool _set) {
00171 if (_set) flags |= FLAG_WAIT;
00172 else flags &= ~FLAG_WAIT;
00173 }
00174
00175 void PythonExecute::print() {
00176 printf("magic %d, interpreter %d, flags 0x%x\n",magic ,interpreter, flags);
00177 }
00178
00179 void PythonPrint::print() {
00180 printf("magic %d, interpreter %d, flags 0x%x\n",magic, interpreter, flags);
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190