summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <colomar.6.4.3@gmail.com>2020-02-29 19:20:57 +0100
committerAlejandro Colomar <colomar.6.4.3@gmail.com>2020-02-29 19:20:57 +0100
commit9372a21f98a57a5a0bccb964fa88b2585c0fcd03 (patch)
treeabe5c946c07b0bce95fe9c0d49261cd080c17afd
parent54d32951da94e11e5e6fe76917b052a462326e8e (diff)
getenvrob_0.3.0
-rw-r--r--rob/rob.c101
1 files changed, 82 insertions, 19 deletions
diff --git a/rob/rob.c b/rob/rob.c
index 3f38848..a4eef10 100644
--- a/rob/rob.c
+++ b/rob/rob.c
@@ -19,22 +19,23 @@
#include <libalx/base/errno/error.h>
#include <libalx/base/socket/tcp/server.h>
#include <libalx/base/stdio/printf/sbprintf.h>
+#include <libalx/base/stdlib/getenv/getenv_i.h>
+#include <libalx/base/stdlib/getenv/getenv_s.h>
+#include <libalx/base/sys/types.h>
#include "libalx/extra/telnet-tcp/client/client.h"
/******************************************************************************
******* define ***************************************************************
******************************************************************************/
-#define ROBOT_ADDR "robot"
-#define ROBOT_PORT "23"
-#define ROBOT_USER "user"
-#define ROBOT_PASSWD ""
-
-#define ROB_PORT "13100"
-#define CLIENTS_MAX (50)
-
-#define DELAY_LOGIN (1000 * 1000)
-#define DELAY_US (10 * 1000)
+#define ENV_ROBOT_ADDR "ROBOT_ADDR"
+#define ENV_ROBOT_PORT "ROBOT_PORT"
+#define ENV_ROBOT_USER "ROBOT_USER"
+#define ENV_ROBOT_PASSWD "ROBOT_PASSWD"
+#define ENV_ROB_PORT "ROB_PORT"
+#define ENV_ROB_CAMS_MAX "ROB_CAMS_MAX"
+#define ENV_DELAY_LOGIN "DELAY_LOGIN"
+#define ENV_DELAY_US "DELAY_US"
/******************************************************************************
@@ -48,8 +49,27 @@
/******************************************************************************
+ ******* static variables *****************************************************
+ ******************************************************************************/
+/* environment variables */
+static char robot_addr[_POSIX_ARG_MAX];
+static char robot_port[_POSIX_ARG_MAX];
+static char robot_user[_POSIX_ARG_MAX];
+static char robot_passwd[_POSIX_ARG_MAX];
+static char rob_port[_POSIX_ARG_MAX];
+static int rob_cams_max;
+static int delay_login;
+static int delay_us;
+/* pid */
+static pid_t pid;
+
+
+/******************************************************************************
******* static functions (prototypes) ****************************************
******************************************************************************/
+static
+int init_env (void);
+static
void cam_session (int cam, FILE *telnet);
@@ -65,17 +85,23 @@ int main (void)
socklen_t cam_addr_len;
int status;
- status = EXIT_FAILURE;
-
- if (telnet_open_client(&telnet, ROBOT_ADDR, ROBOT_PORT, "w"))
+ pid = getpid();
+ status = 1;
+ if (init_env())
+ goto out0;
+ status++;
+ if (telnet_open_client(&telnet, robot_addr, robot_port, "w"))
goto out0;
- if (telnet_login(telnet, ROBOT_USER, ROBOT_PASSWD, DELAY_LOGIN))
+ status++;
+ if (telnet_login(telnet, robot_user, robot_passwd, delay_login))
goto out1;
- tcp = tcp_server_open(ROB_PORT, CLIENTS_MAX);
+ status++;
+ tcp = tcp_server_open(rob_port, rob_cams_max);
if (tcp < 0)
goto out1;
+ status++;
if (telnet_send(telnet, "ls -la"))
goto out;
if (telnet_send(telnet, "date"))
@@ -83,11 +109,12 @@ int main (void)
if (telnet_send(telnet, "stat ."))
goto out;
+ status++;
cam_addr_len = sizeof(cam_addr);
while (true) {
cam = accept(tcp, (struct sockaddr *)&cam_addr, &cam_addr_len);
if (cam < 0) {
- usleep(DELAY_US);
+ usleep(delay_us);
continue;
}
@@ -98,11 +125,13 @@ int main (void)
status = EXIT_SUCCESS;
out:
if (close(tcp))
- status = EXIT_FAILURE;
+ status += 32;
out1:
if (pclose(telnet))
- status = EXIT_FAILURE;
+ status += 64;
out0:
+ fprintf(stderr, "rob#%"PRIpid": ERROR\n", pid);
+ perrorx(NULL);
return status;
}
@@ -111,6 +140,40 @@ out0:
/******************************************************************************
******* static functions (definitions) ***************************************
******************************************************************************/
+static
+int init_env (void)
+{
+ int status;
+
+ status = -1;
+ if (getenv_s(robot_addr, ARRAY_SIZE(robot_addr), ENV_ROBOT_ADDR))
+ return status;
+ status--;
+ if (getenv_s(robot_port, ARRAY_SIZE(robot_port), ENV_ROBOT_PORT))
+ return status;
+ status--;
+ if (getenv_s(robot_user, ARRAY_SIZE(robot_user), ENV_ROBOT_USER))
+ return status;
+ status--;
+ if (getenv_s(robot_passwd, ARRAY_SIZE(robot_passwd), ENV_ROBOT_PASSWD))
+ return status;
+ status--;
+ if (getenv_s(rob_port, ARRAY_SIZE(rob_port), ENV_ROB_PORT))
+ return status;
+ status--;
+ if (getenv_i32(&rob_cams_max, ENV_ROB_CAMS_MAX))
+ return status;
+ status--;
+ if (getenv_i32(&delay_login, ENV_DELAY_LOGIN))
+ return status;
+ status--;
+ if (getenv_i32(&delay_us, ENV_DELAY_US))
+ return status;
+
+ return 0;
+}
+
+static
void cam_session (int cam, FILE *telnet)
{
static int i = 0;
@@ -127,7 +190,7 @@ void cam_session (int cam, FILE *telnet)
return;
if (telnet_send(telnet, buf))
return;
- usleep(DELAY_US);
+ usleep(delay_us);
}
}