/**
* \file Inkboard.cpp
* Source for Inkscape whiteboard functionality (Jabber communication side)
*
* \author Jonas Collaros, Steven Montgomery
* \date 5-02-2005
*/
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include "inkboard.h"
#include "dialogs/InkboardMsg.h"
/**
* Globals
*/
gchar *recipient;
LmConnection *connection;
Buddy* online_roster = NULL;
TimedMessageNode* queue_header_node = NULL;
TimedMessageNode* send_queue_header_node = NULL;
unsigned int sequence_largest_dropped = 0;
unsigned int sequence_number = 1;
gboolean logged_in = FALSE, whiteboard = FALSE, chatroom = FALSE, open_for_doc = FALSE;
/**
* This inkscape-jabber interface function connects to specified #server as #username with password #pw and resource "Inkboard".
* Also sends initial precense to server for broadcast.
*
* \param server gchar* pointer to string containing the server to connect to
* \param username gchar* pointer to string containing login name to use when connecting
* \param pw gchar* pointer to string containing the password to use when connecting
* \return 0 if connected sucessfully, and error code otherwise
*/
int connect_and_auth(gchar* server, gchar* username, gchar* pw) {
GError *error = NULL;
gchar* jid = (char*)malloc( (strlen(server) + strlen(username) + strlen("@/inkboard") + 1)
* sizeof(char));
LmMessage *m, *roster;
LmMessageNode *node, *root;
LmMessageSubType subType;
strcat(strcat(strcat(strcpy(jid,username),"@"),server),"/Inkboard");
if(DEBUG)
printf("Connecting as %s\n",jid);
connection = lm_connection_new( server );
lm_connection_set_jid(connection, jid);
if ( !lm_connection_open_and_block (connection, &error) ) {
if(DEBUG) printf("Failed to open: <literal>%s</literal>\n", error->message);
lm_message_unref(m);
free(jid);
return 1;
}
if (!lm_connection_authenticate_and_block (connection,
username, pw, "Inkboard",
&error))
{
if(DEBUG) printf("Failed to authenticate: <literal>%s</literal>\n", error->message);
lm_message_unref(m);
free(jid);
return 2;
}
// Handler for received presence message
LmMessageHandler *mh;
mh = lm_message_handler_new((LmHandleMessageFunction)presence_handler, NULL, NULL);
lm_connection_register_message_handler( connection, mh, LM_MESSAGE_TYPE_PRESENCE,
LM_HANDLER_PRIORITY_NORMAL);
// Register message handler for receiving stream error messages
mh = lm_message_handler_new((LmHandleMessageFunction)stream_error_handler, NULL, NULL);
lm_connection_register_message_handler( connection, mh, LM_MESSAGE_TYPE_STREAM_ERROR,
LM_HANDLER_PRIORITY_NORMAL);
// Register message handler for receiving chat messages
mh = lm_message_handler_new((LmHandleMessageFunction)default_handler, NULL, NULL);
lm_connection_register_message_handler( connection, mh, LM_MESSAGE_TYPE_MESSAGE,
LM_HANDLER_PRIORITY_NORMAL);
// Send initial online presence
m = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_PRESENCE,
LM_MESSAGE_SUB_TYPE_NOT_SET);
if (!lm_connection_send (connection, m, &error) ) {
if(DEBUG) printf("Send of inital precense failed: <literal>%s</literal>\n", error->message);
}else if (DEBUG_VERBOSE) {
if(DEBUG) printf("Sent initial presence message to server\n" );
}
logged_in = TRUE;
//add TCP poll timeout
lm_message_unref(m);
//Inkboard - Memory Management
free(jid);
//End Inkboard
return 0;
}
/**
* Timeout for recieved messages that have been queued. This is called and used internally to
* determine when to remove messages from the recieving queue
*
* \param data object pointer associated with this timeout
* \return false, indicating successul handling of the timeout
*/
gboolean message_timeout(gpointer data) {
TimedMessageNode* timeout_node = (TimedMessageNode*)data;
if( timeout_node->sequence > sequence_largest_dropped ) {
sequence_largest_dropped = timeout_node->sequence;
}
delete_from_queue(timeout_node, queue_header_node);
return false;
}
/**
* Timeout for messages waiting to be sent (in the send-queue). This is called and used internally to
* stagger release of change messages to prevent server flooding.
*
* \param data object pointer associated with this timeout
* \return false, indicating successul handling of the timeout, or true, indicating an error in sending
*/
gboolean send_timeout(gpointer data) {
//stop if disconnected
if(whiteboard == FALSE) {
if(DEBUG_VERBOSE) printf("...stopped queue.\n");
return false;
}
if(DEBUG_VERBOSE) {
printf(".");
fflush(stdout);
}
//check if connection is still on:
if( lm_connection_get_state(connection) != LM_CONNECTION_STATE_OPEN ) {
if(DEBUG) printf("Error: Connection lost!\n");
error_connection( "Connection has been lost to the Jabber server." );
if(DEBUG) printf("...stopped queue.\n");
return true;
}
g_timeout_add( SEND_TIMEOUT, (GSourceFunc)send_timeout, NULL );
if( send_queue_header_node == NULL )
return false;
//send as either a repeatable or non-repeatable change
if( send_queue_header_node->repeatable == TRUE ) {
send_ink_message(INK_CHANGE_REPEATABLE, send_queue_header_node->sequence,
send_queue_header_node->message_body, NULL);
}else {
send_ink_message(INK_CHANGE_NOT_REPEATABLE, send_queue_header_node->sequence,
send_queue_header_node->message_body, NULL);
}
TimedMessageNode* temp = send_queue_header_node->next_node;
send_queue_header_node->set_next_node(NULL);
insert_into_queue(send_queue_header_node, FALSE);
if( DEBUG_VERBOSE && !DEBUG_ABSURDLY_VERBOSE ) {
printf("(%u)[%s]<Document>\n", send_queue_header_node->sequence,
send_queue_header_node->sender);
}
send_queue_header_node = temp;
if( DEBUG_ABSURDLY_VERBOSE ) {
printf("Queue:\n");
print_queue(stdout,queue_header_node);
}
return false;
}
/**
* Closes connection and sends unavailable presence
*
*/
void disconnect_from_server( ) {
GError *error = NULL;
/*
Send disconnect presence
*/
LmMessage *m;
m = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_PRESENCE,
LM_MESSAGE_SUB_TYPE_UNAVAILABLE);
if (!lm_connection_send (connection, m, &error) )
if(DEBUG) printf("Send of unavailable precense failed: <literal>%s</literal>\n", error->message);
lm_message_unref(m);
lm_connection_close (connection, NULL);
lm_connection_unref (connection);
}
/**
* Sets the JID of the currently connected sharing user, to which change messages should be sent.
*
* \param const char* recipientFullJID Full JID of intended recipient
* \return int 0 upon sucessful. Anything else if failed.
*/
int setRecipient(const char* recipientFullJID) {
if(DEBUG) printf("setRecipient(%s)\n", recipientFullJID);
if(recipientFullJID == NULL)
recipient = NULL;
recipient = g_strdup(recipientFullJID);
return 0;
}
/**
* This function will construct and send out any inkscape messages based on the specified values. The status of the connection
* should be checked before using this function.
*
* \param message_type an INK_MESSAGE_TYPES enumerated value, indicating what kind of message to send (see Inkboard.h)
* \param sequence unsigned int that is the sequence number to attach to this message. this field may not be needed, depending on the message type
* \param message char* that is the body of this change message
* \param recipient_JID const char* for the target recipient of this message. if this is NULL, it will use the default (the global recipient)
*
* \return 0 upon sucessful, a >0 error code otherwise
*/
int send_ink_message(INK_MESSAGE_TYPES message_type, unsigned int sequence, char* message,
const char* recipient_JID)
{
if(DEBUG) {
if( recipient_JID == NULL ) {
if(DEBUG) printf("Sending message (type %i) to connected user/chatroom...\n",message_type);
} else {
if(DEBUG) printf("Sending message (type %i) to %s...\n",message_type,recipient_JID);
}
}
LmMessage *m;
GError *er
inkboard2.tar.gz_xmpp
版权申诉
68 浏览量
2022-09-22
18:39:45
上传
评论
收藏 17KB GZ 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
最新资源
- 院校-课程网盘链接提取码下载 .txt
- SamsungeShop.apk
- 32Meg x 8, 16Meg x16 256Mb SYNCHRONOUS DRAM手册
- 手写字识别-基于Transformer实现手写字文本识别-附项目源码-优质项目实战.zip
- 基于 Swin-Transformer 网络对 眼底血管2分类识别项目【数据集+代码+训练好的所有结果】
- 采用P-f和Q-V滞控的去中心化逆变器型交流微电网的模拟(Simulink仿真实现)
- 彩虹聚合二级域名DNS管理系统源码v1.3
- 【TOF相机笔记3】Simulink使用方法
- 算法部署-基于C++和Python使用ONNXRuntime部署RT-DETR目标检测算法-附项目源码-优质项目实战.zip
- Bitree.cpp
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