/* * This file part of gsopcast - A gtk front-end of p2p tv sopcast. * http://lianwei3.googlepages.com/home2 * Copyright (C) 2006 Wei Lian * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "header.h" #include "socket.h" #include "fork.h" #include "iostatistics.h" #include "iochannel.h" char sop_ip[128]; extern gboolean flag_player; guint ptimer_statistics = 0; static guint ptimer_echo = 0; Channel iostatistics = { 0, 0 }; int elapsed_seconds; static gchar sta_buf[128]; static int sta_read, sta_size; extern char outport[8]; extern GtkWidget *entry_status; extern pid_t pid_sop; static int socket_status; //----------------------------------------------------- gboolean statistics_monitor(void *) { /////////////////connect to server socket_status = connect_to_server("127.0.0.1", atoi(outport)); if (socket_status < 0) { ///show state char status[64]; sprintf(status, "Connecting %s", elapsed_seconds % 4 ? ((elapsed_seconds % 4) % 3 ? (((elapsed_seconds % 4) % 3) % 2 ? "." : "..") : "...") : " "); elapsed_seconds++; gtk_entry_set_text(GTK_ENTRY(entry_status), status); ///relaunch sp-sc if (pid_sop == -2) fork_sop(sop_ip); return TRUE; } ///write int i; for (i = 0; i < 2; i++) write(socket_status, "state\ns\n", sizeof("state\ns\n")); /////////////////// ///set initial value memset(sta_buf, 0, sizeof(sta_buf)); sta_read = 0; sta_size = sizeof(sta_buf); ///put socket_status to io channel if (iostatistics.tag == 0) { create_iochannel(iostatistics, socket_status, io_socket_statistics); } else fprintf(stderr, "attempt to create multiple io channels!\n"); ////////// return FALSE; } //----------------------------------------------------- void statistics_show(char *buf) { char *savept = NULL; guint value[6]; value[0] = atoi(strtok_r(buf, " ", &savept)); int i; for (i = 1; i < 6; i++) value[i] = atoi(strtok_r(NULL, " ", &savept)); char *str = g_strdup_printf("%3d%%,ur=%2dk,dr=%3dk,us=%2dk,ds=%3dk,peers=%2d", value[0], value[1] / 1024, value[2] / 1024, value[3] / 1024, value[4] / 1024, value[5]); gtk_entry_set_text(GTK_ENTRY(entry_status), str); free(str); ///start player if (value[0] > 3 && flag_player) { fork_player(); flag_player = FALSE; } } //----------------------------------------------------- gboolean io_socket_statistics(GIOChannel * source, GIOCondition cond, gpointer d) { GError *error = NULL; // for error handling GIOStatus status; // save the reading status gsize bytes_read; // save the number of chars read ////data availible if ((status = g_io_channel_read_chars(source, sta_buf + sta_read, sta_size, &bytes_read, &error)) == G_IO_STATUS_NORMAL && bytes_read > 0) { sta_read += bytes_read; sta_size -= bytes_read; char *ptr; if ((ptr = strstr(sta_buf, "\n")) != 0) { *ptr = 0; statistics_show(sta_buf); ///restore to default value memset(sta_buf, 0, sizeof(sta_buf)); sta_read = 0; sta_size = sizeof(sta_buf); ///echo /// int fd = g_io_channel_unix_get_fd(source); ptimer_echo = g_timeout_add(1000, echo, NULL); } } ////quit else if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) { ///close io channel delete_iochannel(iostatistics); ///stop timer if (ptimer_statistics != 0) g_source_remove(ptimer_statistics); ///remove timer if (ptimer_echo != 0) g_source_remove(ptimer_echo); ///restart gtk_entry_set_text(GTK_ENTRY(entry_status), "Restart monitoring"); printf("Restart monitoring\n"); ptimer_statistics = g_timeout_add(1000, statistics_monitor, NULL); return FALSE; } else { printf("rest options\n"); } //g_assert (status == G_IO_STATUS_AGAIN);//resource temporarily unavailable, try again return TRUE; /* keep the event source */ } gboolean echo(void *) { write(socket_status, "s\n", sizeof("s\n")); return FALSE; }