/******************************************************************************
  Write data that will be read by other program
******************************************************************************/
int write_cosimulation_data(PARA_DATA *para, REAL **var)
{
  int i;
  int imax = para->geom->imax, jmax = para->geom->jmax;
  int kmax = para->geom->kmax;
  int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);

  ffdSharedData data;


  /*--------------------------------------------------------------------------
  | The following code is to be modified by the users
  --------------------------------------------------------------------------*/
  for(i=0; i<3; i++)
    data.number[i] = var[VX][IX(1,1,1)] + i;

  data.status = 1;
  data.t = para->mytime->t;

  strcpy(data.message, "This is FFD data\0");

  if(write_to_shared_memory(&data))
    exit(1);
  else
    ffd_log("cosimulation.c: write data to shared memory.", FFD_NORMAL);

  return 0;
} // End of write_cosimulation_data()
Beispiel #2
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;
}