Сигналы. Сетевая доставка сигналов. Системный вызов. Отправка сигнала процессу на удаленном узле. Программа приема сигнала, страница 13

msg.ClientMsg.msg_buff );

}

break;

}

}

/* Поиск свободного агента */

int SearchFreeAgent( )

{

int agent_rcv_id = 0,

i=0;

for( ;i<agent_num; ++i )

{

if( agent_info[i].busy == 0 ) {

agent_rcv_id = agent_info[i].recv_id;

agent_info[i].busy = 1;

break;

}

}

return agent_rcv_id;

}

/* Освобождение агента */

void ReleaseAgent( int rcv_id )

{

int i=0;

for( ;i<agent_num; ++i )

{

if( agent_info[i].recv_id == rcv_id ) {

agent_info[i].busy = 0;

break;

}

}

}

Листинг 7.3. Программа агента. Файл reply_driven_agent.c.

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

#include <time.h>

#include <signal.h>

#include <signal.h>

#include <sys/dispatch.h>

#include "../reply_driven_server/defs.h"

void sigint_handler( int );

int          connect_id;

AgentMsg_T   send_msg;

AgentMsg_T   reply_msg;

FILE*        agent_log;

void         fprintTime( );

int main( int argc, char *argv[] )

{

if( argc < 2 )

{

printf( "Agent: cmd args error!\n" );

exit(1);

}

unsigned int flag;

if( strcmp(argv[1],"-l" ) == 0 )       flag = 0;

else if( strcmp(argv[1],"-g" ) == 0 )  flag = NAME_FLAG_ATTACH_GLOBAL;

else {

printf( "Agent: cmd args error!\n" );

exit(1);

}

char file_name[64];

sprintf( file_name, "agent_log_%d.txt", getpid( ) );

agent_log = fopen( file_name, "w" );

if( agent_log == NULL ) {

printf( "Agent: open log file error!\n" );

exit(2);

}

// Открытие канала //

connect_id = name_open( ATTACH_POINT, flag );

if( connect_id == -1 )

{

fprintf( agent_log, "Agent: name open error!\n" );

exit(2);

}

printf( "Agent: started. PID=%d.\n", getpid() );

fprintf( agent_log, "Agent: started. PID=%d.\n", getpid() );

send_msg.type = AGENT_START;

while(1)

{

// Отправлем сообщение сервера //

if( MsgSend( connect_id, &send_msg, sizeof(send_msg), &reply_msg, sizeof(reply_msg) ) == -1 ) {

fprintf( agent_log, "Agent: send msg error!\n" );

exit(3);

}

// Сообщение от сервера //

fprintTime();

fprintf( agent_log, "REPLY MSG:\n%s\n\n", reply_msg.msg_buff );

send_msg.type = AGENT_REPLY;

send_msg.client_rcvid = reply_msg.client_rcvid;

sprintf( send_msg.msg_buff, "Reply to the client RECVID=%d from agent PID=%d",

reply_msg.client_rcvid,

getpid( ) );

sleep( 2 );

fprintTime();

fprintf( agent_log, "SEND MSG:\n%s\n\n", send_msg.msg_buff );

}

return EXIT_SUCCESS;

}

void sigint_handler( int signo )

{

fprintf( agent_log, "Terminate.\n" );

fclose( agent_log );

name_close( connect_id );

}

void fprintTime( )

{

time_t currentTime;

struct tm *timm;

currentTime = time( NULL );

timm = localtime( &currentTime );

fprintf( agent_log, "%2.2d:%2.2d:%2.2d -  ", timm->tm_hour, timm->tm_min, timm->tm_sec );

}

Листинг 7.4. Программа клиента. Файл reply_driven_client.c.

#include <stdlib.h>

#include <stdio.h>

#include <unistd.h>

#include <signal.h>

#include <sys/dispatch.h>

#include "../reply_driven_server/defs.h"

void sigint_handler( int );

ClientMsg_T  send_msg;

char         reply_buff[ BUF_LEN ];

int          connect_id;

FILE*        client_log;

void         fprintTime( );

int main( int argc, char *argv[] )

{

if( argc < 2 ) {

printf( "Client: cmd args error!\n" );

exit(1);

}

unsigned int flag;

if( strcmp(argv[1],"-l" ) == 0 )       flag = 0;

else if( strcmp(argv[1],"-g" ) == 0 )  flag = NAME_FLAG_ATTACH_GLOBAL;

else {

printf( "Client: cmd args error!\n" );

exit(1);

}

char file_name[64];

sprintf( file_name, "client_log_%d.txt", getpid( ) );