Hitachi

JP1 Version 12 JP1/Base 関数リファレンス


付録B.1 サンプルソースファイルの詳細

サンプルソースファイルの詳細について説明します。

〈この項の構成〉

(1) サンプルソースファイルで扱うイベント

表B‒1 サンプルソースファイルで扱うイベント

属性の種別

項目

属性名

内容

基本属性

イベントID

0x00000001

メッセージ

「Starts the SAMPLE application.」

拡張属性(共通情報)

重大度

SEVERITY

Notice

ユーザー名

USER_NAME

アプリケーション実行ユーザー名

プロダクト名

PRODUCT_NAME

/COMPANY/APP1/SAMPLE_PRODUCT(プロダクトの名称)

オブジェクトタイプ

OBJECT_TYPE

SAMPLE

オブジェクト名

OBJECT_NAME

SAMPLE_NAME

登録名タイプ

ROOT_OBJECT_TYPE

ROOT_SAMPLE

登録名

ROOT_OBJECT_NAME

ROOT_SAMPLE_NAME

オブジェクトID

OBJECT_ID

SAMPLE_ID

事象種別

OCCURRENCE

START

開始時刻

START_TIME

SAMPLEアプリケーションの開始時刻。UTC 1970年1月1日00:00:00からの秒数。

プラットフォーム

PLATFORM

NT

バージョン情報

ACTION_VERSION

0600

拡張属性(固有情報)

SAMPLE共通属性1

COMMON_ATTR1

NATIVE

SAMPLE共通属性2

COMMON_ATTR2

TRUE

SAMPLE開始属性1

START_ATTR1

SAMPLE1

SAMPLE開始属性2

START_ATTR2

SAMPLE2

(2) サンプルソースファイルのコーディング内容

(a) sender.cのコーディング内容

#include <stdio.h>
#include <time.h>
#include "JevApi.h"
 
int regist_start_event()
{
      int rc;                    /* Return code */
      long status = 0;           /* Detailed error code */
      const char* server;        /* Event server name */
      long baseID;               /* Event ID */
      const char* message;       /* Message */
      char starttime[32];
      const char* extattrs[16];  /* Array for storing extended attributes */
 
      /* Set the destination event server name. */
      server = NULL;
 
      /* Set the event ID. */
      baseID = 0x00000001;
 
      /* Set the message. */
      message = "Starts the SAMPLE application.";
 
      /* Set the extended attributes. */
      extattrs[0]  = "SEVERITY=Notice";
      extattrs[1]  = "USER_NAME=SAMPLE_USER";
      extattrs[2]  = "PRODUCT_NAME=/COMPANY/APP1/SAMPLE_PRODUCT";
      extattrs[3]  = "OBJECT_TYPE=SAMPLE";
      extattrs[4]  = "OBJECT_NAME=SAMPLE_NAME";
      extattrs[5]  = "OBJECT_ROOT_TYPE=ROOT_SAMPLE";
      extattrs[6]  = "OBJECT_ROOT_NAME=ROOT_SAMPLE_NAME";
      extattrs[7]  = "OBJECT_ID=SAMPLE_ID";
      extattrs[8]  = "OCCURRENCE=START";
      sprintf(starttime, "START_TIME=%ld", time(NULL));
      extattrs[9]  = starttime;
      extattrs[10] = "PLATFORM=NT";
      extattrs[11] = "VERSION=0600";
      extattrs[12] = "COMMON_ATTR1=NATIVE";
      extattrs[13] = "COMMON_ATTR2=TRUE";
      extattrs[14] = "START_ATTR1=SAMPLE1";
      extattrs[15] = "START_ATTR2=SAMPLE2";
 
      /* Register the JP1 event. */
      rc = JevRegistEvent(&status,
                          server,
                          baseID,
                          message,
                          extattrs,
                          16);
      if(rc < 0) {
          fprintf(stderr,
                  "JevRegistEvent() failed. status = %ld\n",
                   status);
          return -1;
      }
 
      return 0;
}
 
int main()
{
      return regist_start_event();
}

(b) receiver.cのコーディング内容

#include <stdio.h>
#include <string.h>
#include "JevApi.h"
 
