/* ------------------------------------------------------------------------- */ int Reg_Update(Reg_TRegistry* reg, const ezxml_t items) { ezxml_t new_item; assert(reg); /* sanity check */ if (!(new_item = items->child)) return -EXIT_SUCCESS; while(new_item) { if ('\0' == ezxml_txt(new_item)[0]) return -EXIT_FAILURE; new_item = new_item->ordered; } /* update */ new_item = items->child; while(new_item) { ezxml_t existing_item; if ((existing_item = ezxml_child(reg->m_root, ezxml_name(new_item)))) ezxml_set_txt_d(existing_item, ezxml_txt(new_item)); else { ezxml_t n = ezxml_add_child_d(reg->m_root, ezxml_name(new_item), 0); ezxml_set_txt_d(n, ezxml_txt(new_item)); } ezxml_remove(new_item); new_item = items->child; } if (reg->m_file_name) Reg_ExportToFile(reg, reg->m_file_name); return -EXIT_SUCCESS; }
/* ------------------------------------------------------------------------- */ void Servant_OnMessageReceived(TServant *const servant, const ezxml_t message) { s_on_message_received++; switch(s_test_case) { case 6: assert('\0' != ezxml_error(message)[0]); /* error */ break; case 7: assert('\0' == ezxml_error(message)[0]); /* no error */ { const char *const s = ezxml_name(message); assert(strcmp(s, "retrieve") == 0); } break; } }
/* Load a BSDF struct from the given file (free first and keep name) */ SDError SDloadFile(SDData *sd, const char *fname) { SDError lastErr; ezxml_t fl, wtl; if ((sd == NULL) | (fname == NULL || !*fname)) return SDEargument; /* free old data, keeping name */ SDfreeBSDF(sd); /* parse XML file */ fl = ezxml_parse_file(fname); if (fl == NULL) { sprintf(SDerrorDetail, "Cannot open BSDF \"%s\"", fname); return SDEfile; } if (ezxml_error(fl)[0]) { sprintf(SDerrorDetail, "BSDF \"%s\" %s", fname, ezxml_error(fl)); ezxml_free(fl); return SDEformat; } if (strcmp(ezxml_name(fl), "WindowElement")) { sprintf(SDerrorDetail, "BSDF \"%s\": top level node not 'WindowElement'", sd->name); ezxml_free(fl); return SDEformat; } wtl = ezxml_child(fl, "FileType"); if (wtl != NULL && strcmp(ezxml_txt(wtl), "BSDF")) { sprintf(SDerrorDetail, "XML \"%s\": wrong FileType (must be 'BSDF')", sd->name); ezxml_free(fl); return SDEformat; } wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); if (wtl == NULL) { sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'", sd->name); ezxml_free(fl); return SDEformat; } /* load geometry if present */ lastErr = SDloadGeometry(sd, wtl); if (lastErr) { ezxml_free(fl); return lastErr; } /* try loading variable resolution data */ lastErr = SDloadTre(sd, wtl); /* check our result */ if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ lastErr = SDloadMtx(sd, wtl); /* done with XML file */ ezxml_free(fl); if (lastErr) { /* was there a load error? */ SDfreeBSDF(sd); return lastErr; } /* remove any insignificant components */ if (sd->rf != NULL && sd->rf->maxHemi <= .001) { SDfreeSpectralDF(sd->rf); sd->rf = NULL; } if (sd->rb != NULL && sd->rb->maxHemi <= .001) { SDfreeSpectralDF(sd->rb); sd->rb = NULL; } if (sd->tf != NULL && sd->tf->maxHemi <= .001) { SDfreeSpectralDF(sd->tf); sd->tf = NULL; } if (sd->tb != NULL && sd->tb->maxHemi <= .001) { SDfreeSpectralDF(sd->tb); sd->tb = NULL; } /* return success */ return SDEnone; }
/* ------------------------------------------------------------------------- */ static int OnMessageReceived(TServant *const servant, ezxml_t message) { int ret = -EXIT_SUCCESS; #ifdef UNIT_TESTS if (g_Servant_OnMessageReceived) g_Servant_OnMessageReceived(servant, message); #endif assert(message); do { const char *req; const char *error; error = ezxml_error(message); if ('\0' != error[0]) { LOG2_ERROR("servant", "the session (%u) received an invalid " "formated xml message: %s", servant->m_session.m_id, error); ret = -EXIT_FAILURE; break; } req = ezxml_name(message); LOG2_DEBUG("servant", "the session (%u) received '%s' request", servant->m_session.m_id, req); if (strcmp(req, "update") == 0) { if (servant->m_reg) { if ((ret = Reg_Update(servant->m_reg, message))) LOG2_ERROR("servant", "the session (%u) failed to update " "the registry", servant->m_session.m_id, req); } } else if (strcmp(req, "retrieve") == 0) { if (servant->m_reg) { ezxml_t status = ezxml_new("status"); ret = Reg_Get(servant->m_reg, message, status); TxMessage(servant, status); ezxml_free(status); LOG2_DEBUG("servant", "the session (%u) replied to '%s' request", servant->m_session.m_id, req); } } else { LOG2_WARNING("servant", "the session (%u) received an undefined " "request (%s)", servant->m_session.m_id, req); ret = -EXIT_FAILURE; } } while(0); ezxml_free(message); return ret; }
/* ------------------------------------------------------------------------- */ int main(int argc, char* argv[]) { int ret; ezxml_t xml; bool retrieve; TMessage message; int connection; if (argc < 2) { printf("usage: %s <xml file>\n", argv[0]); return -EXIT_FAILURE; } xml = ezxml_parse_file(argv[1]); if (!xml) { printf("Failed to parse the file '%s'.\n", argv[1]); return -EXIT_FAILURE; } message.m_body = ezxml_toxml(xml, false); message.m_len = strlen(message.m_body); { const char *const name = ezxml_name(xml); retrieve = (0 == strcmp("retrieve", name)); } ezxml_free(xml); ret = -EXIT_FAILURE; do { if (-1 == (connection = Connect(SERVER_IP, SERVER_PORT))) break; if (SendMessage(connection, &message)) { printf("Failed to send the message.\n"); break; } if (!retrieve) { ret = -EXIT_SUCCESS; break; } if (ReceiveMessage(connection, &message)) { printf("Failed to receive the reply message.\n"); break; } xml = ezxml_parse_str(message.m_body, message.m_len); if (xml) { const char *const str = ezxml_toxml(xml, true); if (str) { puts(str); free((char*)str); } ezxml_free(xml); } else { printf("Failed to parse the reply message.\n"); break; } ret = -EXIT_SUCCESS; } while(0); free(message.m_body); close(connection); return ret; }