/* linphone Copyright (C) 2010 Belledonne Communications SARL This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /** * @defgroup chatroom_tuto Chat room and messaging * @ingroup tutorials *This program is a _very_ simple usage example of liblinphone, *desmonstrating how to send/receive SIP MESSAGE from a sip uri identity passed from the command line. *
Argument must be like sip:jehan@sip.linphone.org . *
*ex chatroom sip:jehan@sip.linphone.org *
*@include chatroom.c * */ #ifdef IN_LINPHONE #include "linphonecore.h" #else #include "linphone/linphonecore.h" #endif #include static bool_t running=TRUE; static void stop(int signum){ running=FALSE; } /** * function invoked to report file transfer progress. * */ static void file_transfer_progress_indication(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, size_t progress) { const LinphoneAddress* from_address = linphone_chat_message_get_from(message); const LinphoneAddress* to_address = linphone_chat_message_get_to(message); char *address = linphone_chat_message_is_outgoing(message)?linphone_address_as_string(to_address):linphone_address_as_string(from_address); printf(" File transfer [%d%%] %s of type [%s/%s] %s [%s] \n", (int)progress ,(linphone_chat_message_is_outgoing(message)?"sent":"received") , content->type , content->subtype ,(linphone_chat_message_is_outgoing(message)?"to":"from") , address); free(address); } /** * function invoked when a file transfer is received. **/ static void file_transfer_received(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, const char* buff, size_t size){ FILE* file=NULL; if (!linphone_chat_message_get_user_data(message)) { /*first chunk, creating file*/ file = fopen("receive_file.dump","wb"); linphone_chat_message_set_user_data(message,(void*)file); /*store fd for next chunks*/ } else { /*next chunk*/ file = (FILE*)linphone_chat_message_get_user_data(message); if (size==0) { printf("File transfert completed\n"); linphone_chat_room_destroy(linphone_chat_message_get_chat_room(message)); linphone_chat_message_destroy(message); fclose(file); running=FALSE; } else { /* store content on a file*/ if (fwrite(buff,size,1,file)==-1){ ms_warning("file_transfer_received() write failed: %s",strerror(errno)); } } } } char big_file [128000]; /* * function called when the file transfer is initiated. file content should be feed into object LinphoneContent * */ static void file_transfer_send(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, char* buff, size_t* size){ int offset=-1; if (!linphone_chat_message_get_user_data(message)) { /*first chunk*/ offset=0; } else { /*subsequent chunk*/ offset = (int)((long)(linphone_chat_message_get_user_data(message))&0x00000000FFFFFFFF); } *size = MIN(*size,sizeof(big_file)-offset); /*updating content->size with minimun between remaining data and requested size*/ if (*size==0) { /*end of file*/ return; } memcpy(buff,big_file+offset,*size); /*store offset for next chunk*/ linphone_chat_message_set_user_data(message,(void*)(offset+*size)); } /* * Call back to get delivery status of a message * */ static void linphone_file_transfer_state_changed(LinphoneChatMessage* msg,LinphoneChatMessageState state,void* ud) { const LinphoneAddress* to_address = linphone_chat_message_get_to(msg); char *to = linphone_address_as_string(to_address); printf("File transfer sent to [%s] delivery status is [%s] \n" , to , linphone_chat_message_state_to_string(state)); free(to); } /* * Call back called when a message is received */ static void message_received(LinphoneCore *lc, LinphoneChatRoom *cr, LinphoneChatMessage *msg) { const LinphoneContent *file_transfer_info = linphone_chat_message_get_file_transfer_information(msg); printf ("Do you really want to download %s (size %ld)?[Y/n]\nOk, let's go\n", file_transfer_info->name, (long int)file_transfer_info->size); linphone_chat_message_start_file_download(msg, linphone_file_transfer_state_changed); } LinphoneCore *lc; int main(int argc, char *argv[]){ LinphoneCoreVTable vtable={0}; const char* dest_friend=NULL; int i; const char* big_file_content="big file"; LinphoneChatRoom* chat_room; LinphoneContent content; LinphoneChatMessage* chat_message; /*seting dummy file content to something*/ for (i=0;i