|
楼主 |
发表于 2004-12-7 22:29:08
|
显示全部楼层
[code:1]
/*
#include <gtk/gtk.h>
#include "hello.h"
#include <gdk/gdkkeysyms.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
//bt include
#include <curl/curl.h>
#include <curl/easy.h>
#include <openssl/sha.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <locale.h>
#include <stdarg.h>
#include <ctype.h>
*/
/* btget.c */
#include "config.h"
int poit=0;
#include <curl/curl.h>
#include <curl/easy.h>
#include <openssl/sha.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#if !WIN32
#include <sys/param.h>
#endif
#include <time.h>
#include <signal.h>
#include <locale.h>
/* inet sockets */
#include <sys/types.h>
#if WIN32
#include <winsock2.h>
#include "getopt.h"
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <netdb.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <pthread.h>
/* libbt */
#include "bts.h"
#include "types.h"
#include "benc.h"
#include "random.h"
#include "peer.h"
#include "stream.h"
#include "util.h"
#include "segmenter.h"
#include "context.h"
#include "bterror.h"
#include "bitset.h"
#include "pig.h"
#define READABLE 1
#define EXECUTE 2
#define Headname 0
#define V(vers) #vers
#include <gtk/gtk.h>
#include "hello.h"
#include <gdk/gdkkeysyms.h>
btContext context;
int opthelp = 0;
int optverbose = 0;
float optexaggerate = 1.0;
int optseed = 0;
int optsnub = 1;
////////////////////////////////////表格的内容函数:
gchar* name_get(char*name,char*from);
gchar* nameget;
///////////////////////////////////////
char* btget(btget_arg args_btget,GtkFileSelection*fs);
void
on_open_activate (GtkMenuItem *menuitem,
gpointer user_data);
void
on_item4_activate (GtkMenuItem *menuitem,
gpointer user_data);
GtkWidget* create_fileselection (void);
void file_select_ok(GtkWidget*button,GtkFileSelection*fs);
void quit();
void mission_window(GtkWidget *clist1);
GtkWidget* create_window(void);
GtkWidget* menu_bar(void);
GtkWidget*create_entry(void);
GtkWidget*down_button(void);
void on_ok_button_clicked();
void on_cancel_button_clicked();
void Pack(void);
void clist_append(btget_arg args_btget,GtkWidget*clist,char*torrent,GtkFileSelection*fs);
GtkWidget*clist_return(GtkWidget*clist);
char*filename_return(char*ag);
int client_init( btContext *ctx, int cs);
int client_run( btContext *ctx, int cs, int cmd);
int client_write( btContext *ctx, int cs);
int client_finit( btContext *ctx, int cs);
void client_error( btContext *ctx, char *activity, int cs);
//gtk defined
////////////////
GtkWidget* rid_window;
static gchar*title[10]={"Name","Size","Downed","Completed","Status","Peers","Friends","DownSpeed","UpSpeed","TimeLeft"};
const gchar*new_row[10];
//static GtkWidget*clist;
/* static GtkWidget*entry_name;
static GtkWidget*entry_size;
static GtkWidget*entry_downed;
static GtkWidget*entry_completed;
static GtkWidget*entry_status;
static GtkWidget*entry_peers;
static GtkWidget*entry_friends;
static GtkWidget*entry_downloadspeed;
static GtkWidget*entry_uploadspeed;
static GtkWidget*entry_time_left;
static GtkWidget*entry_fav;//目前这个禁用阿 可惜阿*/
////////////////
RETSIGTYPE sigint_handler( int signal){
exit(0);}
#if !WIN32
void sigepipe_handler( int signal) {
printf("Caught EPIPE for unknown file.\n");
}
void epipe_setup() {
int err;
struct sigaction sa = {};
sa.sa_handler = sigepipe_handler;
sigemptyset( &sa.sa_mask);
sa.sa_flags = SA_RESETHAND | SA_RESTART;
err = sigaction( SIGPIPE, &sa, NULL);
if (err) SYSDIE("sigaction");
}
#endif
btStream *fetchtorrent( char *url);
char* BTlist(char *torrent,btget_arg args_btget,GtkFileSelection*fs);
void BTcheck(char*filename);
int client_init( btContext *ctx, int cs) {
return peer_answer( ctx, cs);
}
int client_run( btContext *ctx, int cs, int cmd) {
int res = 0;
int err;
btPeer *p = ctx->sockpeer[cs];
/* check for new messages */
do {
err = peer_recv_message( ctx, p);
} while (err == 1);
#if 0
printf("Still have %d bytes buffered\n", kStream_iqlen(&p->ios));
#endif
if (err < 0 && p->ios.error != EAGAIN) return -1;
/* do queue processing */
if(p->download!=INT_MAX)
err = peer_process_queue( &ctx->downloads[p->download]->fileset, p);
if (err < 0 && p->ios.error != EAGAIN) return -1;
if (err == 1 && cmd==EXECUTE) res = 1;
return res;
}
/*
* Returns 1 to indicate that more output is still queued
* Returns 0 if the output buffer has drained, no more writing
* Returns -1 on error
*/
int client_write( btContext *ctx, int cs) {
/* writable player socket */
int res = 0;
int err;
btPeer *p = ctx->sockpeer[cs];
if (p->state == PEER_INIT) {
/* unconnected socket */
err = peer_connect_complete( ctx, p);
if (err<0 && p->ios.error == EAGAIN) {
return 1; /* keep waiting */
}
if (err) return -1;
/* connection complete, add to read set */
ctx_setevents( ctx, cs, POLLIN);
peer_send_handshake( ctx, p);
peer_send_bitfield( ctx, p);
p->state = PEER_OUTGOING;
} else {
/* connected/good/etc. socket */
err = kStream_flush( &p->ios);
if (err < 0 && p->ios.error != EAGAIN) {
return -1;
}
}
if (kStream_oqlen( &p->ios) > 0) {
res = 1;
}
return res;
}
int client_finit( btContext *ctx, int cs) {
int i, j, dl;
char *err = "connection closed";
btPeer *p = ctx->sockpeer[cs];
if (p->ios.error != 0) err = bts_strerror( p->ios.error);
dl=p->download;
peer_shutdown( ctx, p, err);
/* find where peer is */
if(dl!=INT_MAX) {
btPeerset *pset=&ctx->downloads[dl]->peerset;
for (i=0; i < pset->len; i++) {
if (pset->peer[i] == p) {
j = i;
pset->peer[i] = NULL;
break;
}
}
/* shift down the rest */
for (i=j; i < pset->len-1; i++) {
pset->peer[i] = pset->peer[i+1];
}
pset->len--;
}
btfree(p);
return 0;
}
void client_error( btContext *ctx, char *activity, int cs) {
/* errors on a socket */
printf( "%d: Peer %s shutdown %s (%s)\n",
cs, inet_ntoa(ctx->sockpeer[cs]->ip),
activity,
bts_strerror(ctx->sockpeer[cs]->ios.error));
client_finit( ctx, cs);
close(cs);
/* clear the execute bit if it was set */
if (ctx->x_set[cs]) {
ctx->xsock--;
ctx->x_set[cs] = 0;
}
}
#if 0
void
fdset_dump( fd_set fset) {
int i;
for (i=0; i<sizeof(fd_set)*8; i++) {
if (FD_IS_SET( i, &fset)) {
putchar('1');
} else {
putchar('0');
}
}
}
#endif
btStream *fetchtorrent( char *url) {
btStream *io;
int curlret;
CURL *hdl = curl_easy_init();
curl_easy_setopt( hdl, CURLOPT_URL, url);
io = bts_create_strstream( BTS_OUTPUT);
curl_easy_setopt( hdl, CURLOPT_FILE, io);
curl_easy_setopt( hdl, CURLOPT_WRITEFUNCTION, writebts);
if ((curlret = curl_easy_perform( hdl)) != CURLE_OK)
{
switch (curlret)
{
case CURLE_COULDNT_CONNECT:
fprintf(stderr, "Failed to transfer URL: could not connect (%d)\n", curlret);
default:
fprintf(stderr, "Failed to transfer URL for reason %d (see curl.h)\n", curlret);
}
exit(1);
}
curl_easy_cleanup( hdl);
return io;
}
gint my_popup_handler(GtkWidget *Menu,
gint row,
gint column,
GdkEventButton *event,
gpointer data )
{
GtkMenu *menu;
GdkEventButton *event_button;
g_return_val_if_fail(data!=NULL,FALSE);
g_return_val_if_fail(GTK_IS_MENU((GtkMenu *)data),FALSE);
g_return_val_if_fail(event!=NULL,FALSE);
menu=GTK_MENU(data);
//if(event_button->button==3)
//{
//get the rows;
gtk_menu_popup(menu,NULL,NULL,NULL,NULL,
event_button->button,event_button->time);
return TRUE;
//}
}////////////////////
void unselection_made(GtkWidget *clist,
gint row,
gint column,
GdkEventButton *event,
gpointer data)
{
//g_print("You unselected row %d. More specifically you clicked in ""column %d, and the text in this cell is %s\n\n",row, column, text);
GtkCList *list;
list=(GtkCList*)data;
printf("%d\n",list->rows-1);
}
void selection_made( GtkWidget *clist,
gint row,
gint column,
GdkEventButton *event,
gpointer data )
{
gchar *text;
GtkWidget*button;
//button=gtk_button_new_with_label("*");
/* Get the text that is stored in the selected row and column
* which was clicked in. We will receive it as a pointer in the
* argument text.
*/
gtk_clist_get_text(GTK_CLIST(clist), row, column, &text);
/* Just prints some information about the selected row */
g_print("You selected row %d. More specifically you clicked in "
"column %d, and the text in this cell is %s\n\n",
row, column, text);
rows=row;
printf("rows=%d\n",rows);
return;
}
void LOAD()
{
FILE*conf;
}
//***********************************************************************************************************************************
int
main( int argc, char *argv[] )
{
//GtkWidget *window;
GtkWidget *POPmenu;
// if(!g_thread_supported())
//g_thread_init(NULL);
gtk_init (&argc, &argv);
Pack();
POPmenu=popmenu_create(clist);
g_signal_connect(GTK_OBJECT(clist),"select_row",GTK_SIGNAL_FUNC(my_popup_handler),(gpointer)POPmenu);
g_signal_connect(GTK_OBJECT(clist), "select_row", GTK_SIGNAL_FUNC(selection_made), NULL);
g_signal_connect(GTK_OBJECT(clist), "remove_row", GTK_SIGNAL_FUNC(unselection_made), (gpointer)clist);
// vod();
//test_for(clist);
//BTlist("1.torrent");
// MsgBox("this is pig window!");
LOAD();
gdk_threads_enter();
gtk_main();return FALSE;
gdk_threads_leave();
}//end of the main
void quit(){
gtk_main_quit();
}
//void on_open_activate (GtkMenuItem *menuitem,gpointer user_data){}
void
on_item4_activate (GtkMenuItem *menuitem,
gpointer user_data){}
GtkWidget *create_window(void){
GtkWidget *window;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "hello this the main!");
return window;
}
GtkWidget *menu_bar(void){
GtkWidget *menu;
GtkWidget *file;
GtkWidget *file_menu;
GtkWidget *open;
GtkWidget *item3;
GtkWidget *item3_menu;
GtkWidget *item4;
menu=gtk_menu_bar_new();
file = gtk_menu_item_new_with_label ("File");
gtk_container_add (GTK_CONTAINER (menu), file);
gtk_widget_show (file);
file_menu = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), file_menu);
open = gtk_menu_item_new_with_label ("Open");
gtk_container_add (GTK_CONTAINER (file_menu), open);
gtk_widget_show (open);
g_signal_connect (GTK_OBJECT (open), "activate",
GTK_SIGNAL_FUNC (on_open_activate),
NULL);
item3 = gtk_menu_item_new_with_label ("item3");
gtk_container_add (GTK_CONTAINER (menu), item3);
item3_menu = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item3), item3_menu);
item4 = gtk_menu_item_new_with_label ("item4");
gtk_container_add (GTK_CONTAINER (item3_menu), item4);
g_signal_connect (GTK_OBJECT (item4), "activate",
GTK_SIGNAL_FUNC (on_item4_activate),
NULL);
return menu;
}
GtkWidget*create_entry(void){
GtkWidget*entry;
entry = gtk_entry_new ();
return entry;
}
GtkWidget*down_button(void){
GtkWidget*button;
button = gtk_button_new_with_label ("Download now!");
return button;
}
void Pack(void){
GtkWidget* window;
GtkWidget*vbox;
GtkWidget*menubar;
GtkWidget*entry;
GtkWidget*button;
GtkWidget*hbox;
mission_window(clist);
window=create_window();
vbox=gtk_vbox_new(FALSE,0);
gtk_widget_show(vbox);
gtk_container_add(GTK_CONTAINER(window),vbox);
menubar=menu_bar();
gtk_widget_show(menubar);
gtk_box_pack_start(GTK_BOX(vbox),menubar,FALSE,FALSE,0);
hbox=gtk_hbox_new(FALSE,0);
gtk_widget_show(hbox);
gtk_box_pack_start(GTK_BOX(vbox),hbox,TRUE,TRUE,0);
entry=create_entry();
gtk_widget_show(entry);
gtk_box_pack_start(GTK_BOX(hbox),entry,TRUE,TRUE,0);
button=down_button();
gtk_widget_show(button);
gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,FALSE,0);
gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NONE);//平整的表面
gtk_widget_show(window);
g_signal_connect(GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(quit),NULL);
}
/*gtk defined
////////////////
GtkWidget* rid_window;
static gchar*title[10]={"Name","Size","Downed","Completed","Status","Peers","Friends","DownSpeed","UpSpeed","TimeLeft"};
const gchar*new_row[10];
static GtkWidget*clist;
/* static GtkWidget*entry_name;
static GtkWidget*entry_fav;//目前这个禁用阿 可惜阿
cacheopen(mandrake9.1/Mandrake91-cd1-inst.i586.iso)
0 of 7802 completed (0 ok)Skipping 0 blocks
cacheopen(mandrake9.1/Mandrake91-cd2-ext.i586.iso)
Skipping 0 blocks
cacheopen(mandrake9.1/Mandrake91-cd3-i18n.i586.iso)
Skipping 0 blocks
似乎通过 不让他cachopen来选择下载 天哪 我终于知道了 哈哈哈
*/
void mission_window(GtkWidget *clist1){
GtkWidget*vbox;
GtkWidget*clist=clist1;
//gchar*text[10]={"see","you","bye","!"};
gchar*rape[10];
rid_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(rid_window),"Mission");
vbox=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(rid_window),vbox);
clist=gtk_clist_new_with_titles(10,title);
gtk_box_pack_start(GTK_BOX(vbox),clist,FALSE,TRUE,5);
//gtk_clist_append(GTK_CLIST(clist),text);
gtk_widget_show_all(rid_window);
}
void thread_filestream(btStream *in,char*fname)
{
}
char* BTlist(char *torrent,btget_arg args_btget,GtkFileSelection*fs){
//torrent=fopen();
//rid_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_title(GTK_WINDOW(rid_window),"");
char *fname;
btStream *in;
btObject *o;
btString *s;
btInteger *i;
btList *l;
btDict *d;
int idx;
int debug = 0;
int opthelp = 0;
int opt;
fname=torrent;
in=bts_create_filestream(fname,BTS_INPUT);
if(benc_get_object(in,&o)){
//MsgBox("read failed");
printf("Read Failed\n");
debug=1;
exit(1);
}
////MsgBox(fname);
//array_list[1]=fname;
printf("fname=%s\n",fname);
if(debug){
btObject_dump(0,o);}
btStream *tmpbts;
unsigned char digest[SHA_DIGEST_LENGTH];
struct btstrbuf out;
d=BTDICT(btObject_val(o,"info"));
tmpbts=bts_create_strstream();
benc_put_dict(tmpbts,d);// need to see benc.c.h
out = bts_get_buf( tmpbts);
SHA1( out.buf, out.len, digest);
printf("info hash-----:");
//array_list[2]="info hash--------:";
//BTSTRING(); is the most nice???!~~~~~~~
for (idx=0; idx<sizeof(digest); idx++) {
printf("%02x",digest[idx]);
}
bts_destroy( tmpbts);
printf("fname=%s\n",fname);
printf("\n");printf("fname=%s\n",fname);
i=BTINTEGER( btObject_val(o, "info/length"));
if (i) {
/* file mode */
btInteger *pl;
s=BTSTRING( btObject_val( o, "info/name"));
printf("file name.....: %s\n", s->buf);
// array_list[3]=s->buf;
s=BTSTRING( btObject_val( o, "info/pieces"));
pl=BTINTEGER( btObject_val(o, "info/piece length"));
printf("file size.....: %lld (%lld * %lld + %lld)\n", i->ival, i->ival/pl->ival, pl->ival, i->ival % pl->ival);
sprintf(size,"%lld",i->ival);
} else {
/* dir mode */
_int64 tsize=0;
s=BTSTRING( btObject_val( o, "info/name"));
printf("directory name: %s\n", s->buf);
//filename_return(s->buf);
dir_name=btString_buf(s);
printf("lksjaf;lkajs;flkjas;fkdj;askjldf;ksjd;fkjas;dfjkas;fkja;sdfsadlkfj;askjf;skljf %s\n",dir_name);
args_btget.p->title[Headname]=dir_name;
l=BTLIST( btObject_val(o, "info/files"));
printf("files.........: %d\n", l->len);
for (idx=0; idx<l->len; idx++) {
int pathel;
btList *filepath;
btInteger *filesize;
filepath=BTLIST( btObject_val( l->list[idx], "path"));
filesize=BTINTEGER( btObject_val( l->list[idx], "length"));
printf(" ");
for (pathel=0; pathel<filepath->len; pathel++) {
if (pathel>0) printf("/");
printf("%s", BTSTRING(filepath->list[pathel])->buf);
}
printf(" (%lld)\n", filesize->ival);
tsize+=filesize->ival;
}
s=BTSTRING( btObject_val( o, "info/pieces"));
i=BTINTEGER( btObject_val(o, "info/piece length"));
printf("archive size..: %lld (%lld * %lld + %lld)\n",
tsize, tsize/i->ival, i->ival, tsize%i->ival);
sprintf(size,"%d",tsize);
args_btget.p->title[1]=size;
neo=size;
}
s=BTSTRING( btObject_val( o, "announce"));
printf("announce url..: %s\n", s->buf);
//MsgBox(s->buf); this successful!
printf("\n");
clist_append(args_btget,clist,torrent,fs);
btObject_destroy( o);
bts_destroy( in);
return "over btlitt()";
}//? end of BTlist()?
void
on_open_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkWidget*file_dialog;
file_dialog=create_fileselection();
gtk_widget_show(file_dialog);//successful!!!
const gchar*filename;
//printf("file_dialog\n");
//if( GTK_FILE_SELECTION (file_dialog)->ok_button) filename=gtk_file_selection_get_filename(file_dialog); printf("dfd%s\n",filename);
}
GtkWidget*
create_fileselection (void)
{
GtkWidget *fileselection;
GtkWidget *ok_button;
GtkWidget *cancel_button;
fileselection = gtk_file_selection_new ("选择文件 Select File");
gtk_object_set_data (GTK_OBJECT (fileselection), "fileselection", fileselection);
gtk_container_set_border_width (GTK_CONTAINER (fileselection), 10);
ok_button = GTK_FILE_SELECTION (fileselection)->ok_button;
gtk_object_set_data (GTK_OBJECT (fileselection), "ok_button", ok_button);
gtk_widget_show (ok_button);
GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
cancel_button = GTK_FILE_SELECTION (fileselection)->cancel_button;
gtk_object_set_data (GTK_OBJECT (fileselection), "cancel_button", cancel_button);
gtk_widget_show (cancel_button);
GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
g_signal_connect (GTK_OBJECT (ok_button), "clicked",
GTK_SIGNAL_FUNC (on_ok_button_clicked),
NULL);
g_signal_connect(GTK_OBJECT( GTK_FILE_SELECTION (fileselection)->ok_button),
"clicked",
GTK_SIGNAL_FUNC(file_select_ok),
GTK_FILE_SELECTION (fileselection));
g_signal_connect (GTK_OBJECT (cancel_button), "clicked",
GTK_SIGNAL_FUNC (on_cancel_button_clicked),
GTK_FILE_SELECTION (fileselection));
return fileselection;
}
void on_ok_button_clicked(){
//clist_append(clist);
printf("on_ok_button_clicked\n");
}
void on_cancel_button_clicked(GtkWidget*button,GtkFileSelection*fs){
gtk_widget_destroy(GTK_WIDGET(fs));
printf("on_cannel_button_clicked\n");}
void *destroy(GtkFileSelection*fs)
{
GtkFileSelection*fs_my=fs;
gtk_widget_destroy(GTK_WIDGET(fs));
}
list_data *tl;
int list_init(gchar*filename,list_data *tl,GtkFileSelection*fs){
list_data *p;
tl=(list_data*)btmalloc(sizeof(list_data));
p=tl;
p->title[Headname]="";
p->title[1]="";
p->title[2]="";p->title[3]="";p->title[4]="";p->title[5]="";p->title[6]="";p->title[7]="";p->title[8]="";
p->title[9]="";p->title[10]="";
args_btget.p=p;
pthread_t thread;
if(pthread_create(&thread,NULL,(void*)BTlist((char*)filename,args_btget,fs),NULL))
{printf("error thread\n");}
btfree(p);//destroy(fs);
tl=tl->next;
}//?end of list_init()?
pthread_t init_thread[256];
pthread_t fs_thread;
void file_select_ok(GtkWidget*button,GtkFileSelection*fs)
{
//GtkWidget*dialog;
char *message[1024];
const gchar*filename;
filename=gtk_file_selection_get_filename(fs);
//if(g_file_test(filename,G_FILE_TEST_IS_DIR))
if(filename){
printf("filename is :%s\n",filename);
//gtk_widget_destroy(GTK_WIDGET(fs));
//list_init((gchar*)filename,tl,fs);
if(pthread_create(&init_thread[poit],NULL,(void*)list_init((gchar*)filename,tl,fs),NULL))
{printf("error thread\n");}
return;
//pthread_create(&fs_thread,NULL,destroy(fs),NULL);
poit++;
printf("filename after destroy the file_selection is :%s\n",filename);
}
else {
printf("No files\n");
//gtk_widget_destroy(GTK_WIDGET(fs));
}
//BTlist((char*)filename,tl);
//gtk_widget_destroy(GTK_WIDGET(fs));
//if(pthread_join(fs_thread,NULL)){printf("fs_thread join error\n");abort();}
return;
}
char* btget(btget_arg args_btget,GtkFileSelection*fs){
btget_arg local_arg;
local_arg=args_btget;
btStream *bts;
struct btContext *ctx = &context;
int cs;
struct sockaddr csin;
int err;
time_t choke_timer;
time_t report_timer;
time_t *reregister_timer=NULL;
int reregister_timers=0;
time_t now;
int ttv;
int tv_slow = 1; /*ms*/
int tv_fast = 0; /*ms*/
btPeer *peer;
int opt;
char *optfile = NULL;
int optquiettmo = 0;
int opturl = 0;
int dl;
//*************************************************************
time(&choke_timer);
time(&report_timer);
// clist_append(dir_name,clist);
#if WIN32
#else
epipe_setup();
#endif
signal( SIGINT, sigint_handler);
signal( SIGTERM, sigint_handler);
btContext_create( ctx, optexaggerate, ".libbtrc");
bts = bts_create_filestream( local_arg.filename, BTS_INPUT);
dl=ctx_loadfile( bts, ctx, optseed);
if (dl < 0) {
bts_perror( errno, "Unable to load .torrent");
optind++;
//continue;
}
if(dl>=reregister_timers) {
reregister_timers=dl+1;
reregister_timer=btrealloc(reregister_timer, sizeof(*reregister_timer)*reregister_timers);
}
time(&reregister_timer[dl]);
bts_destroy( bts);
ctx_startserver( ctx);
for(dl=0; dl<ctx->downloadcount; dl++) {
if (ctx->downloads[dl]) {
ctx_register( ctx, dl);
}
}
on_exit( ctx_exit, ctx);
printf("%d: Server ready...\n", ctx->ss);//***********************8
destroy(fs);
for (;;) {
gdk_threads_enter();
int i;
int readerr;
int writeerr;
int execerr;
int pollerr;
int sa_len;
int complt;
/*
* Select a socket or time out
*/
if (ctx->xsock) {
ttv = tv_fast;
} else {
ttv = tv_slow;
}
// sleep(1);
err = poll( ctx->status, ctx->nstatus, ttv);
#if 0
if (err == EINTR) {
/* caught a signal */
continue;
}
#endif
if (err < 0) { bts_perror(errno, "poll"); abort(); }
#if 0
printf("poll returned %d (xsock=%d)\n", err, ctx->xsock);
#endif
time( &now);
for (cs=0; cs < SOCKID_MAX; cs++) {
/* loop through executable sockets */
if (ctx->x_set[ cs]) {
btPeer *p = ctx->sockpeer[cs];
#if 0
printf("executable %d\n", cs);
#endif
execerr = client_run( ctx, cs, EXECUTE);
if (execerr == 0) {
if ( ctx->x_set[ cs]) {
ctx->x_set[ cs] = 0;
ctx->xsock--;
}
}
if ( kStream_oqlen( &p->ios)) {
/* data is waiting on the output buffer */
#if 0
printf("%d: %d bytes queued for output\n",
cs, kStream_oqlen( &p->ios));
#endif
ctx_setevents( ctx, cs, POLLOUT);
}
if (execerr < 0) {
client_error( ctx, "execute", cs);
} /* if */
} /* if */
} /* for */
for (i=0; i<ctx->nstatus; i++) {
/* for each poll event */
cs = ctx->status[i].fd;
if (ctx->statmap[cs] != i) {
printf("Map synch error stat=%d -> socket=%d -> stat=%d\n",
i, cs, ctx->statmap[cs]);
}
DIE_UNLESS( ctx->statmap[cs] == i);
readerr=0;
writeerr=0;
execerr=0;
pollerr=0;
if (CTX_STATUS_REVENTS( ctx, i) & POLLIN) {
DIE_UNLESS( ctx->status[i].events & POLLIN);
#if 0
printf("readable %d\n", cs);
#endif
/* readable */
if (cs == ctx->ss) {
/* service socket */
sa_len = sizeof(csin);
cs = accept( ctx->ss, &csin, &sa_len);
#if 0
printf("%d: Client connect on %d\n", cs, ctx->listenport);
#endif
if (cs < 0) {
bts_perror( errno, "accept");
} else {
client_init( ctx, cs);
} /* if */
} else {
/* client socket */
btPeer *p = ctx->sockpeer[ cs];
#if 0
printf("peer state is %d\n", p->state);
#endif
readerr = client_run( ctx, cs, READABLE);
if (readerr == 1) {
/* more to read */
if (!ctx->x_set[cs]) {
ctx->x_set[cs] = 1;
ctx->xsock++;
}
}
if ( kStream_oqlen( &p->ios)) {
/* data is waiting on the output buffer */
#if 0
printf("%d: %d bytes queued for output\n",
cs, kStream_oqlen( &p->ios));
#endif
ctx_setevents( ctx, cs, POLLOUT);
}
}
} /* if */
if (CTX_STATUS_REVENTS( ctx, i) & POLLOUT) {
#if 0
printf("writeable %d\n", cs);
#endif
writeerr = client_write( ctx, cs);
#if 0
printf("%d: %d bytes queued for output\n",
cs, kStream_oqlen( &ctx->sockpeer[cs]->ios));
#endif
if (writeerr == 0) {
/* output drained */
ctx_clrevents( ctx, cs, POLLOUT);
if (!ctx->x_set[ cs]) {
/* output buffer is empty, check for more work */
ctx->x_set[ cs] = 1;
ctx->xsock++;
}
}
} /* if */
if (CTX_STATUS_REVENTS( ctx, i) & (POLLERR | POLLHUP | POLLNVAL))
{
int events = CTX_STATUS_REVENTS( ctx, i);
if (events & POLLHUP) {
ctx->sockpeer[cs]->ios.error = BTERR_POLLHUP;
} else if (events & POLLERR) {
ctx->sockpeer[cs]->ios.error = BTERR_POLLERR;
} else if (events & POLLNVAL) {
ctx->sockpeer[cs]->ios.error = BTERR_POLLNVAL;
}
pollerr = -1;
}
if (readerr < 0 || writeerr < 0 || execerr < 0 || pollerr < 0) {
char *act = NULL;
if (readerr<0) act = "read";
if (writeerr<0) act = "write";
if (execerr<0) act = "execute";
if (pollerr<0) act = "poll";
client_error( ctx, act, cs);
} /* if */
peer = ctx->sockpeer[cs];
if ( optsnub && peer &&
!peer->remote.choked &&
!peer->local.snubbed &&
now - peer->lastreceived > 120)
{
printf("%d: Peer unchoked but not sending data for %ld seconds\n",
peer->ios.fd, (long)(now - peer->lastreceived));
peer->local.snubbed = 1;
}
} /* for */
for(dl=0; dl<ctx->downloadcount; dl++) {
if (!ctx->downloads[dl]) continue;
if (now - reregister_timer[dl] > ctx->downloads[dl]->reregister_interval) {
reregister_timer[dl] = now;
ctx_reregister( ctx, dl);
}
}
if (now - report_timer > 0) {
/* show connection status */
report_timer=now;
printf( "Time %ld\n", now);
for(dl=0; dl<ctx->downloadcount; dl++) {
if (!ctx->downloads[dl]) continue;
complt = bs_countBits( &ctx->downloads[dl]->fileset.completed);
if ((complt == ctx->downloads[dl]->fileset.npieces) &&
!ctx->downloads[dl]->complete) {
/* close all files, only once */
int i;
ctx_complete (ctx, dl);
cacheclose();
for (i=0; i<ctx->downloads[dl]->peerset.len; i++) {
if (ctx->downloads[dl]->peerset.peer[i] == NULL) continue;
/* All blocks should've been cancelled during reception,
* completion is filled in as we write to disk. */
DIE_UNLESS (ctx->downloads[dl]->peerset.peer[i]->currentPiece == NULL);
}
}
if (optverbose) {
peer_dump( &ctx->downloads[dl]->peerset);
}
printf("%d%% (%d of %d) ", //多么经典的地方阿 这里就是 百分比的地方 接!
complt * 100 / ctx->downloads[dl]->fileset.npieces,
complt, ctx->downloads[dl]->fileset.npieces);
sprintf(down_data,"%d%% (%d of %d) ", //多么经典的地方阿 这里就是 百分比的地方 接!
complt * 100 / ctx->downloads[dl]->fileset.npieces,
complt, ctx->downloads[dl]->fileset.npieces);
neo_down=down_data;
args_btget.p->title[3]=down_data;
//////////////////////////////////////
set_Text(args_btget.p,clist);
peer_summary( &ctx->downloads[dl]->peerset);
if (optverbose) {
printf("\n----\n");
} else {
printf("%79s\r", "");
}
}
}
if (now - choke_timer > 30) {
/* recalculate favorite peers */
choke_timer=now;
/* should this be done for all torrents at the same time? */
for(dl=0; dl<ctx->downloadcount; dl++) {
if (!ctx->downloads[dl]) continue;
peer_favorites( ctx, &ctx->downloads[dl]->peerset);
}
}
#if 0
if (optquiettmo > 0) {
static int lastcheck = 0;
static int donetime = 0;
int check = (ctx->complete == 1) && peer_allcomplete( &ctx->downloads[0]->peerset);
if (check) {
if (!lastcheck) {
printf("=== All peers done!\n");
donetime = now;
} else {
if (now - donetime > optquiettmo) {
printf("Peers have been quiescent for %ld secs, exiting.\n", (now - donetime));
exit (0);
}
}
}
lastcheck = check;
}
#endif
gdk_threads_leave();
g_usleep(123);
}///end of for(;;)
return 0;
}
//if_first(GtkWidget*clist);
pthread_t my_thread[256];
//int poit=0;
void clist_append(btget_arg args_btget,GtkWidget*clist,char*torrent,GtkFileSelection*fs)
{
printf("dir_name=%s\n",dir_name);
//gchar*title[10]={""};
GtkCList *list;
list=(GtkCList*)clist;
//list_db->row=-1;
//char array[123]={'d','v','a','v','e','d','v','a','f','f','f','d','r','g','s'};
if(list->rows<0)
{args_btget.p->row=0;}
else{
first=FALSE;
args_btget.p->row=gtk_clist_find_row_from_data((GtkCList*)clist,(gpointer)args_btget.p->title[Headname]);
}
gtk_clist_append(GTK_CLIST(clist),args_btget.p->title);
//btget(torrent,p);
args_btget.filename=torrent;
//btget (args_btget);
g_thread_create((void*)btget(args_btget,fs),NULL,FALSE,NULL);
//if(pthread_create(&my_thread[poit],NULL,(void*)btget(args_btget,fs),NULL)){printf("error thread\n");}
//poit++;
return;
}//? end ?
GtkWidget*clist_return(GtkWidget*clist)
{
return clist;
}
/*
void gtk_clist_set_text (GtkCList *clist,
gint row,
gint column,
const gchar *text);
gtk_clist_find_row_from_data (GtkCList *clist,
gpointer data);
*/
//size->column=1,down_data=2
void set_Text(list_data*p,GtkWidget*clist){
gchar*text;// gtk_clist_find_row_from_data(),
//list_db=(list_data*)btmalloc(sizeof(list_data));
if( gtk_clist_find_row_from_data((GtkCList*)clist,(gpointer)p->title[Headname])){
gtk_clist_set_text (GTK_CLIST(clist),
p->row,
1,
(gchar*)p->title[2]);
gtk_clist_set_text (GTK_CLIST(clist),
p->row,
2,
(gchar*)p->title[3]);
}
else{return;}
}
/*void if_first(GtkWidget*clist,){
if((GtkCList*)clist->rows<0) {list_db->row=0;}
else
{first=FALSE;}
}
可以在btget里面设置一个过程检查时候删除或者添加了 列表 然后break;然后就能tl=tl->next;链表阿
*/
void on_del_activate(gpointer data){
printf("del_activate\n");
gtk_clist_remove((GtkCList*)data,rows);
printf("rows=%d\n",rows);
}
GtkWidget*popmenu_create(GtkWidget *clist){
GtkWidget *file_menu;
GtkWidget*file;
GtkWidget *del;
GtkWidget *start;
GtkWidget *stop;
GtkWidget*sep;
file = gtk_menu_item_new_with_label ("File");
file_menu = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), file_menu);
sep=gtk_menu_item_new_with_label("==============");
gtk_container_add (GTK_CONTAINER (file_menu), sep);
start= gtk_menu_item_new_with_label ("start");
gtk_container_add (GTK_CONTAINER (file_menu), start);
stop=gtk_menu_item_new_with_label ("stop");
gtk_container_add (GTK_CONTAINER (file_menu), stop);
del = gtk_menu_item_new_with_label ("del");
gtk_container_add (GTK_CONTAINER (file_menu), del);
gtk_signal_connect_object(GTK_OBJECT (del), "activate",
GTK_SIGNAL_FUNC (on_del_activate),
(gpointer)clist);
gtk_widget_show_all(file_menu);
return file_menu;
}
[/code:1] |
|