/soc/2013/ashmew2/filetransferX: ee53a19b530f: Sending experimen...

Ashish Gupta ashmew2 at gmail.com
Sun Sep 8 04:46:53 EDT 2013


Changeset: ee53a19b530ff97ca1a24b7208c031a7ee850a04
Author:	 Ashish Gupta <ashmew2 at gmail.com>
Date:	 2013-09-05 21:28 +0530
Branch:	 filetransferX
URL: https://hg.pidgin.im/soc/2013/ashmew2/filetransferX/rev/ee53a19b530f

Description:

Sending experimental now.

diffstat:

 libpurple/protocols/jabber/google/google_session.c |  229 +++++++++++++++++---
 1 files changed, 195 insertions(+), 34 deletions(-)

diffs (truncated from 373 to 300 lines):

diff --git a/libpurple/protocols/jabber/google/google_session.c b/libpurple/protocols/jabber/google/google_session.c
--- a/libpurple/protocols/jabber/google/google_session.c
+++ b/libpurple/protocols/jabber/google/google_session.c
@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
-
+#include<stdlib.h>
 #include "internal.h"
 #include "debug.h"
 #include "google_session.h"
@@ -38,8 +38,10 @@ typedef enum {
 	GTALK_XFER_CHANNEL_SENT_HTTP_GET,
 	GTALK_XFER_CHANNEL_HTTP_GET_RECEIVED,
 	GTALK_XFER_CHANNEL_HTTP_OK_SENT,
-	GTALK_XFER_CHANNEL_PARSED_HTTP_RESPONSE_HEADER
-	
+	GTALK_XFER_CHANNEL_HTTP_SENDING,
+	GTALK_XFER_CHANNEL_HTTP_RECEIVING,
+	GTALK_XFER_CHANNEL_HTTP_TRANSFERRING,
+	GTALK_XFER_CHANNEL_PARSED_HTTP_RESPONSE_HEADER	
 } GoogleXferChannelState;
 
 typedef struct {
@@ -57,6 +59,8 @@ typedef struct {
 	gint stream_id;
 	gboolean candidates_gathered;
 	gboolean pair_selected;
+	gboolean file_opened;
+	FILE *file_to_send;
 	GList *remote_share_candidates; /* lists of PurpleMediaCandidate OR NiceCandidates(maybe?)*/
 	GList *local_share_candidates;	
 } GoogleXferSessionData; 
@@ -447,7 +451,7 @@ gint gtalk_xfer_init_agent(GoogleSession
 			   G_CALLBACK (cb_nice_component_state_changed), session);
 	
 	g_signal_connect (G_OBJECT (share_session->share_agent), "reliable-transport-writable",
-			  G_CALLBACK (cb_nice_component_writable), NULL);
+			  G_CALLBACK (cb_nice_component_writable), share_session);
 	
 	share_session->share_agent = agent;
 	share_session->stream_id = stream_id;
@@ -501,7 +505,7 @@ jabber_google_relay_response_session_ini
 								  relay_username, relay_password, NICE_RELAY_TYPE_TURN_TLS);
 			}
 
-			nice_agent_attach_recv(agent, stream_id, 1, g_main_context_default(),
+/*			nice_agent_attach_recv(agent, stream_id, 1, g_main_context_default(),
 					       cb_nice_recv, share_session);
 
 			g_signal_connect(G_OBJECT(agent), "candidate-gathering-done",
@@ -1786,7 +1790,7 @@ gtalk_xfer_read(guchar **out_buffer, Pur
 		buffer += tmp;
 		purple_circ_buffer_mark_read(share_session->buffer, tmp);
 	}
-	purple_debug_info("google_session", "gtalk_xfer_read returning : %ld\n",size);
+	purple_debug_info("google_session", "Inside gtalk_xfer_read,  returning : %ld\n",size);
 	return size;
 }
 
@@ -1800,7 +1804,8 @@ cb_nice_recv(NiceAgent *agent, guint str
 	PurpleXfer *xfer = share_session->xfer;
 	gchar *curr = buf;
 	
-	purple_debug_info("google_session", "Inside cb_nice_recv\n");
+	purple_debug_info("google_session", "Inside cb_nice_recv. Received : %s", buf);
+
 	if(purple_xfer_get_type(xfer) == PURPLE_XFER_SEND) {
 		purple_debug_info("google_session", "Inside cb_nice_recv for XFER_SEND.Received : %s\n", buf);
 		/*Most probably we have received an HTTP GET Request from GTalk here. 
@@ -1814,40 +1819,86 @@ cb_nice_recv(NiceAgent *agent, guint str
 		gchar *response = g_strdup_printf( "HTTP/1.1 200\r\n"
 						   "Connection: Keep-Alive\r\n"
 						   "Content-Length: %llu\r\n"
-						   "Content-Type: application/octet-stream\r\n",  
-						   xfer->size );
+						   "Content-Type: application/octet-stream\r\n\r\n",
+						   xfer->size);
 		
-		send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1, 
+		int fread_ret;
+		file_buf = g_malloc(65536);
+
+		send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
 					   strlen(response), response);
 		purple_debug_info("google_session", "Sending response : %s\n", response);
 		purple_debug_info("google_session", "For sending 200 OK, send_ret is : %d",send_ret);
-		purple_debug_info("google_session", "Filesize is : %llu", share_session->filesize);
+		purple_debug_info("google_session", "Filesize is : %llu", xfer->size);
+		/*Share_session->fileize should probably have xfer->size's value*/
 		share_session->channel_state = GTALK_XFER_CHANNEL_HTTP_OK_SENT;
+		share_session->channel_state = GTALK_XFER_CHANNEL_HTTP_SENDING;
+		purple_debug_info("google_session", "\nChanged to _SENDING, Bytes remaining: %llu",
+				  purple_xfer_get_bytes_remaining(xfer));
+		fread_ret = fread(file_buf, 1, 65530, file_ptr);
+		
+		send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+					   fread_ret, file_buf);
+
+		purple_debug_info("google_session", "\nInitiating File Send..send_ret is : %d",send_ret);
+		xfer->bytes_sent += send_ret;
+		xfer->bytes_remaining -= send_ret;
+		purple_debug_info("google_session", "\nBytes remaining: %llu",
+				  purple_xfer_get_bytes_remaining(xfer));
+
+/*		if(file_ptr) {
+			share_session->file_opened = 1;
+			share_session->file_to_send = file_ptr;
+			cb_nice_component_writable(share_session->share_agent, share_session->stream_id,
+						   1, share_session);
+
+		}
+		else
+			purple_debug_info("google_session", "FAILED TO OPEN FILE TO SEND! ABORTING!\n");
+*/		
 /*TODO: Remove the following xfer_send part, this has been moved to cb_nice_component_changed*/
