aboutsummaryrefslogtreecommitdiff
path: root/chat_request.h
blob: 6b4db03b1a17b069c724f36de769c56801ed91ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Localchat
 * Ben Burwell
 *
 * Send chat request
 */


void send_chat_request(char * username) {

	int i;
	char ip[16];

	pthread_mutex_lock(&peer_table_lock);

	for (i = 0; i < num_peers_in_table; i++) {
		if (strcmp(peers[i].username, username) == 0) {
			strcpy(ip, peers[i].ip);
		}
	}

	pthread_mutex_unlock(&peer_table_lock);

	struct sockaddr_in  server_addr;
	char                out_buf[GLOBAL_MSG_LENGTH];
	char                in_buf[GLOBAL_MSG_LENGTH];
	int                 retcode;

	// create the socket
	client_s = socket(AF_INET, SOCK_STREAM, 0);

	if (client_s < 0) {
		printf("Network error, quitting... \n");
		exit(-1);
	}

	// fill in server info
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(CHAT_PORT);
	server_addr.sin_addr.s_addr = inet_addr(ip);

	retcode = connect(client_s, (struct sockaddr *)&server_addr, sizeof(server_addr));

	if (retcode < 0) {
		printf("Network error, quitting... \n");
		exit(-1);
	}

	// send the session request
	strcpy(out_buf, "SESREQ");
	send(client_s, out_buf, strlen(out_buf), 0);

	retcode = recv(client_s, in_buf, sizeof(in_buf), 0);

	if (strcmp(in_buf, "SESANS:Y") == 0) {
		printf("Request accepted, type /q to leave. \n");

		// set my in_chat flag
		strcpy(in_chat, "Y");

		pthread_t recv_thread;
		pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);

		// set the user prompt
		strcpy(prompt, "chat>");

	} else {
		printf("Session declined. \n");
	}

	return;
}