Esempio n. 1
0
static int simple_handler(CManager cm, void *vevent, void *client_data, attr_list attrs)
{
  log_info("Simple Handler");
  simple_rec_ptr event = (_simple_rec*)vevent;
  if(EVclient_source_active(stor_source_handle))
  {
    printf("Sending the message on...\n");
    EVsubmit(stor_source_handle, event, NULL);
  }
  else
  {
    fprintf(stderr, "Error, stor_source_handle inactive something is wrong!!\n");
  }

  file_receive(event);
  return 1;
}
void client() {

	int consock, registerfd, server_sockfd, client_sockfd;
	int server_len, client_len;
	struct sockaddr_in server_address;
	struct sockaddr_in client_address;
	int result;
	char option[1024];

	// consock=connect_client(2);
	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = htonl(INADDR_ANY);
	server_address.sin_port = htons(PORTNO);
	server_len = sizeof(server_address);
	bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
	listen(server_sockfd, 5);

	//initializing readfds to sockfd.
	FD_ZERO(&readfds);

	FD_SET(server_sockfd, &readfds);

	FD_SET(0, &readfds);

	while (1) // waiting for client requests
	{

		int fd;

		int nread;
		char ch[1024];

		testfds = readfds;

		result = select(FD_SETSIZE, &testfds, (fd_set *) 0, (fd_set *) 0,
				(struct timeval *) 0);
		if (result <= 0) {
			perror("Error");
			exit(1);
		}

		if (FD_ISSET(0, &testfds)) {
			memset(option, '\0', 1024);
			fgets(option, 1024, stdin);
			option[strlen(option) - 1] = '\0';
			parse_command(option);

		}

		else {

			for (fd = 0; fd < FD_SETSIZE; fd++) {
				if (FD_ISSET(fd, &testfds)) {
					if (fd == server_sockfd)

					{
						client_sockfd = accept(server_sockfd,
								(struct sockaddr *) &client_address,
								&client_len);

						FD_SET(client_sockfd, &readfds);

					} else {
						memset(ch, 0, 100);
						ioctl(fd, FIONREAD, &nread);
						if (nread == 0 && fd != 0)

						{
							close(fd);
							int j;
							////finding the fd in client connection////
							for (j = 1; j < 4; j++) {

								if (mycon[j].confd == fd) {
									break;
								}
							}

							////terminate the connection
							terminate_con(mycon[j].con_id);
							FD_CLR(fd, &readfds);

							display_con();

						} else {
							char temp1[1024];
							memset(ch, 0, 1024);
							memset(temp1, 0, 1024);
							read(fd, &ch, sizeof(ch));
							strcpy(temp1, ch);
							char *reader;
							reader = strtok(temp1, " ");

							if (reader != NULL) {

								if (strcmp(reader, "send") == 0) {
									read_table(fd, ch);
									memset(ch, 0, 1024);

								}

								else if (strcmp(reader, "connect") == 0) {

									update_conn(ch, fd);
									memset(ch, 0, 1024);

								} else if (strcmp(reader, "ERROR") == 0) {

									fprintf(stderr, "\n%s\n", ch);
									memset(ch, 0, 1024);

								} else if (strcmp(reader, "upload") == 0) {
									char filenm[256];
									bzero(filenm, 256);
									char* tk;
									long long files;
									tk = strtok(NULL, " ");

									strcpy(filenm, tk);

									tk = strtok(NULL, " ");
									files = atoll(tk);

									int val = file_receive(fd, files, filenm);
									if (val == -1)
										fprintf(stderr,
												"\n\nERROR IN FILE TRANSFER\n\n");
									memset(ch, 0, 1024);

								} else if (strcmp(reader, "terminate") == 0) {

									char* temp2;
									char ht[50];
									int pt, i;
									temp2 = strtok(NULL, " ");
									strcpy(ht, temp2);
									temp2 = strtok(NULL, " ");
									pt = atoi(temp2);
									for (i = 1; i < 4; i++) {
										if (((strcmp(mycon[i].hostn, ht) == 0))
												&& (mycon[i].port == pt))
											break;

									}

									mycon[i].con_id = -1;
									close(mycon[i].confd);
									FD_CLR(mycon[i].confd, &readfds);
									display_con();
									memset(ch, 0, 1024);

								} else if (strcmp(reader, "download") == 0) {
									char dfile[256], conip[16];
									bzero(dfile, 256);
									char* toknize;
									int dfd, port;
									toknize = strtok(NULL, " ");
									strcpy(dfile, toknize);
									toknize = strtok(NULL, " ");
									strcpy(conip, toknize);
									toknize = strtok(NULL, " ");
									port = atoi(toknize);
									int j;
									for (j = 1; j < 4; j++) {

										if ((strcmp(mycon[j].ip, conip) == 0)
												&& (mycon[j].port == port))
											break;
									}
									int val = file_transfer(mycon[j].confd,
											dfile);
									if (val == -1) {
										fprintf(stderr,
												"\n\nERROR IN FILE TRANSFER\n\n");
										write(mycon[j].confd, "ERROR IN FILE",
												30);
									}
									memset(ch, 0, 1024);

								}
							} else {

								memset(ch, '\0', 1024);
								memset(temp1, '\0', 1024);
								reader = NULL;

							}
						}
					}

				}
			}
		}
	}
}
Esempio n. 3
0
void chat_interface(int new_fd)
{
    clear();
    refresh();
    
    int flag = 3;

    int ch;

    WINDOW *new_window1, *new_window2, *new_window5;

    setlocale(LC_ALL, "");
    //initscr();

    //keypad(ch_window4, 1);

    new_window1 = newwin(3, 91,0,22);
    ch_window2 = newwin(13, 68,4,23);
    new_window5 = newwin(15, 70,3,22);
    ch_window3 = newwin(6, 70,18,22);
    ch_window4 = newwin(21, 21,3,92);
    ch_window5 = newwin(6, 20,0,2);
    ch_window1 = newwin(18, 20,6,2);

    box(new_window1, 0, 0);
    box(ch_window4, 0, 0);
    box(ch_window5, 0, 0);
    box(new_window5, 0, 0);
    box(ch_window1, 0, 0);
    box(ch_window2, ' ', ' ');
    box(ch_window3, 0, 0);

    mvwaddstr(ch_window1, 1, 5, "ONLINE-USER");
    mvwaddstr(ch_window1, 2, 5, "===========");

    mvwaddstr(ch_window5, 2, 7, msg1.name);
    mvwaddstr(ch_window5, 3, 6, msg1.id);
    
    mvwaddstr(ch_window4, 1, 6, "FUNCTION");
    mvwaddstr(ch_window4, 2, 5, "===========");
    mvwaddstr(ch_window4, 3, 5, "私聊");
    mvwaddstr(ch_window4, 4, 5, "群聊");
    mvwaddstr(ch_window4, 5, 5, "发送表情");
    mvwaddstr(ch_window4, 6, 5, "查看聊天记录");
    mvwaddstr(ch_window4, 7, 5, "删除聊天记录");
    mvwaddstr(ch_window4, 8, 5, "发送文件");
    mvwaddstr(ch_window4, 9, 5, "修改昵称");
    mvwaddstr(ch_window4, 10, 5, "修改密码");
    mvwaddstr(ch_window4, 11, 5, "切换用户");

    wrefresh(new_window1);
    wrefresh(ch_window3);
    wrefresh(ch_window5);
    wrefresh(new_window5);
    wrefresh(ch_window1);
    wrefresh(ch_window2);
    wrefresh(ch_window4);
 
    mvwaddstr(ch_window4, flag, 3, "=>");
    wrefresh(ch_window4);

    keypad(ch_window4,true);

    cbreak();

    while(1)
    {
        if(filesend == FILE_SEND)
	{
	    file_receive(new_fd);

	    filesend = 0;
	}

        curs_set(0);
        
        /*mvwaddstr(ch_window4, flag, 3, "  ");
	
	flag = 3;

        mvwaddstr(ch_window4, flag, 3, "=>");
	wrefresh(ch_window4);*/

        noecho();
        while((ch = wgetch(ch_window4)) != '\n')
	{
	    if(ch == KEY_UP)
	    {
		mvwaddstr(ch_window4, flag, 3, "  ");

	        if(flag == 3)
		{
		    flag = 11;
		}
		else
		{
		    flag--;
		}

		mvwaddstr(ch_window4, flag, 3, "=>");
	    }
	    if(ch == KEY_DOWN)
	    {
		mvwaddstr(ch_window4, flag, 3, "  ");
                 
	        if(flag == 11)
		{
		    flag = 3;
		}
		else
		{
		    flag++;
		}

		mvwaddstr(ch_window4, flag, 3, "=>");
	        
	    }

	    wrefresh(ch_window4);
	}

	switch(flag)
	{
	    case 3:
	    {
	        private_chat(new_fd);
                break;
	    }
	    case 4:
	    {
	        group_chat(new_fd);
	        break;
	    }
	    case 5:
	    {   
	        send_face(new_fd);
	        break;
	    }
	    case 6:
	    {
	        check_history(new_fd);
	        break;
	    }
	    case 7:
	    {
	        delete_history(new_fd);
	        break;
	    }
	    case 8:
	    {
	        file_send(new_fd);
	        break;
	    }
	    case 9:
	    {
	        change_name(new_fd);
	        break;
	    }
	    case 10:
	    {
 
	        change_password(new_fd);

    box(new_window1, 0, 0);
    box(ch_window4, 0, 0);
    box(ch_window5, 0, 0);
    box(new_window5, 0, 0);
    box(ch_window1, 0, 0);
    box(ch_window2, ' ', ' ');
    box(ch_window3, 0, 0);

    mvwaddstr(ch_window1, 1, 5, "ONLINE-USER");
    mvwaddstr(ch_window1, 2, 5, "===========");

    mvwaddstr(ch_window5, 2, 7, msg1.name);
    mvwaddstr(ch_window5, 3, 6, msg1.id);
    
    mvwaddstr(ch_window4, 1, 6, "FUNCTION");
    mvwaddstr(ch_window4, 2, 5, "===========");
    mvwaddstr(ch_window4, 3, 5, "私聊");
    mvwaddstr(ch_window4, 4, 5, "群聊");
    mvwaddstr(ch_window4, 5, 5, "发送表情");
    mvwaddstr(ch_window4, 6, 5, "查看聊天记录");
    mvwaddstr(ch_window4, 7, 5, "删除聊天记录");
    mvwaddstr(ch_window4, 8, 5, "发送文件");
    mvwaddstr(ch_window4, 9, 5, "修改昵称");
    mvwaddstr(ch_window4, 10, 5, "修改密码");
    mvwaddstr(ch_window4, 11, 5, "切换用户");

    wrefresh(new_window1);
    wrefresh(ch_window3);
    wrefresh(ch_window5);
    wrefresh(new_window5);
    wrefresh(ch_window1);
    wrefresh(ch_window2);
    wrefresh(ch_window4);
 
    mvwaddstr(ch_window4, flag, 3, "=>");
    wrefresh(ch_window4);


                     break;

	    }
	    case 11:
	    {
                clear();
                refresh();
                new_window2 = newwin(13, 50, 5, 30);
                box(new_window2, 0, 0);
                mvwaddstr(new_window2, 3, 22, "GOODBYE");
                mvwaddstr(new_window2, 5, 22, msg1.name);
		wrefresh(new_window2);

		msg1.action = QUIT;
		write(new_fd, &msg1, sizeof(Node));

	        goto EXIT_CHATINTERFACE;
	        break;
	    }
	}
    }

    EXIT_CHATINTERFACE:

    curs_set(1);
    wgetch(new_window1);

}