int get_start_event()
{
      int rc;               /* Return code */
      long position;        /* Sequence number within the event database */
      long status;          /* Status code address */
      char filter[256];     /* Filter statement buffer */
      const char *server;   /* Event server name */
      const char *message;  /* Pointer to the message */
      const char *name;     /* Pointer to the extended attribute name */
      const char *value;    /* Pointer to the extended attribute value */
      JEVGETKEY key;        /* Handle for acquiring JP1 events */
      JP1EVENT event;       /* Handle for accessing JP1 events */
      JEVACCESSTYPE access; /* Action when no JP1 event exists */
 
      /* Set the filter statement to acquire JP1 events. */
      strcpy(filter, "B.ID IN 00000001\n");
      strcat(filter, "E.SEVERITY IN Notice\n");
      strcat(filter, 
             "E.PRODUCT_NAME IN /COMPANY/APP1/SAMPLE_PRODUCT");
 
      /* Connect to the event server of the physical host. */
      status = 0;
      /* Event server of the physical host to connect to */
      server = NULL;
      /* Acquisition starts with sequence number 0 within the event database. */
      position = 0;
      key = JevGetOpen(&status, server, filter, position);
      if(key == NULL){
          fprintf(stderr,
                  "JevGetOpen() failed. Status = %ld\n",
                  status);
          return -1;
      }
 
      /* Acquire all the JP1 events which match the filter condition. */
      while(1) {
          status = 0;
          /* Error return when no JP1 event matches the filter condition */
          access = JEVGET_NOWAIT;
          event = JevGetEvent(&status, key, access);
          if(event == NULL){
              if(status == JEV_S_NO_EVENT) {
                  /* No more JP1 event matches the filter condition. */
                  break;
              }
              else {
                  /* Error occurred while acquiring JP1 events. */
                  fprintf(stderr,
                          "JevGetEvent() failed. Status = %ld\n",
                          status);
                  JevGetClose(&status, key);
                  return -1;
              }
          }
 
          /* Acquire a message. */
          status = 0;
          rc = JevGetMessage(&status, event, &message);
          if(rc < 0){
              fprintf(stderr,
                      "JevGetMessage() failed. Status = %ld\n",
                      status);
              JevFreeEvent(&status, event);
              JevGetClose(&status, key);
              return -1;
          }
          else{
              printf("JevGetMessage() message = %s\n", message);
          }
 
          /* Acquire the (first) extended attribute. */
          status = 0;
          rc = JevGetFirstExtAttr(&status, event, &name, &value);
          if(rc < 0){
              fprintf(stderr,
                      "JevGetFirstExtAttr() failed. Status = %ld\n",
                      status);
              JevFreeEvent(&status, event);
              JevGetClose(&status, key);
              return -1;
          }
          else{
              printf("JevGetFirstExtAttr() name = %s\n", name);
              printf("JevGetFirstExtAttr() value = %s\n", value);
          }
 
          /* Acquire the (subsequent) extended attribute. */
          while(1) {
              status = 0;
              rc = JevGetNextExtAttr(&status, event, &name, &value);
              if(rc < 0 ){
                  if(status == JEV_S_EXTATTR_EOD) {
                  /* No more extended attribute exists. */
                      break;
                  }
                  else {
                      /* Error occurred while acquiring extended attributes. */
                      fprintf(stderr,
                              "JevGetNextExtAttr() failed. Status = %ld\n",
                              status);
                      JevFreeEvent(&status, event);
                      JevGetClose(&status, key);
                      return -1;
                  }
              }
              else {
                  printf("JevGetNextExtAttr() name = %s\n", name);
                  printf("JevGetNextExtAttr() value = %s\n", value);
              }
          }
 
          /* Release the memory allocated for the acquired JP1 events. */
          rc = JevFreeEvent(&status, event);
          if(rc < 0){
              fprintf(stderr,
                      "JevFreeEvent() failed. Status = %ld\n",
                      status);
              JevGetClose(&status, key);
              return -1;
          }
      }
 
      /* Disconnect the event server. */
      rc = JevGetClose(&status, key);
      if(rc < 0){
          fprintf(stderr,
                  "JevGetClose() failed. Status = %ld\n",
                  status);
          return -1;
      }
 
      return 0;
}
 
int main()
{
      return get_start_event();
}