blob: 226432f4ef88c83270db19f5f6a36f61b93321bd (
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/**
* Localchat
* Ben Burwell
*
* Main Program
*/
// include necessary libraries
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
// define constants
#define DEBUG 0
#define MAX_NUM_PEERS 100
#define CMD_PORT 6060
#define CHAT_PORT 6061
#define BROADCAST_IP "192.168.130.255"
#define GLOBAL_MSG_LENGTH 1024
#define REQUEST_TIMEOUT 10
#define SESSION_TIMEOUT 30
#define RR 0
// structs
#include "peer.h"
// global variables
struct peer peers[MAX_NUM_PEERS];
int num_peers_in_table = 0;
pthread_mutex_t peer_table_lock;
char my_ip[64];
char in_chat[4] = "N";
const char * username;
char command[256];
char prompt[16] = ">";
int client_s;
char in_buf[GLOBAL_MSG_LENGTH];
char out_buf[GLOBAL_MSG_LENGTH];
struct sockaddr_in client_addr;
int accepted_client;
struct sockaddr_in accepted_addr;
int respond_to_chat_request = 0;
// include functions
#include "clean_table.h"
#include "online.h"
#include "own_ip.h"
#include "status.h"
#include "parse.h"
#include "receive.h"
#include "check_user_name.h"
#include "loading.h"
#include "chat_acceptor.h"
#include "chat_request.h"
#include "user_command.h"
int main(int argc, char const *argv[]) {
if (argc != 2) {
printf("Usage: %s [username]\n", argv[0]);
return -1;
}
// figure out IP address
get_own_ip();
// start some threads we need
start_receive_thread();
start_clean_table_thread();
// send an ONLINE message
send_online_broadcast();
// wait for responses
loading();
// check that the username they have requested
// is not already in use
if (check_user_name(argv[1])) {
printf("Sorry, the username you selected is already in use. \n");
printf("Please choose another. \n\n");
exit(1);
}
// now we can set the username field
username = argv[1];
// start broadcasting statuses
start_status_thread();
// we can accept chats
start_accepting_chat_requests();
// enter the user input loop
while (1) {
printf("%s ", prompt);
fgets(command, 256, stdin);
process_user_command();
}
return 0;
}
|