Changeset 1708
- Timestamp:
- Apr 12, 2007, 2:41:19 PM (16 years ago)
- Location:
- branches/daemon/daemon
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/daemon/daemon/client.c
r1696 r1708 76 76 { 77 77 int64_t tag; 78 cl_listfunc listcb;79 78 cl_infofunc infocb; 80 cl_hashfunc hashcb;81 79 cl_statfunc statcb; 82 80 RB_ENTRY( resp ) links; … … 546 544 547 545 int 548 client_list( cl_ listfunc func )546 client_list( cl_infofunc func ) 549 547 { 550 548 struct req * req; … … 557 555 } 558 556 559 resp-> listcb = func;557 resp->infocb = func; 560 558 req->types = IPC_INF_NAME | IPC_INF_HASH; 561 559 … … 582 580 583 581 int 584 client_hashids( cl_ hashfunc func )582 client_hashids( cl_infofunc func ) 585 583 { 586 584 struct req * req; … … 593 591 } 594 592 595 resp-> hashcb = func;593 resp->infocb = func; 596 594 req->types = IPC_INF_HASH; 597 595 … … 613 611 resp->statcb = func; 614 612 req->types = IPC_ST_STATE | IPC_ST_ETA | IPC_ST_COMPLETED | 615 IPC_ST_DOWNSPEED | IPC_ST_UPSPEED | IPC_ST_ERROR | IPC_ST_ERRMSG; 613 IPC_ST_DOWNSPEED | IPC_ST_UPSPEED | IPC_ST_DOWNTOTAL | IPC_ST_UPTOTAL | 614 IPC_ST_ERROR | IPC_ST_ERRMSG; 616 615 617 616 return 0; … … 869 868 void * arg UNUSED ) 870 869 { 871 benc_val_t * dict;872 int ii;873 int64_t id, size;874 char * name, * hash;875 struct resp * resp, key;870 benc_val_t * dict; 871 int ii; 872 struct cl_info inf; 873 int64_t id; 874 struct resp * resp, key; 876 875 877 876 assert( IPC_MSG_INFO == msgid ); 878 877 879 if( TYPE_LIST != list->type )878 if( TYPE_LIST != list->type || NULL == resp->infocb ) 880 879 { 881 880 return; … … 899 898 } 900 899 901 id = getinfoint( msgid, dict, IPC_INF_ID, -1 );902 name = getinfostr( msgid, dict, IPC_INF_NAME, NULL );903 hash = getinfostr( msgid, dict, IPC_INF_HASH, NULL );904 size = getinfoint( msgid, dict, IPC_INF_SIZE, -1 );900 id = getinfoint( msgid, dict, IPC_INF_ID, -1 ); 901 inf.name = getinfostr( msgid, dict, IPC_INF_NAME, NULL ); 902 inf.hash = getinfostr( msgid, dict, IPC_INF_HASH, NULL ); 903 inf.size = getinfoint( msgid, dict, IPC_INF_SIZE, -1 ); 905 904 906 905 if( !TORRENT_ID_VALID( id ) ) … … 909 908 } 910 909 911 if( NULL != resp->infocb ) 912 { 913 resp->infocb( id, name, size ); 914 } 915 else if( NULL != resp->listcb ) 916 { 917 resp->listcb( id, name, hash ); 918 } 919 else if( NULL != resp->hashcb ) 920 { 921 resp->hashcb( id, hash ); 922 } 910 inf.id = id; 911 resp->infocb( &inf ); 923 912 } 924 913 … … 931 920 void * arg UNUSED ) 932 921 { 933 benc_val_t * dict;934 int ii;935 int64_t id, eta, done, down, up;936 char * state, * error, * errmsg;937 struct resp * resp, key;922 benc_val_t * dict; 923 int ii; 924 int64_t id; 925 struct cl_stat st; 926 struct resp * resp, key; 938 927 939 928 assert( IPC_MSG_STAT == msgid ); 940 929 941 if( TYPE_LIST != list->type )930 if( TYPE_LIST != list->type || NULL == resp->statcb ) 942 931 { 943 932 return; … … 961 950 } 962 951 963 id = getinfoint( msgid, dict, IPC_ST_ID, -1 ); 964 state = getinfostr( msgid, dict, IPC_ST_STATE, NULL ); 965 eta = getinfoint( msgid, dict, IPC_ST_ETA, -1 ); 966 done = getinfoint( msgid, dict, IPC_ST_COMPLETED, -1 ); 967 down = getinfoint( msgid, dict, IPC_ST_DOWNSPEED, -1 ); 968 up = getinfoint( msgid, dict, IPC_ST_UPSPEED, -1 ); 969 error = getinfostr( msgid, dict, IPC_ST_ERROR, NULL ); 970 errmsg = getinfostr( msgid, dict, IPC_ST_ERRMSG, NULL ); 952 id = getinfoint( msgid, dict, IPC_ST_ID, -1 ); 953 st.state = getinfostr( msgid, dict, IPC_ST_STATE, NULL ); 954 st.eta = getinfoint( msgid, dict, IPC_ST_ETA, -1 ); 955 st.done = getinfoint( msgid, dict, IPC_ST_COMPLETED, -1 ); 956 st.ratedown = getinfoint( msgid, dict, IPC_ST_DOWNSPEED, -1 ); 957 st.rateup = getinfoint( msgid, dict, IPC_ST_UPSPEED, -1 ); 958 st.totaldown = getinfoint( msgid, dict, IPC_ST_DOWNTOTAL, -1 ); 959 st.totalup = getinfoint( msgid, dict, IPC_ST_UPTOTAL, -1 ); 960 st.error = getinfostr( msgid, dict, IPC_ST_ERROR, NULL ); 961 st.errmsg = getinfostr( msgid, dict, IPC_ST_ERRMSG, NULL ); 971 962 972 963 if( !TORRENT_ID_VALID( id ) ) … … 975 966 } 976 967 977 if( NULL != resp->statcb ) 978 { 979 resp->statcb( id, state, eta, done, down, up, error, errmsg ); 980 } 968 st.id = id; 969 resp->statcb( &st ); 981 970 } 982 971 … … 1027 1016 if( NULL != resp->infocb ) 1028 1017 { 1029 resp->infocb( -1, NULL, -1 ); 1030 } 1031 else if( NULL != resp->listcb ) 1032 { 1033 resp->listcb( -1, NULL, NULL ); 1034 } 1035 else if( NULL != resp->hashcb ) 1036 { 1037 resp->hashcb( -1, NULL ); 1018 resp->infocb( NULL ); 1038 1019 } 1039 1020 else if( NULL != resp->statcb ) 1040 1021 { 1041 resp->statcb( -1, NULL, -1, -1, -1, -1, NULL,NULL );1022 resp->statcb( NULL ); 1042 1023 } 1043 1024 } -
branches/daemon/daemon/client.h
r1635 r1708 29 29 struct strlist; 30 30 31 typedef void ( * cl_listfunc )( int, const char *, const char * ); 32 typedef void ( * cl_infofunc )( int, const char *, int64_t ); 33 typedef void ( * cl_statfunc )( int, const char *, int64_t, int64_t, int64_t, 34 int64_t, const char *, const char * ); 35 typedef void ( * cl_hashfunc )( int, const char * ); 31 struct cl_info 32 { 33 int id; 34 const char * name; 35 const char * hash; 36 int64_t size; 37 }; 38 39 struct cl_stat 40 { 41 int id; 42 const char * state; 43 int64_t eta; 44 int64_t done; 45 int64_t ratedown; 46 int64_t rateup; 47 int64_t totaldown; 48 int64_t totalup; 49 const char * error; 50 const char * errmsg; 51 }; 52 53 typedef void ( * cl_infofunc )( const struct cl_info * ); 54 typedef void ( * cl_statfunc )( const struct cl_stat * ); 36 55 37 56 int client_init ( struct event_base * ); … … 49 68 int client_stop ( size_t, const int * ); 50 69 int client_remove ( size_t, const int * ); 51 int client_list ( cl_ listfunc );70 int client_list ( cl_infofunc ); 52 71 int client_info ( cl_infofunc ); 53 int client_hashids ( cl_ hashfunc );72 int client_hashids ( cl_infofunc ); 54 73 int client_status ( cl_statfunc ); 55 74 -
branches/daemon/daemon/remote.c
r1696 r1708 78 78 int64_t eta; 79 79 int64_t done; 80 int64_t down; 81 int64_t up; 80 int64_t ratedown; 81 int64_t rateup; 82 int64_t totaldown; 83 int64_t totalup; 82 84 char * errorcode; 83 85 char * errormsg; … … 103 105 static int hasharg ( const char *, struct strlist *, int * ); 104 106 static int fileargs ( struct strlist *, int, char * const * ); 105 static void listmsg ( int, const char *, const char * ); 106 static void infomsg ( int, const char *, int64_t ); 107 static void statmsg ( int, const char *, int64_t, int64_t, int64_t, 108 int64_t, const char *, const char * ); 109 static void hashmsg ( int, const char * ); 107 static void listmsg ( const struct cl_info * ); 108 static void infomsg ( const struct cl_info * ); 109 static void statmsg ( const struct cl_stat * ); 110 static void hashmsg ( const struct cl_info * ); 110 111 static float fmtsize ( int64_t, const char ** ); 111 112 static char * strdup_noctrl( const char * ); … … 533 534 534 535 void 535 listmsg( int id, const char * name, const char * hash)536 listmsg( const struct cl_info * inf ) 536 537 { 537 538 char * newname; 538 539 size_t ii; 539 540 540 if( !TORRENT_ID_VALID( id ))541 if( NULL == inf ) 541 542 { 542 543 return; 543 544 } 544 545 545 if( NULL == name )546 if( NULL == inf->name ) 546 547 { 547 548 errmsg( "missing torrent name from server" ); 548 549 return; 549 550 } 550 else if( NULL == hash )551 else if( NULL == inf->hash ) 551 552 { 552 553 errmsg( "missing torrent hash from server" ); … … 554 555 } 555 556 556 newname = strdup_noctrl( name );557 newname = strdup_noctrl( inf->name ); 557 558 if( NULL == newname ) 558 559 { … … 560 561 } 561 562 562 for( ii = 0; '\0' != hash[ii]; ii++ )563 { 564 if( isxdigit( hash[ii] ) )565 { 566 putchar( tolower( hash[ii] ) );563 for( ii = 0; '\0' != inf->hash[ii]; ii++ ) 564 { 565 if( isxdigit( inf->hash[ii] ) ) 566 { 567 putchar( tolower( inf->hash[ii] ) ); 567 568 } 568 569 } … … 573 574 574 575 void 575 infomsg( int id, const char * name, int64_t size)576 { 577 struct torinfo * info, key;576 infomsg( const struct cl_info * cinfo ) 577 { 578 struct torinfo * tinfo, key; 578 579 579 580 gl_gotlistinfo = 1; 580 581 581 if( !TORRENT_ID_VALID( id ))582 if( NULL == cinfo ) 582 583 { 583 584 if( gl_gotliststat ) … … 588 589 } 589 590 590 if( NULL == name || 0 >=size )591 if( NULL == cinfo->name || 0 >= cinfo->size ) 591 592 { 592 593 errmsg( "missing torrent info from server" ); … … 595 596 596 597 bzero( &key, sizeof key ); 597 key.id = id; 598 key.id = cinfo->id; 599 tinfo = RB_FIND( torlist, &gl_torinfo, &key ); 600 if( NULL == tinfo ) 601 { 602 tinfo = calloc( 1, sizeof *tinfo ); 603 if( NULL == tinfo ) 604 { 605 mallocmsg( sizeof *tinfo ); 606 return; 607 } 608 tinfo->id = cinfo->id; 609 RB_INSERT( torlist, &gl_torinfo, tinfo ); 610 } 611 612 free( tinfo->name ); 613 tinfo->name = strdup_noctrl( cinfo->name ); 614 tinfo->size = cinfo->size; 615 } 616 617 void 618 statmsg( const struct cl_stat * st ) 619 { 620 struct torinfo * info, key; 621 622 gl_gotliststat = 1; 623 624 if( NULL == st ) 625 { 626 if( gl_gotlistinfo ) 627 { 628 printlisting(); 629 } 630 return; 631 } 632 633 if( NULL == st->state || 0 > st->done || 634 0 > st->ratedown || 0 > st->rateup || 635 0 > st->totaldown || 0 > st->totalup ) 636 { 637 errmsg( "missing torrent status from server" ); 638 return; 639 } 640 641 bzero( &key, sizeof key ); 642 key.id = st->id; 598 643 info = RB_FIND( torlist, &gl_torinfo, &key ); 599 644 if( NULL == info ) … … 605 650 return; 606 651 } 607 info->id = id;652 info->id = st->id; 608 653 RB_INSERT( torlist, &gl_torinfo, info ); 609 654 } 610 655 611 free( info->name ); 612 info->name = strdup_noctrl( name ); 613 info->size = size; 656 free( info->state ); 657 info->state = strdup_noctrl( st->state ); 658 info->eta = st->eta; 659 info->done = st->done; 660 info->ratedown = st->ratedown; 661 info->rateup = st->rateup; 662 info->totaldown = st->totaldown; 663 info->totalup = st->totalup; 664 if( NULL != st->error && '\0' != st->error[0] ) 665 { 666 info->errorcode = strdup_noctrl( st->error ); 667 } 668 if( NULL != st->errmsg && '\0' != st->errmsg[0] ) 669 { 670 info->errormsg = strdup_noctrl( st->errmsg ); 671 } 614 672 } 615 673 616 674 void 617 statmsg( int id, const char * state, int64_t eta, int64_t done, 618 int64_t down, int64_t up, const char * terr, const char * terrmsg ) 619 { 620 struct torinfo * info, key; 621 622 gl_gotliststat = 1; 623 624 if( !TORRENT_ID_VALID( id ) ) 625 { 626 if( gl_gotlistinfo ) 627 { 628 printlisting(); 629 } 630 return; 631 } 632 633 if( NULL == state || 0 > done || 0 > down || 0 > up ) 634 { 635 errmsg( "missing torrent status from server" ); 636 return; 637 } 638 639 bzero( &key, sizeof key ); 640 key.id = id; 641 info = RB_FIND( torlist, &gl_torinfo, &key ); 642 if( NULL == info ) 643 { 644 info = calloc( 1, sizeof *info ); 645 if( NULL == info ) 646 { 647 mallocmsg( sizeof *info ); 648 return; 649 } 650 info->id = id; 651 RB_INSERT( torlist, &gl_torinfo, info ); 652 } 653 654 free( info->state ); 655 info->state = strdup_noctrl( state ); 656 info->eta = eta; 657 info->done = done; 658 info->down = down; 659 info->up = up; 660 if( NULL != terr && '\0' != terr[0] ) 661 { 662 info->errorcode = strdup_noctrl( terr ); 663 } 664 if( NULL != terrmsg && '\0' != terrmsg[0] ) 665 { 666 info->errormsg = strdup_noctrl( terrmsg ); 667 } 668 } 669 670 void 671 hashmsg( int id, const char * hash ) 675 hashmsg( const struct cl_info * inf ) 672 676 { 673 677 struct torhash key, * found; 674 678 675 if( !TORRENT_ID_VALID( id ))679 if( NULL == inf ) 676 680 { 677 681 sendidreqs(); … … 679 683 } 680 684 681 if( NULL == hash )685 if( NULL == inf->hash ) 682 686 { 683 687 errmsg( "missing torrent hash from server" ); … … 686 690 687 691 bzero( &key, sizeof key ); 688 strlcpy( key.hash, hash, sizeof key.hash );692 strlcpy( key.hash, inf->hash, sizeof key.hash ); 689 693 found = RB_FIND( torhashes, &gl_hashids, &key ); 690 694 if( NULL != found ) 691 695 { 692 found->id = i d;696 found->id = inf->id; 693 697 } 694 698 } … … 868 872 struct torinfo * ii, * next; 869 873 const char * units, * name, * upunits, * downunits; 870 float size, progress, upspeed, downspeed ;874 float size, progress, upspeed, downspeed, ratio; 871 875 872 876 /* sort the torrents by name */ … … 886 890 /* massage some numbers into a better format for printing */ 887 891 size = fmtsize( ii->size, &units ); 888 upspeed = fmtsize( ii-> up, &upunits );889 downspeed = fmtsize( ii-> down, &downunits );892 upspeed = fmtsize( ii->rateup, &upunits ); 893 downspeed = fmtsize( ii->ratedown, &downunits ); 890 894 name = ( NULL == ii->name ? "???" : ii->name ); 891 895 progress = ( float )ii->done / ( float )ii->size * 100.0; … … 915 919 else if( 0 == strcasecmp( "seeding", ii->state ) ) 916 920 { 917 printf( "100%% seeding at %.*f %s/s", 918 BESTDECIMAL( upspeed ), upspeed, upunits ); 921 if( 0 == ii->totalup && 0 == ii->totaldown ) 922 { 923 printf( "100%% seeding at %.*f %s/s [N/A]", 924 BESTDECIMAL( upspeed ), upspeed, upunits ); 925 } 926 else if( 0 == ii->totaldown ) 927 { 928 printf( "100%% seeding at %.*f %s/s [INF]", 929 BESTDECIMAL( upspeed ), upspeed, upunits ); 930 } 931 else 932 { 933 ratio = ( float )ii->totalup / ( float )ii->totaldown; 934 printf( "100%% seeding at %.*f %s/s [%.*f]", 935 BESTDECIMAL( upspeed ), upspeed, upunits, 936 BESTDECIMAL( ratio ), ratio ); 937 } 919 938 } 920 939 /* print stopping message */
Note: See TracChangeset
for help on using the changeset viewer.