Permalink
Browse files

Minor fixes

  • Loading branch information...
Detegr committed Feb 24, 2014
1 parent f92b3ad commit c76ba9192635d4c515f447d32fde8dcccb9f5978
Showing with 34 additions and 21 deletions.
  1. +7 −0 core/core.c
  2. +1 −0 core/core.h
  3. +1 −4 core/file.c
  4. +2 −1 core/file.h
  5. +19 −9 core/handlers.c
  6. +2 −5 core/peer.c
  7. +1 −1 core/peermanager.c
  8. +1 −1 core/peermanager.h
View
@@ -666,6 +666,13 @@ void send_event_to_peer(struct peer* p, evt_t* e)
send_event_to_peer_internal(p, e, 0);
}
+void send_event_to_peer_simple(EventType t, void *data, uint32_t data_len, struct peer *p)
+{
+ evt_t *e=event_new(t, data, data_len);
+ send_event_to_peer_internal(p, e, 0);
+ free(e);
+}
+
void send_event_to_peer_nonblocking(struct peer* p, evt_t* e)
{
send_event_to_peer_internal(p, e, 1);
View
@@ -10,6 +10,7 @@ void core_stop(void);
int core_socket(void);
void send_to_all(evt_t* e);
void send_event_to_peer(struct peer* p, evt_t* e);
+void send_event_to_peer_simple(EventType t, void *data, uint32_t data_len, struct peer *p);
void send_event_to_peer_nonblocking(struct peer* p, evt_t* e);
#endif
View
@@ -10,7 +10,7 @@
#include "event.h"
#include "core.h"
-unsigned char* SHA1_for_part(unsigned part, unsigned char* data, size_t datasize)
+unsigned char *SHA1_for_part(unsigned part, unsigned char *data, size_t datasize)
{
unsigned char* ret=malloc(SHA_DIGEST_LENGTH);
size_t bytes;
@@ -87,7 +87,6 @@ void create_metadata_file(const char* from, char* file_sha_as_str)
void check_or_create_metadata(const unsigned char* sha_data, size_t sha_size)
{
- struct config* conf=getconfig();
struct stat buf;
char sha_str[SHA_DIGEST_STR_MAX_LENGTH];
sha_to_str(sha_data, sha_str);
@@ -98,8 +97,6 @@ void check_or_create_metadata(const unsigned char* sha_data, size_t sha_size)
FILE* f=fopen(mdpath, "w");
if(f)
{
- unsigned char type=Metadata;
- fwrite(&type, 1, 1, f);
fwrite(sha_data, sha_size, 1, f);
fclose(f);
}
View
@@ -44,6 +44,7 @@ void create_metadata_file(const char* from, char* file_sha_as_str);
void check_or_create_metadata(const unsigned char* sha_data, size_t sha_size);
void request_file_part_from_peer(int partnum, const char* sha_str, struct peer* p);
void request_file_part_listing_from_peers(const char *sha_str, struct peer *exclude_current);
-const metadata_t *get_metadata(const char *sha_str);
+
+unsigned char *SHA1_for_part(unsigned part, unsigned char *data, size_t datasize);
#endif
View
@@ -114,7 +114,7 @@ static void* sendmetadata(void* args)
do
{
sent += send_file_part(td);
- printf("Sent %lu bytes of %lu\n", sent, td->transfer->metadata.file_size);
+ printf("Sent %lu bytes of %lu\n", sent, td->transfer->metadata.file_size);
} while(sent < td->transfer->metadata.file_size);
clear_file_transfer(td->transfer);
@@ -183,10 +183,8 @@ void handlefiletransferlocal(evt_t* e, void* data)
}
}
-static FILE* check_peer_and_open_file_for_sha(struct peer* p, const char* sha_str, uint64_t* filesize)
+static FILE *open_file_for_sha(const char* sha_str, uint64_t* filesize)
{
- if(!p) return NULL;
-
FILE* ret=NULL;
struct config* conf=getconfig();
struct configitem* ci;
@@ -208,10 +206,14 @@ static FILE* check_peer_and_open_file_for_sha(struct peer* p, const char* sha_st
return ret;
}
-static FILE* check_peer_and_open_metadata(struct peer* p, const char* sha_str)
+static FILE *check_peer_and_open_file_for_sha(struct peer* p, const char* sha_str, uint64_t* filesize)
{
if(!p) return NULL;
+ return open_file_for_sha(sha_str, filesize);
+}
+static FILE *open_metadata(const char *sha_str)
+{
FILE* ret=NULL;
struct config* conf=getconfig();
struct configitem* ci;
@@ -229,6 +231,12 @@ static FILE* check_peer_and_open_metadata(struct peer* p, const char* sha_str)
return ret;
}
+static FILE *check_peer_and_open_metadata(struct peer* p, const char* sha_str)
+{
+ if(!p) return NULL;
+ return open_metadata(sha_str);
+}
+
void handlefiletransfer(evt_t* e, void* data)
{
e->data[e->data_len]=0;
@@ -241,7 +249,6 @@ void handlefiletransfer(evt_t* e, void* data)
file_t* t=get_and_lock_new_filetransfer(p);
if(t)
{
- pthread_t sendthread;
struct file_part_thread_data* td=malloc(sizeof(struct file_part_thread_data));
td->type=Metadata;
td->partnum=0;
@@ -257,6 +264,7 @@ void handlefiletransfer(evt_t* e, void* data)
td->part_count=(filesize/FILE_PART_BYTES)+1;
td->file_size=filesize;
+ pthread_t sendthread;
pthread_create(&sendthread, NULL, &sendmetadata, td);
pthread_mutex_unlock(&t->file_lock);
}
@@ -561,8 +569,10 @@ void handlestatus(pipeevt_t *e, void *data)
}
void handlefilepartlistrequest(evt_t *e, void *data)
-{
- const char *hash_str=(const char*)e->data;
- printf("File part list requested for hash %s\n", hash_str);
+{// TODO: Modify this to indicate parts with bits instead of full bytes
+ const char *sha_str=(const char*)e->data;
+ struct peer *p=peer_exists_simple(e->addr, e->port);
+ if(!p) assert(false);
+ printf("File part list requested for hash %s\n", sha_str);
}
View
@@ -106,11 +106,8 @@ file_t* get_and_lock_existing_filetransfer_for_sha(struct peer* p, const char* s
{
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)
- {
- return &p->file_transfers[i];
- }
- else return NULL;
+ pthread_mutex_lock(&p->file_transfers[i].file_lock);
+ return &p->file_transfers[i];
}
}
return NULL;
View
@@ -161,7 +161,7 @@ struct peer* peer_exists(struct peer* p)
return NULL;
}
-struct peer* peer_exists_simple(char* addr, unsigned short port)
+struct peer* peer_exists_simple(const char* addr, unsigned short port)
{
if(!addr || !port) return NULL;
View
@@ -22,7 +22,7 @@ void peer_removefromset(struct peer* p);
// and thus it doesn't check if p itself exists.
// Returns the existing peer if peer exists, NULL otherwise
struct peer* peer_exists(struct peer* p);
-struct peer* peer_exists_simple(char* addr, unsigned short port);
+struct peer* peer_exists_simple(const char* addr, unsigned short port);
int peer_remove(struct peer* p);
void peers_free(void);
struct peer* peer_from_event(struct event* e);

0 comments on commit c76ba91

Please sign in to comment.