Made a callback for Friend.status change

This commit is contained in:
Maxim Biro 2013-08-08 01:46:27 -04:00
parent 225f74e844
commit f66b6ce50b
2 changed files with 30 additions and 7 deletions

View File

@ -57,6 +57,8 @@ static Friend friendlist[MAX_NUM_FRIENDS];
static uint32_t numfriends;
static void set_friend_status(int friendnumber, uint8_t status);
/* 1 if we are online
0 if we are offline
static uint8_t online; */
@ -122,7 +124,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */
if (friendlist[i].status == NOFRIEND) {
DHT_addfriend(client_id);
friendlist[i].status = FRIEND_ADDED;
set_friend_status(i, FRIEND_ADDED);
friendlist[i].crypt_connection_id = -1;
friendlist[i].friend_request_id = -1;
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
@ -149,7 +151,7 @@ int m_addfriend_norequest(uint8_t * client_id)
for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */
if(friendlist[i].status == NOFRIEND) {
DHT_addfriend(client_id);
friendlist[i].status = FRIEND_REQUESTED;
set_friend_status(i, FRIEND_REQUESTED);
friendlist[i].crypt_connection_id = -1;
friendlist[i].friend_request_id = -1;
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
@ -460,6 +462,23 @@ void m_callback_read_receipt(void (*function)(int, uint32_t))
read_receipt_isset = 1;
}
static void (*friend_status)(int, uint8_t);
static uint8_t friend_status_isset = 0;
void m_callback_friend_status(void (*function)(int, uint8_t))
{
friend_status = function;
friend_status_isset = 1;
}
static void set_friend_status(int friendnumber, uint8_t status)
{
friendlist[friendnumber].status = status;
if (friendlist[friendnumber].status == status)
return;
if (friend_status_isset)
friend_status(friendnumber, status);
}
#define PORT 33445
/* run this at startup */
int initMessenger(void)
@ -487,9 +506,9 @@ static void doFriends(void)
if (friendlist[i].status == FRIEND_ADDED) {
int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
friendlist[i].status = FRIEND_REQUESTED;
set_friend_status(i, FRIEND_REQUESTED);
else if (fr > 0)
friendlist[i].status = FRIEND_REQUESTED;
set_friend_status(i, FRIEND_REQUESTED);
}
if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
if (friendlist[i].status == FRIEND_REQUESTED) {
@ -505,7 +524,7 @@ static void doFriends(void)
friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
break;
case 3: /* Connection is established */
friendlist[i].status = FRIEND_ONLINE;
set_friend_status(i, FRIEND_ONLINE);
break;
case 4:
crypto_kill(friendlist[i].crypt_connection_id);
@ -587,7 +606,7 @@ static void doFriends(void)
if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
crypto_kill(friendlist[i].crypt_connection_id);
friendlist[i].crypt_connection_id = -1;
friendlist[i].status = FRIEND_CONFIRMED;
set_friend_status(i, FRIEND_CONFIRMED);
}
break;
}
@ -608,7 +627,7 @@ static void doInbound(void)
friendlist[friend_id].crypt_connection_id =
accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
friendlist[friend_id].status = FRIEND_CONFIRMED;
set_friend_status(friend_id, FRIEND_CONFIRMED);
}
}
}

View File

@ -197,6 +197,10 @@ void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t));
in that case, you should discard it. */
void m_callback_read_receipt(void (*function)(int, uint32_t));
/* set the callback for friend status changes
function(int friendnumber, uint8_t status) */
void m_callback_friendstatus(void (*function)(int, uint8_t));
/* run this at startup
returns 0 if no connection problems
returns -1 if there are problems */