/soc/2013/ashmew2/filetransferX: 05da15d4e2cf: Setting up for se...
Ashish Gupta
ashmew2 at gmail.com
Fri Aug 23 03:21:22 EDT 2013
Changeset: 05da15d4e2cf008e186eab38cf0211b0382d3cb1
Author: Ashish Gupta <ashmew2 at gmail.com>
Date: 2013-08-23 12:50 +0530
Branch: filetransferX
URL: https://hg.pidgin.im/soc/2013/ashmew2/filetransferX/rev/05da15d4e2cf
Description:
Setting up for sending a file via HTTP
diffstat:
libpurple/protocols/jabber/google/google_session.c | 128 ++++++++++++++++++--
libpurple/protocols/jabber/google/google_session.h | 1 +
2 files changed, 116 insertions(+), 13 deletions(-)
diffs (218 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
@@ -36,7 +36,10 @@
typedef enum {
GTALK_XFER_CHANNEL_INITIALIZED = 0,
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
+
} GoogleXferChannelState;
typedef struct {
@@ -1797,9 +1800,63 @@ cb_nice_recv(NiceAgent *agent, guint str
PurpleXfer *xfer = share_session->xfer;
gchar *curr = buf;
-/* purple_debug_info("google_session", "Received : \n%s\n", buf);*/
-
- if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
+ purple_debug_info("google_session", "Inside cb_nice_recv\n");
+ 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.
+ TODO: Add a parser to make sure it's not bad data*/
+
+ share_session->channel_state = GTALK_XFER_CHANNEL_HTTP_GET_RECEIVED;
+ /*We should send a 200 OK back now*/
+ int send_ret;
+ char *file_buf = NULL;
+ FILE *file_ptr = fopen(xfer->local_filename, "rb");
+ 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 );
+
+ 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);
+ share_session->channel_state = GTALK_XFER_CHANNEL_HTTP_OK_SENT;
+/*TODO: Remove the following xfer_send part, this has been moved to cb_nice_component_changed*/
+/*
+ if(file_ptr)
+ {
+ file_buf = g_malloc(share_session->filesize);
+ fread (file_buf, 1, share_session->filesize, file_ptr);
+ }
+ else
+ purple_debug_info("google_session", "FILE IS NULL :@ \n");
+
+ if(file_buf)
+ {
+ int len=0;
+ while(len < xfer->size)
+ {
+ 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);
+ }
+ 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
+ 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);
+ */
+
+ }
+ /* purple_xfer_start(xfer, -1, NULL, 0); */
+ /* TODO: Something to do */
+ else if (purple_xfer_get_type(xfer) == PURPLE_XFER_RECEIVE) {
if (share_session->channel_state ==
GTALK_XFER_CHANNEL_SENT_HTTP_GET) {
gchar *tmp;
@@ -1819,7 +1876,7 @@ cb_nice_recv(NiceAgent *agent, guint str
/* got a 404 (file not found) stop... */
/* TODO: Terminate the session gracefully as 404 happened? That means source_url
is bugged. Starting the Xfer over again might be the only option. */
- }
+ }
}
/* TODO: check Content-Length and other headers too? */
@@ -1830,7 +1887,7 @@ cb_nice_recv(NiceAgent *agent, guint str
purple_debug_info("google_session", "Headers End here..(Blank Line encountered)\n");
share_session->channel_state =
- GTALK_XFER_CHANNEL_PARSED_HTTP_RESPONSE_HEADER;
+ GTALK_XFER_CHANNEL_PARSED_HTTP_RESPONSE_HEADER;
purple_xfer_start(xfer, -1, NULL, 0);
}
/* otherwise the remaining headers would come in the next package
@@ -1840,10 +1897,10 @@ cb_nice_recv(NiceAgent *agent, guint str
static int curlen = 0;
/* write data to transfer */
curlen+=len;
-
+
purple_debug_info("google-share",
"got %d bytes to write to transfer, CURLEN: %d\n", len, curlen);
-
+
if (len <= purple_xfer_get_bytes_remaining(xfer)) {
purple_circ_buffer_append(share_session->buffer, curr, len);
purple_xfer_prpl_ready(xfer);
@@ -1853,14 +1910,11 @@ cb_nice_recv(NiceAgent *agent, guint str
purple_xfer_cancel_remote(xfer);
}
}
- } else 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);
-
- /*TODO: Something to do*/
}
+ else
+ purple_debug_info("google_session", "XFER_TYPE NOT KNOWN!");
}
-
char *nice_component_state_to_str(guint state)
{
switch(state) {
@@ -1943,7 +1997,43 @@ void cb_nice_component_state_changed(Nic
}
if( purple_xfer_get_type(share_session->xfer) == PURPLE_XFER_SEND && state == NICE_COMPONENT_STATE_READY) {
- purple_debug_info("google_session", "STATE_READY for XFER_SEND!");
+ char *file_buf = NULL;
+ 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);
+ if(share_session->channel_state == GTALK_XFER_CHANNEL_HTTP_OK_SENT) {
+ if(file_ptr)
+ {
+ purple_debug_info("google_session", "Creating a file_buf of size : %llu\n",share_session->xfer->size);
+ file_buf = g_malloc(share_session->xfer->size);
+ fread (file_buf, 1, share_session->filesize, file_ptr);
+ }
+ else
+ purple_debug_info("google_session", "FILE IS NULL :@ \n");
+
+ if(file_buf)
+ {
+ size_t len=0;
+ purple_debug_info("google_session", "Before sending, xfer->size is %llu\n", share_session->xfer->size);
+ purple_debug_info("google_sesssion", "Before sending, xfer->fd is %d\n", share_session->xfer->fd);
+
+ while(len < share_session->xfer->size)
+ {
+ purple_debug_info("google_session", "Current Value of len : %llu\n",len);
+ len += nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+ 1300, file_buf + len);
+ nice_agent_send(share_session->share_agent, share_session->stream_id, 1,
+ strlen("\r\n"), "\r\n");
+ }
+ 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
+ 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);
@@ -2016,6 +2106,11 @@ void cb_nice_component_writable(NiceAgen
gpointer user_data)
{
purple_debug_info("google_session", "__WRITABLE NOW ! (Inside cb_nice_component_writable\n");
+ /*TODO: Whenever nice_agent_send returns -1, that means that either something is VERY WRONG,
+ or the output buffer is full. If the output buffer is full, that means we need to wait for the
+ reliable-transport-writable signal in order to resume writing to the xfer. Should probbaly set up a loop
+ here or something*/
+
}
void cb_new_selected_pair(NiceAgent *agent, guint stream_id, guint component_id,
@@ -2243,6 +2338,7 @@ gtalk_xfer_send(PurpleConnection *gc, co
purple_xfer_set_init_fnc(xfer, gtalk_xfer_init);
purple_xfer_set_request_denied_fnc(xfer,
gtalk_xfer_request_denied);
+ purple_xfer_set_write_fnc(xfer, gtalk_xfer_write);
xfer->data = session;
share_session->xfer = xfer;
@@ -2361,6 +2457,12 @@ gtalk_xfer_request_denied(PurpleXfer *xf
}
void
+gtalk_xfer_write(const guchar *buffer, size_t size, PurpleXfer *xfer)
+{
+ purple_debug_info("google_session", "Inside gtalk_xfer_write..\n");
+}
+
+void
gtalk_xfer_init(PurpleXfer *xfer)
{
GoogleSession *session = (GoogleSession *)(xfer->data);
diff --git a/libpurple/protocols/jabber/google/google_session.h b/libpurple/protocols/jabber/google/google_session.h
--- a/libpurple/protocols/jabber/google/google_session.h
+++ b/libpurple/protocols/jabber/google/google_session.h
@@ -57,6 +57,7 @@ void gtalk_xfer_send(PurpleConnection *g
void gtalk_xfer_init(PurpleXfer *xfer);
void gtalk_xfer_request_denied(PurpleXfer *xfer);
gssize gtalk_xfer_read(guchar **out_buffer, PurpleXfer *xfer);
+void gtalk_xfer_write(const guchar *buffer, size_t size, PurpleXfer *xfer);
void gtalk_xfer_start(PurpleXfer *xfer);
void gtalk_xfer_end(PurpleXfer *xfer);
void gtalk_xfer_send_transport_accept(JabberStream *js, GoogleSession *session);
More information about the Commits
mailing list