📄 master.cpp
字号:
#include "comunic.h"
#include "comunic.cpp"
#include <conio.h>
#define COM 1 //comul pe care se trimite/primeste
#define TS 0 //this station
int poll_list[NR];//poll list - activ=1 / inactiv=0
char *name;//numele fisierului
int slave;//cine este interogat
FILE *file;
void init()
{
int i;
init_port(COM);
poll_list[TS] = 1;
for(i = 0;i < NR;i++)
if(i != TS) poll_list[i]=0;
}
void broadcast()
{
TEL tel;
tel.sa = TS;
tel.da = 127;
strcpy(tel.data, "0");
tel.data = encode(tel.data, strlen(tel.data)+1, &tel.length);
send(COM, &tel, TI_LF_CU_DATE);
}
int request_status(int i)
{
TEL *telr;
TEL teli;
teli.sa = TS;
teli.da = i;
teli.data = NULL;
teli.length = 0;
int type;
send(COM, &teli, TI_FARA_DATE);
telr = recv(COM, &type);
if(type != TR_FARA_DATE && poll_list[i] == 1)
{
send(COM, &teli, TI_FARA_DATE);
telr = recv(COM, &type);
if(type != TR_FARA_DATE)
{
free(telr);
return 0;
}
}
if(type == TR_FARA_DATE)
{
free(telr);
return 1;
}
free(telr);
return 0;
}
TEL* request_data(int i)
{
TEL teli;
TEL *telr;
int error;
int type;
char *cine = "cine";
teli.sa = TS;
teli.da = i;
teli.data = strdup(cine);
teli.length = strlen(cine);
teli.data = encode(teli.data,teli.length+1, &teli.length);
send(COM, &teli, TI_LF_CU_DATE);
telr = recv(COM, &type);
if(type != TR_LF_CU_DATE && poll_list[i] == 1)
{
send(COM, &teli, TI_LF_CU_DATE);
telr = recv(COM, &type);
if(type != TR_LF_CU_DATE)
{
free(telr);
free(teli.data);
return NULL;
//printf("Comunicare esuata.\n");
}
}
if(type != TR_LF_CU_DATE && poll_list[i] == 0)
{
free(telr);
free(teli.data);
return NULL;
//printf("Comunicare esuata\n");
}
telr->data = decode(telr->data, telr->length, &(telr->length), &error);
if(error == -1)
{
free(telr);
free(teli.data);
return NULL;
}
free(teli.data);
return telr;
}
TEL* send_request_data(int i, int pos)
{
TEL teli;
TEL *telr;
int type;
char *arg;
int error;
teli.sa = TS;
teli.da = i;
if(pos == 0)
{
teli.data = strdup(name);
teli.length = strlen(name);
teli.data = encode(teli.data, teli.length+1, &teli.length);
}
else
{
arg = (char*)malloc(sizeof(char) * 4);
sprintf(arg, "%d", pos);
teli.data = strdup(arg);
teli.length = strlen(arg);
teli.data = encode(teli.data, teli.length+1, &teli.length);
free(arg);
}
send(COM, &teli, TI_LV_CU_DATE);
telr = recv(COM, &type);
if(type != TR_LV_CU_DATE && poll_list[i] == 1)
{
send(COM, &teli, TI_LV_CU_DATE);
telr = recv(COM, &type);
if(type != TR_LV_CU_DATE)
{
free(telr);
free(teli.data);
return NULL;
}
}
if(type != TR_LV_CU_DATE && poll_list[i] == 0)
{
free(telr);
free(teli.data);
return NULL;
}
telr->data = decode(telr->data, telr->length, &(telr->length), &error);
if(error == -1)
{
free(telr);
free(teli.data);
return NULL;
}
free(teli.data);
return telr;
}
void explore_polllist()
{
int i;
for(i = 0;i < NR;i++)
if(i != TS)
{
poll_list[i] = request_status(i);
if(poll_list[i] == 1) printf("Slave-ul %d este prezent\n", i);
else printf("Slave-ul %d nu este prezent\n", i);
}
}
void message_cycle()
{
TEL *tel;
int i, pos = 0;
printf("\nServiciu de prioritate mare:\n");
tel = send_request_data(slave, 0);
while(strcmp(tel->data, "gata") != 0 && tel != NULL)
{
printf("Am primit\n");
pos += tel->length;
for(int i = 0;i<tel->length;i++)
if(tel->data[i] == 10) pos++;
//printf("%d ",tel->data[i]);
fwrite(tel->data, sizeof(char), tel->length, file);
free(tel);
tel = send_request_data(slave, pos);
}
if(tel == NULL) printf("\nServire esuata\n");
fclose(file);
//explorare poll list
printf("\nExplorare PollList:\n");
explore_polllist();
printf("\nServiciu de prioritate mica:\n");
for(i = 1;i < NR;i++)
{
tel = request_data(i);
if(tel != NULL) printf("Slave-ul %d raspunde: %s\n", i, tel->data);
free(tel);
}
}
void main(int argc, char *argv[])
{
init();
clrscr();
printf("Vad cine este in retea\n");
int exist = 0;
while(exist == 0)
{
printf("again\n");
explore_polllist();
for(int i = 0;i < NR && exist == 0;i++)
if(poll_list[i] == 1 && i != TS) exist = 1;
}
//broadcast - anunt ca eu sunt master
printf("Broadcast\n");
broadcast();
if(argc == 3)
{
slave = atoi(argv[2]);//slave-ul
name = strdup(argv[1]);//numele fisierului
file = fopen(name, "w");
printf("Message cycle:\n");
message_cycle();
free(name);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -