📄 command.c
字号:
/*************************************************************************** command.c - A fifo-interface to the channel-server ------------------- begin : 2003 authors : Linus Gasser emails : linus.gasser@epfl.ch ***************************************************************************//*************************************************************************** Changes ------- date - name - description 03/01/20 - ineiti - begin **************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * It opens two FIFOs: one for commands, and one for results. They * are called "cmd" and "res" respectivley. The commands you can send: * * - STATS - returns # of channels */#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <pthread.h>#include "command.h"#include "server.h"#include "system.h"#include "debugging.h"#define DBG_LVL 3pthread_t thr_getFifo;void *getFifo( void *arg ) { FILE *cmd, *result; char *str; int ch, i, j, nbr; mkfifo( "cmd", S_IRWXU ); mkfifo( "result", S_IRWXU ); if ( ( cmd = fopen( "cmd", "r" ) ) < 0 ) { PR_DBG( 0, "Couldn't open cmd\n" ); return NULL; } PR_DBG( 4, "Opened cmd\n" ); if ( ( result = fopen( "result", "w" ) ) < 0 ) { PR_DBG( 0, "Couldn't open result\n" ); fclose( cmd ); return NULL; } PR_DBG( 4, "Opened result\n" ); while (1) { if ( ( fscanf( cmd, "%as\n", &str ) > 0 ) ) { PR_DBG( 4, "Got CMD: %s\n", str ); if ( !strcmp( str, "STATS" ) ) { ch = ( nbr_clients > 1 ) ? nbr_clients - 1 : 0; PR_DBG( 4, "STATS: %i %i\n", ch, CHANNEL_LENGTH ); fprintf( result, "%i %i\n", ch, CHANNEL_LENGTH ); } else if ( !strcmp( str, "CHANNEL" ) ) { fscanf( cmd, "%i %i", &ch, &nbr ); PR_DBG( 4, "CHANNEL %i, %i: ", ch, nbr ); for ( i=0; i<nbr; i++ ) { for( j = 0; j < nbr; j++){ fscanf( cmd, "%lf", &channel_tap[ ch ][ i ][ j ] ); PR_DBG_CL( 4, "%f ", channel_tap[ ch ][ i ][ j ] ); } } PR_DBG_CL( 4, "\n" ); fprintf( result, "OK\n" ); } else { PR_DBG( 0, "Error, couldn't recoginze CMD %s", str ); } } else { fclose( cmd ); fclose( result ); PR_DBG( 4, "Fifo closed. Re-opening it\n" ); cmd = fopen( "cmd", "r" ); PR_DBG( 4, "ReOpened cmd\n" ); result = fopen( "result", "w" ); PR_DBG( 4, "ReOpened result\n" ); } } fclose( cmd ); fclose( result );}int init_command() { pthread_create( &thr_getFifo, NULL, getFifo, NULL ); return 0;}void exit_command() { pthread_cancel( thr_getFifo );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -