void session_clean() {
// clear infohashes so no double-free
- hashmap_clear(session.torrent.infohashes);
+ hashmap_clear(session.torrents.infohashes);
- hashmap_free(session.torrent.infohashes);
- hashmap_free(session.torrent.paths);
+ hashmap_free(session.torrents.infohashes);
+ hashmap_free(session.torrents.paths);
}
+#define SESSION_HASHMAP_INITIAL_SIZE 8
+
int session_init() {
- if(hashmap_init(session.torrents.infohashes,SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; }
- if(hashmap_init(session.torrents.paths,SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; }
+ if(hashmap_init(&(session.torrents.infohashes),SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; }
+ if(hashmap_init(&(session.torrents.paths),SESSION_HASHMAP_INITIAL_SIZE)<0) { return -1; }
return 1;
}
int session_torrent_add(struct torrent *p) {
+ int i;
+
while((i = session_torrent_add_by_infohash(p))<=0) {
if(i<0) { return -1; }
if(session_torrent_resize(
- &(session.torrent.infohashes), /* map */
- session.torrent.infohashes.size<<1 /* new_size */
+ &(session.torrents.infohashes), /* map */
+ session.torrents.infohashes->size<<1 /* new_size */
)<0) { return -1; }
}
while((i = session_torrent_add_by_path(p))<=0) {
if(i<0) { return -1; }
if(session_torrent_resize(
- &(session.torrent.paths), /* map */
- session.torrent.paths.size<<1 /* new_size */
+ &(session.torrents.paths), /* map */
+ session.torrents.paths->size<<1 /* new_size */
)<0) { return -1; }
}
struct torrent *p;
int i;
- if((i = hashmap_insert(session.torrents.infohashes,torrent->infohash,crypto_hash_sha_256_BYTES,p))<=0) {
+ if((i = hashmap_insert(session.torrents.infohashes,torrent->infohash,crypto_hash_sha256_BYTES,torrent))<=0) {
if(i<0) { return -1; }
- p = hashmap_find(session.torrents.infohashes,torrent->infohash,crypto_hash_sha_256_BYTES);
+ p = hashmap_find(session.torrents.infohashes,torrent->infohash,crypto_hash_sha256_BYTES);
if((p!=NULL)&&(memcmp(torrent->infohash,p->infohash,crypto_hash_sha256_BYTES)!=0)) { return 0; }
}
struct torrent *p;
int i;
- if((i = hashmap_insert(session.torrents.paths,torrent->root,strlen(torrent->root),p))<=0) {
+ if((i = hashmap_insert(session.torrents.paths,torrent->root,strlen(torrent->root),torrent))<=0) {
if(i<0) { return -1; }
p = hashmap_find(session.torrents.paths,torrent->root,strlen(torrent->root));
static int session_torrent_resize(struct hash_map **map, size_t new_size) {
struct hash_map *new, *old;
+ struct torrent *p;
+ int flag, ret;
if(hashmap_init(&new,new_size)<0) { return -1; }
#include<session.h>
+#include INCLUDE(TORRENT_INFO_SRC_FILE)
+
int main();
+static void session_init_basic_test();
+static void session_torrent_add_basic_test();
int main() {
setup_env();
+ session_init_basic_test();
+ session_torrent_add_basic_test();
+
clean_env();
return EXIT_SUCCESS;
}
+
+static void session_init_basic_test() {
+ assert(1==session_init());
+
+ session_clean();
+}
+
+static void session_torrent_add_basic_test() {
+ struct torrent *p;
+
+ TORRENT_SETUP_EXAMPLE(&p);
+
+ assert(1==session_init());
+
+ assert(1==session_torrent_add(p));
+}