-/*
-		if(file_ptr)
+	
+		/*		if(file_ptr)
 			{
-				file_buf = g_malloc(share_session->filesize);
-				fread (file_buf, 1, share_session->filesize, file_ptr);
+				if(!file_ptr) 
+					purple_debug_info("google_session", "file_ptr is NULL!\n");
+				else {
+					share_session->file_to_send = file_ptr;
+					purple_debug_info("google_session", "file_ptr is not NULL!\n");
+				}
+				file_buf = g_malloc(xfer->size);
+				fread (file_buf, 1, share_session->xfer->size, file_ptr);
+				purple_debug_info("google_session", "file_buf is %s\n",file_buf);
 			}
 		else
 			purple_debug_info("google_session", "FILE IS NULL :@ \n");
 
 		if(file_buf)
 			{
-				int len=0;
-				while(len < xfer->size)
+				int bytes_left = xfer->bytes_remaining;
+				int len=1; 
+				while(len>0)
 					{
+						len = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+								       bytes_left, file_buf + xfer->size - bytes_left);
 						purple_debug_info("google_session", "Current Value of len : %d\n",len);
-						len += nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
-								       share_session->filesize - len, file_buf + len);
+						if(len>0) {
+							xfer->bytes_remaining -= len;
+							xfer->bytes_sent +=len;
+						}
+						if(xfer->bytes_remaining == 0)
+							break;
 					}
-				purple_debug_info("google_session", "Finished the File Sending..");
-				nice_agent_send(share_session->share_agent, share_session->stream_id, 1, 
-						strlen("\r\n\r\n"), "\r\n\r\n");
-				purple_debug_info("google_session", "Ended everything!..\n");
-			}
-		else
+				if(xfer->bytes_remaining == 0)
+					nice_agent_send(share_session->share_agent, share_session->stream_id, 1, 
+							strlen("\r\n\r\n"), "\r\n\r\n");
+				
+				purple_debug_info("google_session","Just after the loop, bytes_remaining = %llu value of len is : %d", xfer->bytes_remaining, len);
+				purple_debug_info("google_session", "Should probably wait for writable signal again..\n");
+/*				nice_agent_send(share_session->share_agent, share_session->stream_id, 1, 
+				strlen("\r\n\r\n"), "\r\n\r\n");*/
+	
+	/*		else
 			purple_debug_info("google_session", "file_buf is NULL\n");
 		/*TODO: Immediately following the response, we should be sending out our file as bytes :D */
 /*	                             	purple_xfer_start(xfer, -1, NULL, 0);                               
@@ -2001,8 +2052,13 @@ void cb_nice_component_state_changed(Nic
 		FILE *file_ptr = fopen(share_session->xfer->local_filename, "rb");
 
 		purple_debug_info("google_session", "STATE_READY for XFER_SEND! in cb_component_state_changed.");
-		purple_debug_info("google_session", "xfer->local_filename = %s\n",share_session->xfer->local_filename);		
+		purple_debug_info("google_session", "xfer->local_filename = %s\n",share_session->xfer->local_filename);
 		if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_OK_SENT) {
+			cb_nice_component_writable(share_session->share_agent, share_session->stream_id, 1,
+				share_session);  
+
+/*TODO: Should this code go to the cb_nice_component_writable?*/
+/*
 			if(file_ptr)
 				{
 					purple_debug_info("google_session", "Creating a file_buf of size : %llu\n",share_session->xfer->size);
@@ -2034,7 +2090,10 @@ void cb_nice_component_state_changed(Nic
 			else
 				purple_debug_info("google_session", "file_buf is NULL\n");
 		}
+
+		}*/
 	}
