Ejemplo n.º 1
0
void client_loop(void *user_data, void *sthread) {

	void *sclient = client_loop_init(sthread);

	if ( sclient == NULL ) {
		st_app_terminate = 1;
		return;
	}

	if ( user_data )
		*(void**)user_data = sclient;

	while(sthread_isterminated(sthread) == 0) {

		supla_log(LOG_INFO, "Connecting...");

		if ( 0 == supla_client_connect(sclient) ) {
			usleep(2000000);
		} else {

			while( sthread_isterminated(sthread) == 0
				   && supla_client_iterate(sclient, 10000000) == 1 ) {
			}

		}
	}

	if ( user_data )
		*(void**)user_data =NULL;


	supla_client_free(sclient);


}
Ejemplo n.º 2
0
void ipc_accept_loop(void *ipc, void *ipc_al_sthread) {

	int client_sd;
	void *ipcctrl_thread_arr = safe_array_init();

	while(sthread_isterminated(ipc_al_sthread) == 0) {

		safe_array_clean(ipcctrl_thread_arr, accept_loop_ipcctrl_thread_cnd);

		if ( -1 == (client_sd = ipcsocket_accept(ipc)) ) {
			break;
		} else {

			Tsthread_params stp;

			stp.execute = accept_loop_ipcctrl_execute;
			stp.finish = accept_loop_ipcctrl_finish;
			stp.user_data = new svr_ipcctrl(client_sd);
			stp.free_on_finish = 0;
			stp.initialize = NULL;

			safe_array_add(ipcctrl_thread_arr, sthread_run(&stp));

		}
	}

	safe_array_clean(ipcctrl_thread_arr, accept_loop_ipcctrl_thread_twt);
	safe_array_free(ipcctrl_thread_arr);

}
Ejemplo n.º 3
0
void s_worker::execute(void *sthread) {
  if (!db->connect()) return;

  s_exec = q->get_job();

  while (s_exec.id && !sthread_isterminated(sthread)) {
    if (db->set_fetched(s_exec.id)) q->mark_fetched();

    s_worker_action *action =
        AbstractActionFactory::createByActionType(s_exec.action, this);

    if (action) {
      action->execute();
      delete action;
    } else {
      db->set_result(s_exec.id, ACTION_EXECUTION_RESULT_CANCELLED);
      supla_log(LOG_ERR, "Action %i is not supported!", s_exec.action);
    }

    if (s_exec.action_param != NULL) free(s_exec.action_param);

    s_exec = q->get_job();
  }

  q->raise_loop_event();
}
Ejemplo n.º 4
0
void datalogger_loop(void *ssd, void *dl_sthread) {
  supla_datalogger *logger = new supla_datalogger();
  database::thread_init();

  while (sthread_isterminated(dl_sthread) == 0) {
    logger->log();
    usleep(1000000);
  }

  delete logger;
  database::thread_end();
}
Ejemplo n.º 5
0
void accept_loop(void *ssd, void *al_sthread) {

	void *supla_socket = NULL;
	void *svrconn_thread_arr = safe_array_init();


	while(sthread_isterminated(al_sthread) == 0) {

		safe_array_clean(svrconn_thread_arr, accept_loop_srvconn_thread_cnd);

		unsigned int ipv4;

		if ( ssocket_accept(ssd, &ipv4, &supla_socket) == 0  ) {
			break;
		} else {

			if ( supla_socket != NULL ) {

				Tsthread_params stp;

				stp.execute = accept_loop_srvconn_execute;
				stp.finish = accept_loop_srvconn_finish;
				stp.user_data = new serverconnection(ssd, supla_socket, ipv4);
				stp.free_on_finish = 0;
				stp.initialize = NULL;

				safe_array_add(svrconn_thread_arr, sthread_run(&stp));
			}

		}

	}

	safe_array_clean(svrconn_thread_arr, accept_loop_srvconn_thread_twt);
	safe_array_free(svrconn_thread_arr);
}
Ejemplo n.º 6
0
void svr_ipcctrl::execute(void *sthread) {

	if ( sfd == -1 )
		return;

	int len;

	struct timeval last_action;
	gettimeofday(&last_action, NULL);

	send(sfd, hello, sizeof(hello), 0);

	while(sthread_isterminated(sthread) == 0) {
		eh_wait(eh, 1000000);

		if ( (len = recv(sfd, buffer, sizeof(buffer), 0)) != 0 ) {

			if ( len > 0 ) {

				buffer[255] = 0;

				if ( match_command(cmd_is_iodev_connected, len) ) {

					int UserID = 0;
					int DeviceID = 0;
					sscanf (&buffer[strlen(cmd_is_iodev_connected)], "%i,%i", &UserID, &DeviceID);

					if ( UserID
						 && DeviceID
						 && supla_user::is_device_online(UserID, DeviceID) ) {
						send_result("CONNECTED:",DeviceID);
					} else {
						send_result("DISCONNECTED:",DeviceID);
					}
				} else if ( match_command(cmd_user_reconnect, len) ) {

					int UserID = 0;
					sscanf (&buffer[strlen(cmd_user_reconnect)], "%i", &UserID);

					if ( UserID
						 && supla_user::reconnect(UserID) ) {
						send_result("OK:", UserID);
					} else {
						send_result("USER_UNKNOWN:", UserID);
					}
				} else if ( match_command(cmd_get_double_value, len) ) {

					int UserID = 0;
					int DeviceID = 0;
					int ChannelID = 0;
					double Value;

					sscanf (&buffer[strlen(cmd_get_double_value)], "%i,%i,%i", &UserID, &DeviceID, &ChannelID);

					if ( UserID
						 && DeviceID
						 && ChannelID
						 && supla_user::get_channel_double_value(UserID, DeviceID, ChannelID, &Value) ) {

						send_result("VALUE:", Value);

					} else {
						send_result("UNKNOWN:", ChannelID);
					}
				}


			}

		} else {
			sthread_terminate(sthread);
		}


		struct timeval now;
		gettimeofday(&now, NULL);

		if ( now.tv_sec-last_action.tv_sec >= 5 ) {
			sthread_terminate(sthread);
			break;
		}
	};

}
Ejemplo n.º 7
0
void svr_ipcctrl::execute(void *sthread) {
  if (sfd == -1) return;

  int len;

  struct timeval last_action;
  gettimeofday(&last_action, NULL);

  send(sfd, hello, sizeof(hello), 0);

  while (sthread_isterminated(sthread) == 0) {
    eh_wait(eh, 1000000);

    if ((len = recv(sfd, buffer, sizeof(buffer), 0)) != 0) {
      if (len > 0) {
        buffer[255] = 0;

        if (match_command(cmd_channel_get_hivalue, len)) {
          char hi;
          int number = 255;
          sscanf(&buffer[strlen(cmd_channel_get_hivalue)], "%i", &number);

          if (channelio_get_hi_value(number, &hi) == 1) {
            send_result("HIVALUE:", hi == 1 ? 1 : 0);
          } else {
            send_result("ERR", number);
          }

        } else if (match_command(cmd_channel_get_type, len)) {
          int number = 255;
          sscanf(&buffer[strlen(cmd_channel_get_type)], "%i", &number);
          send_result("TYPE:", channelio_get_type(number));

        } else if (match_command(cmd_channel_set_hivalue, len)) {
          int hi = 0;
          int number = 255;
          unsigned int timems = 0;

          sscanf(&buffer[strlen(cmd_channel_set_hivalue)], "%i,%i,%i", &number,
                 &hi, &timems);

          if (channelio_set_hi_value(number, hi == 1 ? 1 : 0, timems) == 1) {
            send_result("OK");
          } else {
            send_result("ERR", number);
          }
        }
      }

    } else {
      sthread_terminate(sthread);
    }

    struct timeval now;
    gettimeofday(&now, NULL);

    if (now.tv_sec - last_action.tv_sec >= 5) {
      sthread_terminate(sthread);
      break;
    }
  }
}