int main(int argc, char *argv[]) { SoapCtx *req, *resp; herror_t err; err = soap_client_init_args(argc, argv); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } err = soap_ctx_new_with_method(urn, "computePi", &req); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } soap_env_add_item(req->env, "xsd:decimal", "precision", "10000"); if (argc > 1) err = soap_client_invoke(req, &resp, argv[1], ""); else err = soap_client_invoke(req, &resp, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(req); return 1; } soap_xml_doc_print(resp->env->root->doc); soap_ctx_free(resp); soap_ctx_free(req); soap_client_destroy(); return 0; }
void *reader_thread(void *arg) { SoapCtx *ctx, *ctx2; xmlNodePtr xmlcur,xmlitem,xmlitem2; herror_t err; char *itemname,*itemvalue,buf[1024], *cptr; int ret; THREAD_PARAM *p = (THREAD_PARAM *) arg; const char *url = (const char *) p->user; rlMailbox rlmbx(mbx); // read mbx until it is empty rlmbx.clear(); printf("reader_thread starting\n"); // wait for commands from clients while(1) { ret = rlmbx.read(buf,sizeof(buf)); // read "itemname,itemvalue\n" if(ret <= 0) continue; itemname = itemvalue = &buf[0]; cptr = strchr(buf,','); if(cptr != NULL) { *cptr = '\0'; cptr++; itemvalue = cptr; cptr = strchr(itemvalue,'\n'); if(cptr != NULL) *cptr = 0; } if(debug) { printf("reader_thread Write itemname=%s itemvalue=%s\n",itemname,itemvalue); } p->thread->lock(); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Write", &ctx); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); goto end_of_while; } /* create the ItemList */ xmlitem = soap_env_add_item(ctx->env, "xsd:element", "ItemList", ""); xmlcur = ctx->env->cur; ctx->env->cur = xmlitem; xmlitem2 = soap_env_add_item(ctx->env, "xsd:string", "Items", NULL); if (!xmlNewProp(xmlitem2, BAD_CAST "ItemName", BAD_CAST itemname)) { log_error1("Can not create new xml attribute ItemName"); goto end_of_while; } ctx->env->cur = xmlitem2; if(isdigit(itemvalue[0])) { soap_env_add_item(ctx->env,"xsd:double","Value",itemvalue); } else if(strcmp(itemvalue, "true") == 0 || strcmp(itemvalue, "false") == 0) { soap_env_add_item(ctx->env, "xsd:boolean", "Value", itemvalue); } else { soap_env_add_item(ctx->env,"xsd:istring","Value",itemvalue); } ctx->env->cur = xmlitem; ctx->env->cur = xmlcur; /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); goto end_of_while; } /* print the result */ if(debug) { printf("reader_thread result:\n"); soap_xml_doc_print(ctx2->env->root->doc); } end_of_while: /* free the objects */ soap_ctx_free(ctx2); soap_ctx_free(ctx); p->thread->unlock(); } return NULL; }
int main(int argc, char *argv[]) { SoapCtx *ctx, *ctx2; herror_t err; char *url,*method,*arg; int i; /* check the command line parameters */ if(argc < 3) { printusage(argv[0]); return 1; } arg = argv[0]; strcpy(startme,arg); for(i=1; i<argc; i++) { arg = argv[i]; strcat(startme," "); strcat(startme,arg); } url = argv[1]; method = argv[2]; for(i=3; i<argc; i++) { arg = argv[i]; if(strlen(arg) <= MAX_PATH_LENGTH) { if(strncmp(arg,"-itemlist=",10) == 0) { sscanf(arg,"-itemlist=%s",itemlist); } if(strncmp(arg,"-shm=",5) == 0) { sscanf(arg,"-shm=%s",shm); } if(strncmp(arg,"-mbx=",5) == 0) { sscanf(arg,"-mbx=%s",mbx); } if(strncmp(arg,"-sleep=",7) == 0) { sscanf(arg,"-sleep=%d",&sleep); if(sleep < 10) sleep = 10; } if(strncmp(arg,"-max_name_length=",17) == 0) { sscanf(arg,"-max_name_length=%d",&max_name_length); if(max_name_length < 31) max_name_length = 31; } if(strncmp(arg,"-shmsize=",8) == 0) { sscanf(arg,"-shmsize=%ld",&shmsize); if(shmsize < 1) shmsize = 1; } if(strcmp(arg,"-debug") == 0) { debug = 1; } } else { printf("%s is too long.\n", arg); return 1; } } /* init cSOAP client */ err = soap_client_init_args(argc, argv); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } if(strcmp(method,"GetStatus") == 0) { /* create a SoapCtx object */ printf("soap_ctx_new_with_method(%s,\"GetStatus\")\n", URN); err = soap_ctx_new_with_method(URN, "GetStatus", &ctx); if (err != H_OK) { printf("err != H_OK\n"); log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } /* invoke */ printf("soap_client_invoke(\"%s\")\n", url); err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { printf("err != H_OK\n"); log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); return 1; } /* print the result */ soap_xml_doc_print(ctx2->env->root->doc); /* free the objects */ printf("soap_ctx_free()\n"); soap_ctx_free(ctx2); soap_ctx_free(ctx); /* destroy the cSOAP client */ printf("soap_client_destroy()\n"); soap_client_destroy(); return 0; } else if(strcmp(method,"Browse") == 0) { printf("#%s %s %s\n",argv[0],url,method); printf("#\n"); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Browse", &ctx); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); return 1; } /* browse the childs */ browse_childs(url, ctx2->env->cur->children); /* free the objects */ soap_ctx_free(ctx2); soap_ctx_free(ctx); /* destroy the cSOAP client */ soap_client_destroy(); return 0; } else if(strcmp(method,"Run") == 0) { i = getMaxItemNameLength(); if(i > 0) i = run(url,i); soap_client_destroy(); return i; } else { printusage(argv[0]); soap_client_destroy(); } return 0; }
int run(const char *url, int maxItemNameLength) { long shmsize_needed = ((maxItemNameLength+1)+(max_name_length+1))*num_items + sizeof(SHM_HEADER); FILE *fin; char buf[1024], *cptr; void *shmadr; SHM_HEADER *shmheader; int i; SoapCtx *ctx, *ctx2; xmlNodePtr xmlcur,xmlitem; herror_t err; if(url == NULL) return 1; // print shm parameters printf("maxItemNameLength=%d max_name_length=%d shmsize=%ld shmsize_needed=%ld num_items=%d shm=%s\n", maxItemNameLength, max_name_length, shmsize, shmsize_needed, num_items, shm); if(shmsize_needed > shmsize) { printf("shmsize too small -> increase it\n"); return 1; } if(maxItemNameLength <= 0 || max_name_length <= 0 || shmsize <= 0 || shmsize_needed <= 0) { printf("some values are negative or 0\n"); return 1; } if(maxItemNameLength >= (int) (sizeof(buf) - 100) || max_name_length >= (int) (sizeof(buf) - 100)) { printf("name is bigger than buf length = %d\n", (int) sizeof(buf)); return 1; } // init shared memory rlSharedMemory rlshm = rlSharedMemory(shm, (unsigned long) shmsize); if(rlshm.status != rlSharedMemory::OK) { printf("shared memory status is not OK\n"); return 1; } shmadr = rlshm.getUserAdr(); if(shmadr == NULL) { printf("shmadr = NULL\n"); return 1; } memset(shmadr,0,shmsize); // read itemlist to shared memory fin = fopen(itemlist,"r"); if(fin == NULL) { printf("could not open itemlist %s\n",itemlist); return 1; } i = 0; while(fgets(buf,sizeof(buf)-1,fin) != NULL) { if(buf[0] > ' ' && buf[0] != '#') { cptr = strchr(buf,'\n'); if(cptr != NULL) *cptr = '\0'; cptr = (char *) shmadr; cptr += sizeof(SHM_HEADER) + (i*(maxItemNameLength+1 + max_name_length+1)); strcpy(cptr,buf); i++; } } fclose(fin); // init header in shared memory shmheader = (SHM_HEADER *) shmadr; shmheader->maxItemNameLength = maxItemNameLength; shmheader->maxNameLength = max_name_length; shmheader->numItems = num_items; shmheader->readErrorCount = 0; shmheader->writeErrorCount = 0; strcpy(shmheader->ident,"opc"); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Read", &ctx); if(err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } /* create the Read ItemList */ xmlitem = soap_env_add_item(ctx->env, "xsd:element", "ItemList", ""); xmlcur = ctx->env->cur; ctx->env->cur = xmlitem; for(i=0; i<shmheader->numItems; i++) { cptr = (char *) shmadr; cptr += sizeof(SHM_HEADER) + (i*(maxItemNameLength+1 + max_name_length+1)); sprintf(buf,"Items ItemName=\"%s\"",cptr); soap_env_add_item(ctx->env, "xsd:string", buf, NULL); } ctx->env->cur = xmlcur; // create reader thread and the watchdog rlThread reader,watchdog; reader.create(reader_thread,(void *) url); watchdog.create(watchdog_thread,NULL); // poll the OPC XML-DA server forever while(1) { /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if(err == H_OK) { /* print the result */ if(debug) soap_xml_doc_print(ctx2->env->root->doc); /* write the result to the shared memory */ reader.lock(); write_to_shared_memory(ctx2,shmadr); reader.unlock(); /* free the objects */ soap_ctx_free(ctx2); } watchcnt1++; if(watchcnt1 > 256*256) watchcnt1 = 0; rlsleep(sleep); } return 0; }