+
 /*	else if(state == NICE_COMPONENT_STATE_CONNECTED && !share_session->pair_selected) {/*
 		GSList *remote_candids = nice_agent_get_remote_candidates(agent, stream_id, component_id);
 		GSList *local_candids = share_session->local_share_candidates; /*TODO: Done for grabbing the relay candidate*/
@@ -2097,20 +2156,121 @@ void cb_nice_component_state_changed(Nic
 		}
 		purple_debug_info("google_session", "***********\n");
 */
+	}
 }
 
-
 /*TODO: Add more stuff related to state changes here*/
 
 void cb_nice_component_writable(NiceAgent *agent, guint stream_id, guint component_id,
 				gpointer user_data)  
 {
+	GoogleXferSessionData *share_session = (GoogleXferSessionData *)user_data;
+	PurpleXfer *xfer = share_session->xfer;
+
 	purple_debug_info("google_session", "__WRITABLE NOW ! (Inside cb_nice_component_writable\n");
+
+	if(share_session)
+		purple_debug_info("gogo", "NON NULL SESSION!\n");
+	else
+		purple_debug_info("goog", "NULL SESSION\n");
+
+	if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_SENDING) {
+		FILE *file_ptr = fopen(xfer->local_filename, "rb");
+		int send_ret, fread_ret;
+		char *file_buf = g_malloc(65536);
+
+		purple_debug_info("google_session", "Inside writable(), SENDING_STATE!\n");
+		fseek(file_ptr, purple_xfer_get_bytes_sent(xfer),SEEK_SET);
+		purple_debug_info("google_session", "ftell gives : %ld\n", ftell(file_ptr));
+		
+		if(purple_xfer_get_bytes_remaining(xfer) == 0) {
+			purple_debug_info("google_session", "\nZero bytes remaining!!");
+			nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+					strlen("\r\n\r\n"), "\r\n\r\n");
+			return;
+		}
+
+		fread_ret = fread(file_buf, 1, 65530, file_ptr);
+		send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+					   fread_ret, file_buf);
+
+/*send_ret = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+  purple_xfer_get_bytes_remaining(xfer), file_ptr);*/
+		purple_debug_info("google_session", "\n_SENDING state..send_ret is : %d",send_ret);
+		if(send_ret>0) {
+			xfer->bytes_sent += send_ret;
+			xfer->bytes_remaining -= send_ret;
+		}
+		purple_debug_info("google_session", "\nBytes remaining: %llu",
+				  purple_xfer_get_bytes_remaining(xfer));
+		free(file_buf);
+//TODO??		if (purple_xfer_get_bytes_remaining(xfer) == 0)
+//			exit(0);
+
+                //TODO: Remove redundant part
+		/*if(share_session->file_to_send)
+			{
+				gchar *file_buf = g_malloc(share_session->xfer->size);
+				fread (file_buf, 1, share_session->xfer->size, share_session->file_to_send);
+				purple_debug_info("google_session", "file_buf is %s\n",file_buf);
+
+				if(file_buf) {
+					int bytes_left = share_session->xfer->bytes_remaining;
+					int len=1; 
+					while(len>0) {
+						len = nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+								      bytes_left, file_buf + share_session->xfer->size - bytes_left);
+						purple_debug_info("google_session", "Current Value of len : %d\n",len);
+					}
+					purple_debug_info("google_session", "Just after the loop, value of len is : %d", len);
+					purple_debug_info("google_session", "Should probably wait for writable signal again..\n");
+/*				nice_agent_send(share_session->share_agent, share_session->stream_id, 1, 
+                                          strlen("\r\n\r\n"), "\r\n\r\n");*//*
+									      }
+				
+				}*/
+	}
+
+	if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_OK_SENT)
+		share_session->channel_state = GTALK_XFER_CHANNEL_HTTP_TRANSFERRING;
+
+	purple_debug_info("google_session", "GTALK_XFER_CHHANEL_TREANSFERRING\n");
+
+	if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_TRANSFERRING)
+		{
+			FILE *f_in;
+			PurpleXfer *xfer = share_session->xfer;
+			char file_buf[65535] = "\0";
+			gint send_ret, len_buf;
+
+			if(share_session->file_opened)
+				f_in = share_session->file_to_send;
+			if(f_in == xfer->dest_fp)
+				purple_debug_info("google_session", "It's the same FILE POINTER !\n");
+			
+			len_buf = fread(file_buf + xfer->bytes_sent, xfer->bytes_remaining < 65535 ? xfer->bytes_remaining : 65535, 1, f_in);



More information about the Commits mailing list