Exemplo n.º 1
0
void client::run_client_send() {
	HANDLE Semaphores[2];
	Semaphores[0] = Semaphore_client_send;
	Semaphores[1] = Semaphore_client_send_exit;
	while (1) {
		ReleaseSemaphore(Semaphore_server_send, 1, NULL);
		int index = WaitForMultipleObjects(2, Semaphores, FALSE, INFINITE) - WAIT_OBJECT_0;
		if (index == 1) break;

		if (getch_noblock() != -1) {
			DWORD number_of_bytes_written;
			print_server();
			cin.clear();
			getline(cin, message_to_send);

			ReleaseSemaphore(Semaphore_server_start_print, 1, NULL);

			int number_of_blocks = message_to_send.size() / size_of_buff + 1;
			int size = message_to_send.size();
			WriteFile(hPipe, &number_of_blocks, sizeof(number_of_blocks), &number_of_bytes_written, (LPOVERLAPPED)NULL);
			WriteFile(hPipe, &size, sizeof(size), &number_of_bytes_written, (LPOVERLAPPED)NULL);
			for (int i = 0; i < number_of_blocks; i++) {
				message_to_send.copy(buff, size_of_buff, i * size_of_buff);
				if (!WriteFile(hPipe, buff, size_of_buff, &number_of_bytes_written, (LPOVERLAPPED)NULL)) {
					cout << "Write Error" << endl;
				}
			}
			WaitForSingleObject(Semaphore_server_end_print, INFINITE);
		}
		Sleep(1);
	}
}
Exemplo n.º 2
0
void server::server_send() 
{
	HANDLE Semaphores[2];
	Semaphores[0] = Semaphore_server[3];	//Semafore_Server_Send
	Semaphores[1] = Semaphore_server[4];	//Semafore_Server_Send_exit
	while (1) 
	{
		WaitForMultipleObjects(2, Semaphores, FALSE, INFINITE) - WAIT_OBJECT_0;		//received permission

		ReleaseSemaphore(Semaphore_client[3], 1, NULL);		//SEMAPHORE_CLIENT_SEND
		if (getch_noblock() != -1) 
		{
			DWORD numberber_of_bytes_written;
			print_client();
			cin.clear();
			getline(cin, send_message);

			if (send_message == "end\0" || send_message == "exit\0") 
			{
				ReleaseSemaphore(Semaphore_server[2], 1, NULL);		//SEMAPHORE_SERVER_PRINT_EXIT
				ReleaseSemaphore(Semaphore_client[2], 1, NULL);		//SEMAPHORE_CLIENT_PRINT_EXIT
				ReleaseSemaphore(Semaphore_client[4], 1, NULL);		//SEMAPHORE_CLIENT_SEND_EXIT
				WaitForSingleObject(ChildProcessInfo.hProcess, INFINITE);
				break;
			}

			ReleaseSemaphore(Semaphore_client[0], 1, NULL);			//SEMAPHORE_CLIENT_START_PRINT

			int numberber_of_blocks = send_message.size() / size_of_buffer + 1;
			int size = send_message.size();
			WriteFile(hNamedPipe, &numberber_of_blocks, sizeof(numberber_of_blocks), &numberber_of_bytes_written, (LPOVERLAPPED)NULL);
			WriteFile(hNamedPipe, &size, sizeof(size), &numberber_of_bytes_written, (LPOVERLAPPED)NULL);
			for (int i = 0; i < numberber_of_blocks; i++) 
			{
				send_message.copy(buffer, size_of_buffer, i * size_of_buffer);
				if (!WriteFile(hNamedPipe, buffer, size_of_buffer, &numberber_of_bytes_written, (LPOVERLAPPED)NULL))
				{
					cout << "Write Error" << endl;
				}
			}	
			WaitForSingleObject(Semaphore_client[1], INFINITE);		//SEMAPHORE_CLIENT_END_PRINT
		}
		Sleep(1);
	}
}
Exemplo n.º 3
0
void client::run_client_send() {
	void *buffer = shmat(shm_id, NULL, 0);
	while (1) {
		system("stty -echo");
		ReleaseSemaphore(sem_s_id, 0);
		WaitSemaphore(sem_c_id, 0);
		if (getch_noblock() != -1) {
			ReleaseSemaphore(sem_s_id, 1);
			message_to_send.clear();
			cout << "Server: ";
			system("stty echo");
			tcflush(STDIN_FILENO, TCIFLUSH);
			getline(cin, message_to_send);
			system("stty -echo");

			ReleaseSemaphore(sems_id, 2);

			length = message_to_send.size();
			*(int*)buffer = length;

			ReleaseSemaphore(sems_id, 0);
			WaitSemaphore(sems_id, 1);

			NumberOfBlocks = ceil((double)length / (double)size_of_buff);
			for (int i = 0; i < NumberOfBlocks; i++) {
				message_to_send.copy((char*)buffer, size_of_buff, i*size_of_buff);

				ReleaseSemaphore(sems_id, 0);
				WaitSemaphore(sems_id, 1);
			}
			WaitSemaphore(sems_id, 3);
		}
		usleep(SLEEP_TIME_ONE);
		length = *(int*)buffer;
		if (length == -1) break;
	}
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    char key;
    int threadsCounter = 0;
    int currentPrinting = 1;
    MyThread* threads[10];

#ifdef _WIN32

    while (1)
    {
        key = getch_noblock();
        Sleep(1);
        switch (key)
        {
        case '+':
            if (threadsCounter < 10)
            {
                threadsCounter++;
                threads[threadsCounter - 1] = new MyThread(threadsCounter);
            }
            break;
        case '-':
            if (threadsCounter > 0)
            {
                threads[threadsCounter - 1]->closeThread();
                threadsCounter--;
            }
            break;
        case 'q':
            if (threadsCounter > 0)
            {
                for (int i = 0; i < threadsCounter; i++)
                    threads[i]->closeThread();
            }
            return 0;
        }

        if (threadsCounter && threads[currentPrinting - 1]->canPrint())
        {
            if (currentPrinting >= threadsCounter)
                currentPrinting = 1;
            else
                currentPrinting++;
            threads[currentPrinting - 1]->startPrint();
        }
    }

#elif __linux__

    initscr();
    clear();
    noecho();
    refresh();
    nodelay(stdscr, TRUE);

    while (1)
    {
        key = getch();
        usleep(1000);
        fflush(stdout);
        refresh();

        switch (key)
        {
        case '+':
            if (threadsCounter < 10)
            {
                threadsCounter++;
                threads[threadsCounter - 1] = new MyThread(threadsCounter);
            }
            break;

        case '-':
            if (threadsCounter > 0)
            {
                threads[threadsCounter - 1]->closeThread();
                threadsCounter--;
            }
            break;

        case 'q':
            if (threadsCounter > 0)
            {
                for (int i = 0; i < threadsCounter; i++)
                    threads[i]->closeThread();
            }
            clear();
            endwin();
            return 0;
        }

        if (threadsCounter && threads[currentPrinting - 1]->canPrint())
        {
            if (currentPrinting >= threadsCounter)
                currentPrinting = 1;
            else
                currentPrinting++;
            threads[currentPrinting - 1]->startPrint();
        }
        refresh();
    }
#endif
}
Exemplo n.º 5
0
void server::server_send(char *argv) 
{
	switch (pid)
	{
	case -1:
		printf("New process is not created\n");
		return;

	case 0:
		execlp("mate-terminal", "mate-terminal", "-x", argv, "-n 2", itoa(shm_id),
			itoa(sem_id), itoa(sems_id), itoa(sem_server_id), itoa(sem_client_id), NULL);
		break;

	default:
	{
		void *bufferer = shmat(shm_id, NULL, 0);
		while (1) 
		{
			system("stty -echo");
			ReleaseSemaphore(sem_client_id, 0);
			WaitSemaphore(sem_server_id, 0);
			if (getch_noblock() != -1) 
			{
				ReleaseSemaphore(sem_client_id, 1);
				send_message.clear();
				cout << "Client: ";
				system("stty echo");
				tcflush(STDIN_FILENO, TCIFLUSH);
				getline(cin, send_message);
				system("stty -echo");

				ReleaseSemaphore(sem_id, 2);

				if (send_message == "exit" || send_message == "end") 
				{
					length = -1;
					*(int*)bufferer = length;
					ReleaseSemaphore(sem_id, 0);
					ReleaseSemaphore(sems_id, 2);
					ReleaseSemaphore(sems_id, 0);
					ReleaseSemaphore(sem_client_id, 0);
					WaitSemaphore(sem_id, 1);
					waitpid(pid, NULL, 0);
					break;
				}

				length = send_message.size();
				*(int*)bufferer = length;

				ReleaseSemaphore(sem_id, 0);
				WaitSemaphore(sem_id, 1);

				numberberOfBlocks = ceil((double)length / (double)size_of_buffer);
				for (int i = 0; i < numberberOfBlocks; i++) 
				{
					send_message.copy((char*)bufferer, size_of_buffer, i*size_of_buffer);
					ReleaseSemaphore(sem_id, 0);
					WaitSemaphore(sem_id, 1);
				}
				WaitSemaphore(sem_id, 3);
			}
			usleep(SLEEP_TIME_ONE);
		}
	}
	break;
	}
}
Exemplo n.º 6
0
int main(int argc, char *argv[])
{
    #ifdef __linux__
	initscr();
    clear();
    noecho();
    refresh();
    nodelay(stdscr, TRUE);
    #endif
    char symbol;
    int threadsCount = 0;
    int currentThread = 1;
    Thread* thread[THREADS_NUMBER];
    while(TRUE)
    {
    	symbol = getch_noblock();
    	Sleep(SHORT_SLEEP);
        fflush(stdout);
        #ifdef __linux__
        refresh();
        #endif

    	switch (symbol)
    	{
    		case '+':
    		    if (threadsCount < THREADS_NUMBER)
    		    {
    		    	thread[threadsCount++] = new Thread(threadsCount + 1);
    		    }
    		    break;

    		case '-':
    		    if (threadsCount > 0)
    		    {
    		    	thread[--threadsCount]->closeThread();
    		    }
    		    break;

    		case 'q':
    		    if (threadsCount > 0)
    		    {
    		    	for (int i = 0; i < threadsCount; i++)
    		    	{
    		    		thread[i]->closeThread();
    		    		#ifdef __linux__
    		    		pthread_join(thread[i]->getInner(), NULL);
    		    		#endif
    		    	}
    		    }
    		    #ifdef __linux__
    		    clear();
    		    endwin();
    		    #elif _WIN32
    		    closeMutex();
    		    #endif
    		    return 0;
    	}
    	if (threadsCount && thread[currentThread - 1]->canPrint())
        {
            if (currentThread >= threadsCount)
                currentThread = 1;
            else
                currentThread++;
            thread[currentThread - 1]->startPrint();
        }
        #ifdef __linux__
        refresh();
        #endif
    }
}
//=========================================================================
// MAIN PROGRAM
//=========================================================================
int main (int argc, char *argv[]) {
    Aardvark handle;

    int port       = 0;
    u08 addr       = 0;
    int timeout_ms = 0;
    
    u08 slave_resp[SLAVE_RESP_SIZE];
    int i;
	/*
    if (argc < 4) {
        printf("usage: aai2c_slave PORT SLAVE_ADDR TIMEOUT_MS\n");
        printf("  SLAVE_ADDR is the slave address for this device\n");
        printf("\n");
        printf("  The timeout value specifies the time to\n");
        printf("  block until the first packet is received.\n");
        printf("  If the timeout is -1, the program will\n");
        printf("  block indefinitely.\n");
        return 1;
    }
	*/
/*
    port       = atoi(argv[1]);
    addr       = (u08)strtol(argv[2], 0, 0);
    timeout_ms = atoi(argv[3]);
*/

	port    = 0;	//atoi(argv[1]);
    addr    = 0x08;	//(u08)strtol(argv[5], 0, 0);
    timeout_ms  = 1*1000;	//atoi(argv[6]);

    // Open the device
    handle = aa_open(port);
    if (handle <= 0) {
        printf("Unable to open Aardvark device on port %d\n", port);
        printf("Error code = %d\n", handle);
        return 1;
    }

    // Ensure that the I2C subsystem is enabled
    aa_configure(handle, AA_CONFIG_SPI_I2C);

    // Disable the Aardvark adapter's power pins.
    // This command is only effective on v2.0 hardware or greater.
    // The power pins on the v1.02 hardware are not enabled by default.
    aa_target_power(handle, AA_TARGET_POWER_NONE);

    // Set the slave response; this won't be used unless the master
    // reads bytes from the slave.
    for (i=0; i<SLAVE_RESP_SIZE; ++i)
        slave_resp[i] = 'A' + i;

    //No response: aa_i2c_slave_set_response(handle, SLAVE_RESP_SIZE, slave_resp);
	aa_i2c_slave_set_response(handle, 0, NULL);


    // Enable the slave
    aa_i2c_slave_enable(handle, addr, 0, 0);
	// Watch the I2C port
	printf("Press 'q' to exit the program!!! \n");
	
    do {
		dump(handle, timeout_ms);
	} while(getch_noblock() != 'q' );
	// Disable the slave and close the device
    aa_i2c_slave_disable(handle);
    aa_close(handle);

    return 0;
}