Example #1
0
void unit_end(unit_table_t *unit)
{
		struct link_unit_t *ut;
		char buf[10240];

		if(unit->valid == true || HASH_COUNT(unit->link_unit) > 1) {
				bzero(buf, 10240);
				// emit linked unit lists;
				if(unit->link_unit != NULL) {
						sprintf(buf, "type=unit list=\"");
						for(ut=unit->link_unit; ut != NULL; ut=ut->hh.next) {
								sprintf(buf+strlen(buf), "%d-%d,", ut->id.tid, ut->id.unitid);
						}
						sprintf(buf+strlen(buf), "\" tid=%d \n", unit->tid);
						emit_log(unit, buf);
				}
		}

		delete_unit_hash(unit->link_unit, unit->mem_unit);
		//if(unit->link_unit != NULL) printf("link_unit is not NULL %p\n", unit->link_unit);
		unit->link_unit = NULL;
		unit->mem_unit = NULL;
		unit->valid = false;
		unit->r_addr = 0;
		unit->w_addr = 0;
		unit->unitid++;
}
Example #2
0
JNIEXPORT jboolean JNICALL jni_connectLws(JNIEnv *env, jobject obj)
{
  struct lws_client_connect_info info_ws;
  memset(&info_ws, 0, sizeof(info_ws));

  info_ws.port = port;
  info_ws.address = address;
  info_ws.path = "/";
  info_ws.context = context;
  info_ws.ssl_connection = use_ssl;
  info_ws.host = address;
  info_ws.origin = address;
  info_ws.ietf_version_or_minus_one = -1;
  info_ws.client_exts = exts;
  info_ws.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;

  // connect
  wsi = lws_client_connect_via_info(&info_ws);
  if(wsi == NULL ){
    // Error
    emit_log(LLL_ERR, "Protocol failed to connect.");
    return JNI_FALSE;
  }

  return JNI_TRUE;
}
Example #3
0
static int callback(
  struct lws *wsi,
  enum lws_callback_reasons reason,
  void *user,
  void *in,
  size_t len
)
{
  switch(reason){

    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
      sendMessage(MSG_LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL);
      break;

    case LWS_CALLBACK_CLIENT_ESTABLISHED:
      sendMessage(MSG_LWS_CALLBACK_CLIENT_ESTABLISHED, NULL);
      break;

    case LWS_CALLBACK_CLIENT_RECEIVE:
        ((char *)in)[len] = '\0';
        sendMessage(MSG_DUMB_INCREMENT_PROTOCOL_COUNTER, gEnv->NewStringUTF((const char*)in));
        break;

    case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
      if ((strcmp((const char*)in, "deflate-stream") == 0) && deny_deflate) {
        emit_log(LLL_ERR, "websocket: denied deflate-stream extension");
        return 1;
      }
      if ((strcmp((const char*)in, "deflate-frame") == 0) && deny_deflate) {
        emit_log(LLL_ERR, "websocket: denied deflate-frame extension");
        return 1;
      }
      if ((strcmp((const char*)in, "x-google-mux") == 0) && deny_mux) {
        emit_log(LLL_ERR, "websocket: denied x-google-mux extension");
        return 1;
      }
      break;

    default:
      break;
  }

  return 0;
}
Example #4
0
JNIEXPORT jboolean JNICALL jni_initLws(JNIEnv *env, jobject obj)
{
    if(context) return JNI_TRUE;

    // Attach the java virtual machine to this thread
    gJvm->AttachCurrentThread(&gEnv, NULL);

    // Set java global references to the class and object
    jclass cls = env->GetObjectClass(obj);
    gLwsServiceCls = (jclass) env->NewGlobalRef(cls);
    gLwsServiceObj = env->NewGlobalRef(obj);

    // Get the sendMessage method from the LwsService class (inherited from class ThreadService)
    sendMessageId = gEnv->GetMethodID(gLwsServiceCls, "sendMessage", "(ILjava/lang/Object;)V");

    memset(&info, 0, sizeof(info));
    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;
#if !defined(LWS_WITHOUT_EXTENSIONS)
    info.extensions = exts;
#endif
    info.gid = -1;
    info.uid = -1;

    lws_set_log_level( LLL_NOTICE | LLL_INFO | LLL_ERR | LLL_WARN | LLL_CLIENT, emit_log );

    context = lws_create_context(&info);
    if( context == NULL ){
        emit_log(LLL_ERR, "Creating libwebsocket context failed");
        return JNI_FALSE;
    }

    isExit = 0;

    return JNI_TRUE;
}
int main(int argc, char **argv) {
  int status, scene, next_scene;
  char errmsg[128];
  long elapsed_msec, wait_msec;
  struct timespec wait_time, left_time;
  struct timeval frame_start_time, frame_end_time;
  WINDOW *window;
  struct invaders_game game;
  struct logger error_logger;

  UNUSED(argc);
  UNUSED(argv);

  /* Initialize for ncurses library */
  reset_logger(&error_logger, ERRORLOG_FILEPATH);
  status = 1;
  window = initscr();
  if (ERR == wresize(window, CANVAS_SIZE_X, CANVAS_SIZE_Y)) {
    emit_log(&error_logger,
             "Failed to change the ncurses setting for window size");
    goto cleanup;
  }
  if (ERR == keypad(window, true)) {
    emit_log(&error_logger,
             "Failed to change the ncurses setting for key input receiving");
    goto cleanup;
  }
  if (ERR == noecho()) {
    emit_log(&error_logger,
             "Failed to change the ncurses setting for echoing setting");
    goto cleanup;
  }
  curs_set(0);
  timeout(0);
  if (has_colors() && can_change_color()) {
    if (ERR == start_color()) {
      emit_log(&error_logger,
               "Failed to change the ncurses setting to set up coloring");
      goto cleanup;
    }
    if ((ERR == init_pair(PLAYER_JET_COLOR_PAIR, PLAYER_JET_COLOR, COLOR_BLACK))
        || (ERR == init_pair(PLAYER_BULLET_COLOR_PAIR, PLAYER_BULLET_COLOR,
        COLOR_BLACK))
        || (ERR == init_pair(TOCHCA_COLOR_PAIR, TOCHCA_COLOR, COLOR_BLACK))
        || (ERR == init_pair(COMMANDER_INVADER_COLOR_PAIR, COMMANDER_INVADER_COLOR,
            COLOR_BLACK))
        || (ERR == init_pair(SENIOR_INVADER_COLOR_PAIR, SENIOR_INVADER_COLOR,
        COLOR_BLACK))
        || (ERR == init_pair(YOUNG_INVADER_COLOR_PAIR, YOUNG_INVADER_COLOR,
        COLOR_BLACK))
        || (ERR == init_pair(LOOKIE_INVADER_COLOR_PAIR, LOOKIE_INVADER_COLOR,
        COLOR_BLACK))
        || (ERR == init_pair(INVADER_BULLET_COLOR_PAIR, INVADER_BULLET_COLOR,
        COLOR_BLACK))
        || (ERR == init_pair(TITLE_COLOR_PAIR, TITLE_COLOR, COLOR_BLACK))
        || (ERR == init_pair(EVENT_CAPTION_COLOR_PAIR, EVENT_CAPTION_COLOR,
        COLOR_BLACK))
        || (ERR == init_pair(SCORE_COLOR_PAIR, SCORE_COLOR, COLOR_BLACK))
        || (ERR == init_pair(CREDIT_COLOR_PAIR, CREDIT_COLOR, COLOR_BLACK))
        || (ERR
            == init_pair(CANVAS_FRAME_COLOR_PAIR, CANVAS_FRAME_COLOR, COLOR_BLACK))) {
      emit_log(&error_logger,
               "Failed to change the ncurses setting to define color pair");
      goto cleanup;
    }

  }

  /* Execute game loop */
  scene = -1;
  next_scene = TITLE_SCENE;
  while (1) {
    /* Record the frame starting time */
    if (0 != gettimeofday(&frame_start_time, NULL)) {
      emit_log(&error_logger, "Failed to get time of frame starting");
      goto cleanup;
    }

    /* Change the next scene if needed */
    if (scene != next_scene) {
      scene = next_scene;
      if (INGAME_SCENE == scene) {
        reset_game(&game);
      }
    }

    /* Update the objects */
    if (TITLE_SCENE == scene) {
      update_game_on_title_scene(&next_scene);
    } else if (INGAME_SCENE == scene) {
      update_game_on_ingame_scene(&game, IDEAL_FRAME_TIME, &next_scene);
    }

    /* Render the objects */
    erase();
    if (TITLE_SCENE == scene) {
      draw_title_scene();
    } else if (INGAME_SCENE == scene) {
      draw_ingame_scene(&game);
    }
    draw_canvas_frame();
    refresh();

    /* Adjust the frame interval */
    if (0 != gettimeofday(&frame_end_time, NULL)) {
      emit_log(&error_logger, "Failed to get time of frame finished");
      goto cleanup;
    }
    elapsed_msec = (frame_end_time.tv_sec - frame_start_time.tv_sec) * 1000L
        + (frame_end_time.tv_usec - frame_start_time.tv_usec) / 1000000L;
    wait_msec = IDEAL_FRAME_TIME - elapsed_msec;
    if (0L < wait_msec) {
      wait_time.tv_sec = 0;
      wait_time.tv_nsec = wait_msec * 1000000L;
      while (-1 == nanosleep(&wait_time, &left_time)) {
        if (EINTR == errno) {
          memcpy(&wait_time, &left_time, sizeof(struct timespec));
        } else {
          if (0 == strerror_r(errno, errmsg, sizeof(errmsg))) {
            emit_log(
                &error_logger,
                "Failed to sleep until the end of frame: errmsg=%s, sleeping_msec=%ld",
                errmsg, wait_msec);
          } else {
            emit_log(&error_logger, "Failed to get error message: errno=%d",
            errno);
          }
          goto cleanup;
        }
      }
    }
  }
  status = 0;

 cleanup:
  endwin();
  close_logger(&error_logger);
  return status;
}
Example #6
0
static void emit_int(struct lio *lio, char *key, int n)
{
    char buf0[64] = {};
    sprintf(buf0, "%d", n);
    emit_log(lio, "TraceID", buf0, "tid", key, "File", __FILE__);
}