summaryrefslogtreecommitdiffstats
path: root/friendfinder/handler.c
blob: b8dea19b07b9bad0f38b8165212bafd24d33e2ae (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "pthread.h"

#include "msg_sender.h"
#include "sender.h"
#include "receiver.h"
#include "handler.h"

pthread_t msg_thread, sender_thread, receiver_thread;
int msg_start = 0, sender_start = 0, receiver_start = 0;

void init_msg_thread(char *from, char *to)
{
	if (msg_start == 0)
	{
		struct nick *nicknames = (struct nick*) malloc(sizeof(struct nick));
		nicknames->from = from;
		nicknames->to = to;

		if (pthread_create(&msg_thread, NULL, msg_main_loop, (void*) nicknames) == 0)
			printf("HANDLER: msg thread init...\n");

		else
		{
			printf("HANDLER: msg thread init failure...\n");
			return;
		}

		msg_start = 1;
	}
}

void init_receiver_thread(char *from)
{
	if (receiver_start == 0)
	{
		char* receiver_from = (char*) malloc(sizeof(from));
		memcpy(receiver_from, from, strlen(from));

		if (pthread_create(&receiver_thread, NULL, receiver_main, (void*) from) == 0)
		{	
			pthread_detach(receiver_thread);
			printf("HANDLER: receiver thread init...\n");
		}

		else
		{
			printf("HANDLER: receiver thread init failure...\n");
			return;
		}

		
		receiver_start = 1;
	}
}

void init_sender_thread(char *from)
{
	if (sender_start == 0)
	{
		char* sender_from = (char*) malloc(sizeof(from));
		memcpy(sender_from, from, strlen(from));
			
		if (pthread_create(&sender_thread, NULL, sender_main, (void*) from) == 0)
		{
			pthread_detach(sender_thread);
			printf("HANDLER: sender thread init...\n");
		}

		else
		{
			printf("HANDLER: sender thread init failure...\n");
			return;
		}

		sender_start = 1;
	}
}

void close_threads()
{
	if (receiver_start != 0)
		disconnect_receiver();

	if (sender_start != 0)
		disconnect_sender();

	if (msg_start != 0)
		disconnect_msg_sender();
	
//	exit(0);
}