add simplest terminal

This commit is contained in:
2026-08-11 10:15:20 +03:00
parent e7b345b377
commit 25d35e2294
7 changed files with 65 additions and 15 deletions

View File

@@ -328,18 +328,9 @@ static char *time_asc(double t){
}
*/
void clientproc(SSL_CTX *ctx, int fd){
FNAME();
SSL *ssl;
char buf[1024];
int bytes;
sdat.mode |= 0200; // allow W
sdat.atflag = 0; // clear SHM_RDONLY
if(!get_shm_block(&sdat, ClientSide)){
LOGERR("Can't get SHM block");
ERRX("Can't get SHM block");
}
ssl = SSL_new(ctx);
// open SSL connection for client
static SSL *openConn(SSL_CTX *ctx, int fd){
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);
int c = SSL_connect(ssl);
if(c < 0){
@@ -351,6 +342,21 @@ void clientproc(SSL_CTX *ctx, int fd){
LOGERR("Can't make socket nonblocking");
ERRX("ioctl()");
}
return ssl;
}
// run main client process
void clientproc(SSL_CTX *ctx, int fd){
FNAME();
char buf[1024];
SSL *ssl = openConn(ctx, fd);
if(!ssl) return;
sdat.mode |= 0200; // allow W
sdat.atflag = 0; // clear SHM_RDONLY
if(!get_shm_block(&sdat, ClientSide)){
LOGERR("Can't get SHM block");
ERRX("Can't get SHM block");
}
while(isrunning){
if(!process_system(ssl)){
LOGERR("Motors error");
@@ -360,7 +366,7 @@ void clientproc(SSL_CTX *ctx, int fd){
break;
}
// clear receiving buffer (TODO: parse it?)
bytes = SSL_nbread(ssl, buf, sizeof(buf));
int bytes = SSL_nbread(ssl, buf, sizeof(buf)-1);
if(bytes > 0){
buf[bytes] = 0;
fprintf(stderr, "Received: \"%s\"\n", buf);
@@ -373,3 +379,40 @@ void clientproc(SSL_CTX *ctx, int fd){
DBG("Exit; isrunning=%d", isrunning);
SSL_free(ssl);
}
// run in terminal mode
void terminal(SSL_CTX *ctx, int fd){
FNAME();
char buf[1024], *lptr = NULL;
size_t N = 0;
SSL *ssl = openConn(ctx, fd);
if(!ssl) return;
int printed = FALSE;
while(isrunning){
if(!printed){
printf("> ");
fflush(stdout);
printed = TRUE;
}
if(sl_canread(0)){
ssize_t L = getline(&lptr, &N, stdin);
if(L > (ssize_t)(sizeof(buf)-1)) WARNX("String too long!");
else if(L > 0){
SSL_write(ssl, lptr, L);
}
}
int bytes = 0;
while((bytes = SSL_nbread(ssl, buf, sizeof(buf)-1))){
if(bytes > 0){
buf[bytes] = 0;
printf("< %s\n", buf);
}else{
LOGWARN("Server disconnected or other error");
ERRX("Disconnected");
}
usleep(10000);
printed = FALSE;
}
}
SSL_free(ssl);
}

View File

@@ -21,3 +21,4 @@
#include "sslsock.h"
void clientproc(SSL_CTX *ctx, int fd);
void terminal(SSL_CTX *ctx, int fd);

View File

@@ -72,6 +72,7 @@ static sl_option_t cmdlnopts[] = {
{"server", NEED_ARG, NULL, 's', arg_string, APTR(&G.serverhost), _("server node, IP:port")},
{"lostsync",NEED_ARG, NULL, 'L', arg_double, APTR(&G.T_sync_lost),_("\"lost synchronization\" timeout, s (default: 5)")},
{"emulation",NO_ARGS, NULL, 'e', arg_int, APTR(&G.emulmode), _("run even in emulation mode")},
{"terminal",NO_ARGS, NULL, 'T', arg_int, APTR(&G.terminal), _("run client in terminal mode")},
#endif
end_option
};

View File

@@ -35,6 +35,7 @@
*/
typedef struct{
int emulmode; // emulation mode: don't check server & run in model mode
int terminal; // run client in terminal mode
double acc_timeout; // network timeout, s
double T_sync_lost; // "lost synchronization" timeout, s
double speedchk_interval;// interval of motors' speed checking (resend command if don't reach yet)

View File

@@ -72,6 +72,10 @@ int start_daemon(){
#ifdef CLIENT
//DBG("server: %s", G.serverhost);
if(!G.serverhost) ERRX("Point server name");
if(G.terminal){
isrunning = TRUE;
return open_socket();
}
#endif
if(G.logfile){
int lvl = LOGLEVEL_WARN + G.verbose;

View File

@@ -24,6 +24,5 @@
int main(int argc, char **argv){
sl_init();
parse_args(argc, argv);
DBG("START");
return start_daemon();
}

View File

@@ -139,7 +139,8 @@ int open_socket(){
#ifdef SERVER
serverproc(ctx, fd);
#else
clientproc(ctx, fd);
if(G.terminal) terminal(ctx, fd);
else clientproc(ctx, fd);
#endif
// newer reached
close(fd);