Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Filetransfermanager + file transfer status event

  • Loading branch information...
commit 72423b7429b579547b1b103a70e5cb1bec71273f 1 parent 198ca9b
@Detegr authored
View
1  core/CMakeLists.txt
@@ -13,6 +13,7 @@ add_library(tapi2p_corelib SHARED \\
event.c \\
peermanager.c \\
pipemanager.c \\
+ filetransfermanager.c \\
ptrlist.c \\
handlers.c \\
util.c \\
View
5 core/core.c
@@ -5,6 +5,7 @@
#include "peermanager.h"
#include "pathmanager.h"
#include "pipemanager.h"
+#include "filetransfermanager.h"
#include "../dtgconf/src/config.h"
#include "event.h"
#include "ptrlist.h"
@@ -513,6 +514,7 @@ void* read_thread(void* args)
case RequestFileTransferLocal:
case RequestFileList:
case RequestFileListLocal:
+ case FileTransferStatus:
{
event_run_callbacks(e);
} // Fall through to send event to pipe listeners as well
@@ -742,6 +744,7 @@ int core_start(void)
}
peermanager_init();
+ filetransfermanager_init();
sem_t init_semaphore;
sem_init(&init_semaphore, 0, 0);
@@ -762,6 +765,7 @@ int core_start(void)
event_addlistener(Message, &handlemessage, NULL);
pipe_event_addlistener(ListPeers, &handlelistpeers, getconfig());
pipe_event_addlistener(Status, &handlestatus, &setupstatus);
+ pipe_event_addlistener(FileTransferStatus, &handlefiletransferstatus, NULL);
event_addlistener(RequestFileTransfer, &handlefiletransfer, NULL);
event_addlistener(RequestFileTransferLocal, &handlefiletransferlocal, NULL);
event_addlistener(RequestFilePart, &handlefilepartrequest, NULL);
@@ -770,7 +774,6 @@ int core_start(void)
event_addlistener(RequestFileList, &handlerequestfilelist, NULL);
event_addlistener(RequestFileListLocal, &handlerequestfilelistlocal, NULL);
event_addlistener(AddFile, &handleaddfile, NULL);
- event_addlistener(RequestFilePartList, &handlefilepartlistrequest, NULL);
while(run_threads)
{
View
1  core/event.h
@@ -22,6 +22,7 @@ typedef enum {
Status,
RequestFilePartList,
FilePartList,
+ FileTransferStatus,
EventCount // For iterating through eventtypes
} EventType;
View
28 core/handlers.c
@@ -2,6 +2,7 @@
#include "core.h"
#include "peermanager.h"
#include "pathmanager.h"
+#include "filetransfermanager.h"
#include "../dtgconf/src/config.h"
#include "util.h"
#include "event.h"
@@ -329,6 +330,7 @@ void handlemetadata(evt_t* e, void* data)
metadata_t* md=(metadata_t*)e->data;
md->data = (uint8_t*)e->data + sizeof(metadata_t);
sha_to_str(md->data, sha_str);
+ filetransfer_add(p, sha_str, md);
check_or_create_metadata(md->data, e->data_len-sizeof(metadata_t));
create_file_transfer(p, sha_str, md);
//request_file_part_listing_from_peers(sha_str, p);
@@ -355,6 +357,7 @@ void handlefilepart(evt_t* e, void* data)
{
fflush(ft->file);
printf("Wrote %lu bytes (part %d)\n", e->data_len-sizeof(fp_t), fp->partnum);
+ filetransfer_part_ready(fp->sha_str, fp->partnum);
if(fp->partnum==0 && e->data_len-sizeof(fp_t) != FILE_PART_BYTES)
{
printf("File transfer done, clearing...\n");
@@ -415,7 +418,7 @@ void handlerequestfilelistlocal(evt_t* e, void* data)
json_t *root=get_file_list_as_json();
char *str=json_dumps(root, 0);
printf("%s - %d\n", str, strlen(str));
- pipe_event_send_back_to_caller((pipeevt_t*)e, (const unsigned char*)str, strlen(str));
+ pipe_event_send_back_to_caller((pipeevt_t*)e, str, strlen(str));
free(str);
json_decref(root);
}
@@ -576,3 +579,26 @@ void handlefilepartlistrequest(evt_t *e, void *data)
printf("File part list requested for hash %s\n", sha_str);
}
+
+void handlefiletransferstatus(pipeevt_t *e, void *data)
+{
+ int statuscount;
+ ftstatus *statuses=filetransfer_get_statuses(&statuscount);
+ json_t *root=json_object();
+ json_t *arr=json_array();
+ for(int i=0; i<statuscount; ++i)
+ {
+ json_t *obj=json_object();
+ json_object_set_new(obj, "sha", json_string(statuses[i].sha_str));
+ json_object_set_new(obj, "partCount", json_integer(statuses[i].part_count));
+ json_object_set_new(obj, "partsReady", json_integer(statuses[i].parts_ready));
+ json_object_set_new(obj, "fileSize", json_integer(statuses[i].file_size));
+ json_array_append_new(arr, obj);
+ }
+ json_object_set_new(root, "statuses", arr);
+ char *jsonstr=json_dumps(root, 0);
+ pipe_event_send_back_to_caller(e, jsonstr, strlen(jsonstr));
+ free(jsonstr);
+ json_decref(root);
+ free(statuses);
+}
View
2  core/handlers.h
@@ -16,6 +16,6 @@ void handlerequestfilelist(evt_t* e, void* data);
void handleaddfile(evt_t* e, void* data);
void handlesetup(pipeevt_t *e, void *data);
void handlestatus(pipeevt_t *e, void *data);
-void handlefilepartlistrequest(evt_t *e, void *data);
+void handlefiletransferstatus(pipeevt_t *e, void *data);
#endif
View
36 core/peer.c
@@ -31,6 +31,13 @@ void peer_free(struct peer* p)
p->thread=0;
}
pubkey_free(&p->key);
+ for(int i=0; i<MAX_TRANSFERS; ++i)
+ {
+ if(p->file_transfers[i].file)
+ {
+ clear_file_transfer(&p->file_transfers[i]);
+ }
+ }
}
void clear_file_transfer(file_t* transfer)
@@ -88,27 +95,40 @@ int create_file_transfer(struct peer* p, const char* sha_str, metadata_t* metada
return 0;
}
-file_t* get_and_lock_new_filetransfer(struct peer* p)
-{
+static file_t *get_file_transfer_internal(struct peer *p, const char *sha_str)
+{// Returned struct should NOT be modified if file_lock is not locked.
for(int i=0; i<MAX_TRANSFERS; ++i)
{
- if(pthread_mutex_trylock(&p->file_transfers[i].file_lock) == 0)
+ if(strncmp(p->file_transfers[i].sha_str, sha_str, SHA_DIGEST_STR_MAX_LENGTH) == 0)
{
- return &(p->file_transfers[i]);
+ return &p->file_transfers[i];
}
}
return NULL;
}
-file_t* get_and_lock_existing_filetransfer_for_sha(struct peer* p, const char* sha_str)
+const file_t *get_file_transfer(struct peer *p, const char *sha_str)
+{
+ return (const file_t*)get_file_transfer_internal(p, sha_str);
+}
+
+file_t* get_and_lock_new_filetransfer(struct peer* p)
{
for(int i=0; i<MAX_TRANSFERS; ++i)
{
- if(strncmp(p->file_transfers[i].sha_str, sha_str, SHA_DIGEST_STR_MAX_LENGTH) == 0)
+ if(pthread_mutex_trylock(&p->file_transfers[i].file_lock) == 0)
{
- pthread_mutex_lock(&p->file_transfers[i].file_lock);
- return &p->file_transfers[i];
+ return &(p->file_transfers[i]);
}
}
return NULL;
}
+
+file_t* get_and_lock_existing_filetransfer_for_sha(struct peer* p, const char* sha_str)
+{
+ file_t *ft=get_file_transfer_internal(p, sha_str);
+ if(!ft) return NULL;
+
+ pthread_mutex_lock(&ft->file_lock);
+ return ft;
+}
View
8 core/peer.h
@@ -25,9 +25,11 @@ struct peer
void peer_init(struct peer* p);
void peer_free(struct peer* p);
-void clear_file_transfer(file_t* transfer);
+
int create_file_transfer(struct peer* p, const char* sha_str, metadata_t* metadata);
-file_t* get_and_lock_new_filetransfer(struct peer* p);
-file_t* get_and_lock_existing_filetransfer_for_sha(struct peer* p, const char* sha_str);
+file_t *get_and_lock_new_filetransfer(struct peer* p);
+file_t *get_and_lock_existing_filetransfer_for_sha(struct peer *p, const char *sha_str);
+const file_t *get_file_transfer(struct peer *p, const char *sha_str);
+void clear_file_transfer(file_t* transfer);
#endif
View
9 ui/cli/cli.c
@@ -223,6 +223,7 @@ int main(int argc, char** argv)
case 'S':
{
int fd=core_socket();
+ printf("tapi2p_core status:\n");
event_send_simple(Status, NULL, 0, fd);
pipeevt_t* e=event_recv(fd, NULL);
if(e)
@@ -230,6 +231,14 @@ int main(int argc, char** argv)
printf("%s\n", e->data);
free(e);
}
+ printf("File transfer status:\n");
+ event_send_simple(FileTransferStatus, NULL, 0, fd);
+ e=event_recv(fd, NULL);
+ if(e)
+ {
+ printf("%s\n", e->data);
+ free(e);
+ }
return 0;
}
case 'n':
View
5 ui/web/server/tapi2p_websocket.c
@@ -296,10 +296,11 @@ int main()
info.uid=-1;
ctx = libwebsocket_create_context(&info);
- if(!ctx)
+ while(!ctx)
{
lwsl_err("libwebsocket init failed.");
- return -1;
+ sleep(1);
+ ctx = libwebsocket_create_context(&info);
}
lwsl_notice("tapi2p websocket server started.\n");

0 comments on commit 72423b7

Please sign in to comment.
Something went wrong with that request. Please try again.