Пример #1
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;
}
Пример #2
0
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;
}
Пример #3
0
int browse_childs(char *url, xmlNodePtr node)
{
  SoapCtx *ctx, *ctx2;
  herror_t err;
  const char *itemname, *isitem, *haschildren;

  while(node != NULL)
  {
    if(strcmp((const char *) node->name, "Elements") == 0)
    {
      itemname    = (const char *) xmlGetProp(node, (xmlChar*) "ItemName");
      isitem      = (const char *) xmlGetProp(node, (xmlChar*) "IsItem");
      haschildren = (const char *) xmlGetProp(node, (xmlChar*) "HasChildren");
      if(haschildren != NULL && strcmp(haschildren,"true") == 0)
      {
        if(itemname != NULL)
        {
          /* browse childs */
          printf("#\n");
          printf("#%s\n",itemname);

          /* 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;
          }

          /* add to the request */
          soap_env_add_item(ctx->env, "xsd:string", "ItemName", itemname);

          /* 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;
          }

          /* print the result */
          //soap_xml_doc_print(ctx2->env->root->doc);

          /* browse the childs */
          browse_childs(url, ctx2->env->cur->children);

          /* free the objects */
          soap_ctx_free(ctx2);
          soap_ctx_free(ctx);
        }  
      }
      else if(haschildren != NULL && strcmp(haschildren,"false") == 0 && isitem != NULL && strcmp(isitem,"true") == 0)
      {
        printf("%s\n", itemname);
      }
    }  
    node = node->next;
  }
  return 0;
}
Пример #4
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;
}
Пример #5
0
/*
 * resolve_symlinks - resolve symlinks to the underlying file
 *
 * Replace "path" by the absolute path to the referenced file.
 *
 * Returns 0 if OK, -1 if error.
 *
 * Note: we are not particularly tense about producing nice error messages
 * because we are not really expecting error here; we just determined that
 * the symlink does point to a valid executable.
 */
static int
resolve_symlinks(char *path)
{
#ifdef HAVE_READLINK
	struct stat buf;
	char		orig_wd[MAXPGPATH],
				link_buf[MAXPGPATH];
	char	   *fname;

	/*
	 * To resolve a symlink properly, we have to chdir into its directory and
	 * then chdir to where the symlink points; otherwise we may fail to
	 * resolve relative links correctly (consider cases involving mount
	 * points, for example).  After following the final symlink, we use
	 * getcwd() to figure out where the heck we're at.
	 *
	 * One might think we could skip all this if path doesn't point to a
	 * symlink to start with, but that's wrong.  We also want to get rid of
	 * any directory symlinks that are present in the given path. We expect
	 * getcwd() to give us an accurate, symlink-free path.
	 */
	if (!getcwd(orig_wd, MAXPGPATH))
	{
		log_error(_("could not identify current directory: %s"),
				  strerror(errno));
		return -1;
	}

	for (;;)
	{
		char	   *lsep;
		int			rllen;

		lsep = last_dir_separator(path);
		if (lsep)
		{
			*lsep = '\0';
			if (chdir(path) == -1)
			{
				log_error4(_("could not change directory to \"%s\": %s"), path, strerror(errno));
				return -1;
			}
			fname = lsep + 1;
		}
		else
			fname = path;

		if (lstat(fname, &buf) < 0 ||
			!S_ISLNK(buf.st_mode))
			break;

		rllen = readlink(fname, link_buf, sizeof(link_buf));
		if (rllen < 0 || rllen >= sizeof(link_buf))
		{
			log_error(_("could not read symbolic link \"%s\""), fname);
			return -1;
		}
		link_buf[rllen] = '\0';
		strcpy(path, link_buf);
	}

	/* must copy final component out of 'path' temporarily */
	strlcpy(link_buf, fname, sizeof(link_buf));

	if (!getcwd(path, MAXPGPATH))
	{
		log_error(_("could not identify current directory: %s"),
				  strerror(errno));
		return -1;
	}
	join_path_components(path, path, link_buf);
	canonicalize_path(path);

	if (chdir(orig_wd) == -1)
	{
		log_error4(_("could not change directory to \"%s\": %s"), orig_wd, strerror(errno));
		return -1;
	}
#endif   /* HAVE_READLINK */

	return 0;
}
Пример #6
0
int main( int argc, char** argv ) {

	SoapCtx* request;
	SoapCtx* response;
	herror_t error;
	int ii;

	/* The array of parameter values to be fetched from argv. */
	char* parameters[Parameter_Num_Pos];

	xmlChar* buildID;

	if ( argc != Parameter_Num_Pos + 1 ) {
		printf( "Regresstor: Invalid Number of arguments! Num given: %d\n", argc );
		printUsage();
		return EXIT_FAILURE;
	}

	initParametersArray( parameters );
	fetchParametersFromArgv( parameters, argc, argv );

	if ( ! checkParameters( parameters ) ) {
		printUsage();
		return EXIT_FAILURE;
	}

	printf( "Regresstor: calling web service %s(), %s at %s.\n",
		Regresstor_SubmitCheckMethod,
		Regresstor_URN,
		parameters[URL_Pos]);

	/* SOAP CALL */
	error = soap_client_init_args( argc, argv );
	if ( error != H_OK ) {
		log_error4( "%s():%s [%d]", herror_func(error), herror_message(error), herror_code(error) );
		herror_release(error);
		return EXIT_FAILURE;
	}
	
	error = soap_ctx_new_with_method( Regresstor_URN, Regresstor_SubmitCheckMethod, &request );
	if ( error != H_OK ) {
		log_error4( "%s():%s [%d]", herror_func(error), herror_message(error), herror_code(error) );
		herror_release(error);
		return EXIT_FAILURE;
	}

	/* Add parameters into envelope except for the URL */
	soap_env_push_item( request->env, NULL, "parameters" );

	for ( ii = 0; ii < URL_Pos; ++ii ) {
		if ( ii == OutputLocation_Pos ) {
			/* Attach file */
			char* contents;
			char* paramName = NULL;
			int sendDummyFile = 1;

			paramName = "Output";

			if ( strcmp( parameters[ii], "none" ) != 0 ) {
				contents = getFileContents( parameters[ii] );
				if ( contents != NULL ) {
					soap_env_add_item( request->env, "xsd:string", paramName, contents );
					free( contents );
					sendDummyFile = 0;
				}
			}
			if ( sendDummyFile ) {
				soap_env_add_item( request->env, "xsd:string", paramName, "No output file" );
			}
		}
		else
		{
			soap_env_add_item( request->env, "xsd:string", parameterNames[ii], parameters[ii] );
		}
	}

	/* Check commandline to see if diff passed. If fail, add a "Diff Failed" sub test */
	if ( strcmp( parameters[Passed_Pos], "0" ) == 0 ) {
		/* Add the sub test */
		soap_env_add_item( request->env, "xsd:string", "FailedSubTests", "Error failure" );
	}
	
	soap_env_pop_item( request->env );
	
	error = soap_client_invoke( request, &response, parameters[URL_Pos], "");
                                                                                                                                    
	if ( error != H_OK ) {
		log_error4( "[%d] %s(): %s ", herror_code(error), herror_func(error), herror_message(error) );
		herror_release( error );
		soap_ctx_free( request );
		return EXIT_FAILURE;
	}

	interpretResponse( response, &buildID );

	soap_ctx_free( request );
	soap_ctx_free( response );

	soap_client_destroy();


	return EXIT_SUCCESS;
}