Example #1
0
/**
 * This function represents client thread. Thanks to this function we can send
 * data to serwer.
 *
 * @param nth - not used but demanded by pthread_create() convencion
 * @return no value returned
 */
void* send(void* nth) {
	my_data_t msg;
	
	std::string buffer;
	
	/* We trying to open connection with channel, store coid value */
	while((coid = name_open(stranger_channel, 0)) == -1) {
		std::cout<<"Waiting for the server to be created. \n";
		sleep(2);
	}
	
	msg.header.type = 0x00;
	msg.header.subtype = 0x00;
	
	/* main loop - sending data */
	while(true) {
		//std::cout<<"["<<my_channel<<"]: ";
		printf("[%s]: ", my_channel);
		fflush(stdout);
		std::getline(std::cin, buffer);
		std::strncpy(msg.data, buffer.c_str(), 32);
		msg.data[31]='\0';
		
		if(MsgSend(coid, &msg, sizeof(msg), NULL, 0) == -1) {
			std::cerr<<"Error while sending message\n";
			exit(-1);
		}
	}
}
Example #2
0
int main(int argc, char *argv[]) {
	int status;
	struct MESSAGE msg; // буфер посылаемого сообщени¤
	unsigned char rmsg; // Ѕуфер ответного сообщени¤
	char command[10]; // Ѕуфер команд оператора
	int coid;

	coid=name_open("apu/roby",0);
	printf("apu/roby has coid=%d\n\n", coid);

	do {
		printf("\n>");
		scanf("%s", command);

		switch (command[0]) {
		case 'A':       //Write port A
			msg.type = 0;
			sscanf(command + 1, "%X", &msg.buf);
			status = MsgSend(coid, &msg, sizeof(msg), &rmsg, sizeof(rmsg));
			printf("Write port A: message <0x%X>\n", msg.buf);
			break;

		case 'C':       //Write port C
			msg.type = 1;
			sscanf(command + 1, "%X", &msg.buf);
			status = MsgSend(coid, &msg, sizeof(msg), &rmsg, sizeof(rmsg));
			printf("Write port C: message <0x%X>\n", msg.buf);
			break;
		case 'c':       //Read port  C
			msg.type = 2;
			status = MsgSend(coid, &msg, sizeof(msg), &rmsg, sizeof(rmsg));
			printf("Port C: 0x%X\n", rmsg);
			break;
		case 'b':       //Read port  B
			msg.type = 3;
			status = MsgSend(coid, &msg, sizeof(msg), &rmsg, sizeof(rmsg));
			printf("Port B: 0x%X\n", rmsg);
			break;

		case 'E':
			return 0;       //Return from main
		default:
			printf("Unknown command\n");
			break;
		}
	} while (1);

	return EXIT_SUCCESS;
}
Example #3
0
/*** Client Side of the code ***/
int client() {
    mss_t msg;
    int server_coid;
    mss_t rmsg;

    if ((server_coid = name_open(ATTACH_POINT, 0)) == -1) {
        return EXIT_FAILURE;
    }

    /* We would have pre-defined data to stuff here */
   // msg.hdr.type = 0x00;
    //msg.hdr.subtype = 0x00;
    msg.ile = 0;
    int typ = 1;
    msg.typ = typ;
    int from = getpid();
    msg.from = from;


    int i = 0;
    int count = 0;

    /* Do whatever work you wanted with server connection */

    	printf("what you want Send?");
    	scanf("%s", msg.text);

    	for(i = 0; i < strlen(msg.text); i++){
    		if(islower(msg.text[i]) )
    		count++;
    	}
    	printf("count : %d ", count);
    	msg.ile = count;

    	printf("Client sending msg, lower case letters: %d \n", msg.ile);

        if (MsgSend(server_coid, &msg, sizeof(msg), &rmsg, sizeof(rmsg)) == -1) {
        	perror("MsgSend");
        	exit(EXIT_FAILURE);
        }

        printf("received message %s from server %d\n",rmsg.text, rmsg.from);


    /* Close the connection */
    name_close(server_coid);
    return EXIT_SUCCESS;
}
Example #4
0
int connectslaves(slave_t *pslave) {
	for(int i=0; i<pslave->amount; ++i) {
		if((pslave->pslave[i].coid != ConnectServerInfo(0, pslave->pslave[i].coid, NULL))
			|| (pslave->pslave[i].status == DOWN)) {
			pslave->pslave[i].coid = name_open(pslave->pslave[i].name, NAME_FLAG_ATTACH_GLOBAL);
			pslave->pslave[i].clientnow = 0;
			if(pslave->pslave[i].coid == -1) {
				perror(pslave->pslave[i].name);
				pslave->pslave[i].status = DOWN;
			} else {
				pslave->pslave[i].status = READY;
			}
		}
	}
	return EXIT_SUCCESS;
}
Example #5
0
int main(int argc, char* argv[]) {
	int ping_coid;
	int status; //status return value used for ConnectAttach and MsgSend
	struct _pulse *mypulse;
	struct _msg_info info;
	mss_t msg;
	name_attach_t *attach;

	setvbuf(stdout, NULL, _IOLBF, 0);

	if ((attach = name_attach(NULL, PONG, 0)) == NULL) {
		printf("server:failed to attach name, errno %d\n", errno );
		exit(1);
	}

	if ((ping_coid = name_open(PING, 0)) == -1) {
		printf("failed to find ping, errno %d\n", errno );
		exit(1);
	}

	status = MsgSendPulse(ping_coid, PULSE_PRIORITY, PULSE_CODE, 1);
	if (-1 == status) {
		perror("MsgSendPulse");
		exit(EXIT_FAILURE);
	}

	while (1) {
		int rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), &info);
		if (rcvid == -1) {
			perror("MsgReceive");
			break;
		} else if (rcvid == 0) {
			mypulse = (struct _pulse *) &msg;
			printf("Pulse code: %d, pulse value %d\n", mypulse->code,
					mypulse->value.sival_int);
			sleep(1);
			status = MsgSendPulse(ping_coid, PULSE_PRIORITY, PULSE_CODE,
					mypulse->value.sival_int + 1);
		} else {
			printf("Unexpcted message");

		}
	}

	return EXIT_SUCCESS;
}
Example #6
0
int main(int argc, char *argv[]) {
    int server_coid, a_coid;
    short msg;
    int a_chid;
    struct _asyncmsg_connection_attr aca;
    struct _server_info sinfo;
    int count = 1;

    /* look for server */
    server_coid = name_open( RECV_NAME, 0 );
    while( server_coid == -1 )
    {
      sleep(1);
      server_coid = name_open( RECV_NAME, 0 );
    }

        /* need server's pid, use this to get server info including pid */
        ConnectServerInfo( 0, server_coid, &sinfo );

        /* send message to server request the asynchronous channel id */
        msg = GET_ACHID;
    if (MsgSend( server_coid, &msg, sizeof( msg ), &a_chid, sizeof(a_chid) ))
    {
        perror(PROGNAME "MsgSend");
        exit( EXIT_FAILURE );
    }
    
    /* setup array of send buffers */
    memset(&aca, 0, sizeof(aca));
    aca.buffer_size = 2048;
    aca.max_num_buffer = 5;  /* don't locally accumulate more than 5 buffers */
    aca.trigger_num_msg = 1; /* deliver each message as it is sent */
    aca.call_back = callback; /* call this function after messages are sent */

    /* create my asynchronous connection to the server */
    a_coid = asyncmsg_connect_attach( 0, sinfo.pid, a_chid, 0, 0, &aca );
    if( -1 == a_coid )
    {
        perror( "async connect");
        printf("server pid is %d\n", sinfo.pid );
        exit( EXIT_FAILURE );
    }

    while(1)
    {   int ret, len;
        char buf[80];

        len = sprintf(buf, "message #%d", count)+1;
        printf("sending %dth time\n", count );

        /* deliver message to server, the count value will be passed into
         * my callback as the "handle"
         */
        ret = asyncmsg_put( a_coid, buf, len, count, NULL );
        if( -1 == ret )
        {
                perror( "put");
                exit( EXIT_FAILURE );
        }
        count++;
        sleep(1);
    }
}
Example #7
0
int main(int argc, char **argv) {
	if(argc != 2) {
		printf("Set client id\n");
		return EXIT_SUCCESS;
	}
	uint32_t cid = atoi(argv[1]);//!!
	struct timespec timeout, stime, etime;
	frame_t frame, rframe;
	char buf[BUFFER_SIZE], cmd[BUFFER_SIZE], param[BUFFER_SIZE];
	int itr = -1, n, k;
	FILE *pfile = 0;
	uint32_t itr_max = ITR;
	double_t *pfcn, *psig, init;
	uint64_t timeoutsend = 200000000;

//*****************************************************************************
	timeout.tv_nsec = 0;
	timeout.tv_sec = 1;

	strcpy(buf, CFGFILENAME);
	strcat(buf, argv[1]);
	pfile = fopen(buf, "r");
	if(NULL != pfile) {
		while(0 == feof(pfile)) {
			fscanf(pfile, "%s %s", cmd, param);
			if(0 == strcmp(cmd, "itr_max")) {
				itr_max = atoi(param);
			}
			else if(0 == strcmp(cmd, "timeoutsend")) {
				timeoutsend = atol(param);
			}
			else if(0 == strcmp(cmd, "timeout_sec")) {
				timeout.tv_sec = atol(param);
			}
			else if(0 == strcmp(cmd, "timeout_nsec")) {
				timeout.tv_nsec = atol(param);
			}
		}
		fclose(pfile);
	} else {
		perror(MSG_ERR_CFGFILE);
	}

	strcpy(buf, OUTFILENAME);
	strcat(buf, argv[1]);
	pfile = fopen(buf, "w");
	fprintf(pfile, "itr_max %i\ntimeoutsend   %llu\ntimeout %lu %lu\n",
			itr_max, timeoutsend,
			timeout.tv_sec, timeout.tv_nsec);

	pfcn = malloc(sizeof(double_t)*(SIZE*2));
	psig = malloc(sizeof(double_t)*((SIZE-1)*2));

	srand(time(NULL));
	for(size_t i = 0; i<SIZE; i++) {
		pfcn[i] = i*STEP;
		pfcn[i+SIZE] = sin(pfcn[i])+2;
	}

	for(size_t i = 0; i<SIZE-1; i++) {
		psig[i] = SHIFT + (rand()%SIZE)*STEP;
		psig[i+SIZE-1] = sin(psig[i])+2;
	}

	frame.cid = cid;
	frame.fid = 0;
	frame.size = 2;

//*****************************************************************************

	int srv_coid = name_open(SRV_NAME, NAME_FLAG_ATTACH_GLOBAL);
	__ERROR_EXIT(srv_coid, -1, SRV_NAME);
	frame.ptask = malloc(sizeof(task_t)*2);
	frame.ptask[0].cmd = SPLINE_INIT;
	frame.ptask[0].n = SIZE*2*sizeof(double_t);
	frame.ptask[0].px = (void*)pfcn;
	frame.timeout = timeout;
	frame.ptask[1].cmd = SPLINE_GETVAL;
	frame.ptask[1].n = 2*sizeof(double_t);
	frame.ptask[1].px = (void*)psig;
	frame_repinit(&frame, &rframe);

	__ERROR_CHECK(TimerTimeout(CLOCK_REALTIME, _NTO_TIMEOUT_SEND | _NTO_TIMEOUT_REPLY,
									NULL, &timeoutsend, NULL), -1, "TimerTimeout");
	// send init data about spline
	__ERROR_CHECK(frame_send(srv_coid, &frame, &rframe), -1, "sendv");
	frame_destroy(&rframe);
	free(frame.ptask);

	frame.ptask = malloc(sizeof(task_t));
	frame.ptask[0].cmd = SPLINE_GETVAL;
	frame.ptask[0].n = sizeof(double_t);
	frame.size = 1;
	frame_repinit(&frame, &rframe);
	k=0;
	for(uint32_t i=0; i<itr_max; ++i) {
		frame.fid = i;
		n = rand()%(SIZE-1);
		frame.ptask[0].px = (void*)&psig[n];
		trace_msg_start();
		// send tasks to server and wait for results
		__ERROR_CONT(frame_send(srv_coid, &frame, &rframe), -1, "sendv");
		trace_msg_stop();
		clock_gettime(CLOCK_MONOTONIC, &etime);
		 fprintf(pfile, "%f  %f  %u  %u\n", psig[n], *(double_t*)rframe.ptask->px,
				etime.tv_sec-stime.tv_sec, etime.tv_nsec-stime.tv_nsec);
		 printf("I receive data\n");
	}
	frame.ptask[0].cmd = SPLINE_DESTROY;
	frame.ptask[0].n = 0;
	frame.ptask[0].px = 0;
	frame.fid++;
	// say server to destroy our spline
	__ERROR_CHECK(frame_send(srv_coid, &frame, &rframe), -1, "sendv");
	fclose(pfile);
	frame_destroy(&rframe);
	free(pfcn);
	free(psig);

	__ERROR_CHECK(name_close(srv_coid), -1, "name_close");

	return EXIT_SUCCESS;
}
Example #8
0
int main(int argc, char* argv[]) {
	int coid; //Connection ID to server
	int status; //status return value used for ConnectAttach and MsgSend
	mms reply;
	int file;
	int type;

	if ((coid = name_open(ATTACH_POINT, 0)) == -1) {
		return EXIT_FAILURE;
	}

	while (1) {
		scanf("%d", &type);
		if (type == OPENR) {
			mms msg;
			printf("Nazwa pliku:\n");
			scanf("%s", msg.buffer);
			msg.type = OPENR;
			status = MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
			if(-1 == status) {
				perror("MsgSend");
				exit(EXIT_FAILURE);
			}
			file = reply.fh;
			printf("Otwarto plik, deskryptor: %d\n", file);
			continue;
		}
		if (type == OPENW) {
			mms msg;
			printf("Nazwa pliku:\n");
			scanf("%s", msg.buffer);
			msg.type = OPENW;
			status = MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
			if(-1 == status) {
				perror("MsgSend");
				exit(EXIT_FAILURE);
			}
			file = reply.fh;
			printf("Otwarto plik, deskryptor: %d\n", file);
			continue;
		}
		if (type == READ) {
			mms msg;
			printf("Ilosc bajtow:\n");
			scanf("%d", &(msg.bytes_count));
			msg.fh = file;
			msg.type = READ;
			status = MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
			printf("Odczytano %d bajtow, tresc: %s\n", reply.bytes_count, reply.buffer);
			continue;
		}
		if (type == WRITE) {
			mms msg;
			printf("Tresc:\n");
			scanf("%s", msg.buffer);
			msg.fh = file;
			msg.type = WRITE;
			status = MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
			printf("Zapisano %d bajtow, tresc: %s\n", reply.bytes_count, reply.buffer);
			continue;
		}
		if (type == CLOSE) {
			mms msg;
			msg.fh = file;
			msg.type = CLOSE;
			status = MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
			printf("Zamknieto plik o deskryptorze %d, rezultat %d\n", reply.fh, reply.buffer);
			continue;
		}
		if (type == STOP) {
			mms msg;
			msg.type = STOP;
			status = MsgSend(coid, &msg, sizeof(msg), &reply, sizeof(reply) );
			printf("Koniec\n");
			break;
		}
	}

	return EXIT_SUCCESS;
}
Example #9
0
int main(int argc, char *argv[]) {
    int server_coid, self_coid, chid, rcvid;
    struct reg_msg msg;

    setvbuf (stdout, NULL, _IOLBF, 0);

    /* look for server */
    server_coid = name_open( RECV_NAME, 0 );
    while( server_coid == -1 )
    {  
      sleep(1);
      server_coid = name_open( RECV_NAME, 0 );
    }

    chid = ChannelCreate(0);
    if( -1 == chid)
    {
        perror( PROGNAME "ChannelCreate");
        exit( EXIT_FAILURE );
    }
    self_coid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 );
    if( -1 == self_coid )
    {
        perror( PROGNAME "ConnectAttach");
        exit( EXIT_FAILURE );
    }
    
    msg.type = REG_MSG;
    
    /* class: Initialize the sigevent structure (msg.ev) in the message 
     * to be sent to the server.
     */
    SIGEV_PULSE_INIT( &msg.ev, self_coid, getprio(0), MY_PULSE_CODE, 0 );

    if (MsgSend( server_coid, &msg, sizeof( msg ), NULL, 0 ))
    {
        perror(PROGNAME "MsgSend");
        exit( EXIT_FAILURE );
    }
    
    while( 1 )
    {
        rcvid = MsgReceive( chid, &recv_buf, sizeof(recv_buf), NULL );
        if( -1 == rcvid )
        {
            perror(PROGNAME "MsgReceive");
            continue;
        }
        if ( 0 == rcvid )
        {
            if (MY_PULSE_CODE == recv_buf.pulse.code )
            {
                printf(PROGNAME "got my pulse, value is %d\n", recv_buf.pulse.value.sival_int);
            } else
            {
                printf(PROGNAME "got unexpected pulse with code %d, value %d\n", 
                        recv_buf.pulse.code, recv_buf.pulse.value.sival_int );
            }
            continue;
        }
        printf(PROGNAME "got unexpected message, type: %d\n", recv_buf.type );
        MsgError( rcvid, ENOSYS );
    }
 }