This patch adds 4 new commands to the remote shell:
"close" - closes Gnet connections
"open" - opens Gnet connections
"props" - displays all available properties
"stat" - a one-page status report for the node
Note: I'm still learning my way around GtkG, so don't be surprised if I
missed a "better way" of doing something.
Note2: One thing I am moderately unhappy about is that I had to add a global
variable (only visible within shell.c) to capture the current time - that
value is passed around via function parameters, but as far as I can tell
it's not stored in any property. So I grab it when it's available in
"shell_timer" and store in the the global "shell_current_stamp" for use in
other functions.
Lloyd Bryant
--- shell.c.orig 2006-11-01 03:00:21.000000000 -0700
+++ shell.c 2006-11-01 04:42:58.000000000 -0700
@@ -60,6 +60,7 @@
#define REPLY_GOOD_BYE 900
static GSList *sl_shells = NULL;
+time_t shell_current_stamp;
typedef struct gnutella_shell {
struct gnutella_socket *socket;
@@ -97,7 +98,11 @@
CMD_HORIZON,
CMD_RESCAN,
CMD_SHUTDOWN,
- CMD_NODES
+ CMD_NODES,
+ CMD_STAT,
+ CMD_CLOSE,
+ CMD_OPEN,
+ CMD_PROPS
};
static const struct {
@@ -116,6 +121,10 @@
{ CMD_RESCAN, "rescan" },
{ CMD_SHUTDOWN, "shutdown" },
{ CMD_NODES, "nodes" },
+ { CMD_STAT, "stat" },
+ { CMD_CLOSE, "close" },
+ { CMD_OPEN, "open" },
+ { CMD_PROPS, "props" },
};
@@ -887,6 +896,205 @@
return REPLY_READY;
}
+/**
+ * Displays assorted status information
+ */
+static guint
+shell_exec_stat(gnutella_shell_t *sh)
+{
+ char buf[2048];
+ guint32 peermode;
+ char peerstring[9];
+ gboolean online_mode;
+ time_t last_switch;
+ char switchstring[25];
+ time_t last_check;
+ char checkstring[25];
+ host_addr_t ipv4_address;
+ host_addr_t ipv6_address;
+ char ipv4_addr[18];
+ char ipv6_addr[30];
+ time_t current_ip4_stamp;
+ time_t current_ip6_stamp;
+ gint session_uptime;
+ gint ip4_uptime;
+ gint ip6_uptime;
+ guint32 listen_port;
+ gboolean tcp_limited;
+ gboolean udp_limited;
+ char jamboozled[9];
+ guint32 current_ultras;
+ guint32 max_ultras;
+ guint32 current_normals;
+ guint32 max_normals;
+ guint32 current_leafs;
+ guint32 max_leafs;
+ gnet_bw_stats_t gnet_in_stats;
+ gnet_bw_stats_t gnet_out_stats;
+ gnet_bw_stats_t http_in_stats;
+ gnet_bw_stats_t http_out_stats;
+ gnet_bw_stats_t leaf_in_stats;
+ gnet_bw_stats_t leaf_out_stats;
+ char gnet_out[13];
+ char gnet_in[13];
+ char http_out[13];
+ char http_in[13];
+ char leaf_out[13];
+ char leaf_in[13];
+
+ /* Session info */
+ gnet_prop_get_guint32_val(PROP_CURRENT_PEERMODE, &peermode);
+ if (0 == peermode)
+ g_strlcpy(peerstring, "Leaf", 9);
+ else
+ g_strlcpy(peerstring, "Ultra", 9);
+ gnet_prop_get_boolean_val(PROP_ONLINE_MODE, &online_mode);
+ if(!online_mode)
+ g_strlcpy(peerstring, "(Closed)", 9);
+ gnet_prop_get_timestamp_val(PROP_NODE_LAST_ULTRA_LEAF_SWITCH, &last_switch);
+ if (last_switch < start_stamp)
+ last_switch = start_stamp;
+ gnet_prop_get_timestamp_val(PROP_NODE_LAST_ULTRA_CHECK, &last_check);
+ timestamp_to_string_buf(last_switch, switchstring, 24);
+ if (last_check < start_stamp)
+ last_check = start_stamp;
+ timestamp_to_string_buf(last_check, checkstring, 24);
+ session_uptime = (gint) delta_time(shell_current_stamp, start_stamp);
+
+ /* IP info */
+ gnet_prop_get_ip_val(PROP_LOCAL_IP, &ipv4_address);
+ ipv4_to_string_buf(ipv4_address.addr.ipv4, ipv4_addr, sizeof(ipv4_addr));
+ gnet_prop_get_timestamp_val(PROP_CURRENT_IP_STAMP, ¤t_ip4_stamp);
+ ip4_uptime = (gint) delta_time(shell_current_stamp, current_ip4_stamp);
+ gnet_prop_get_ip_val(PROP_LOCAL_IP6, &ipv6_address);
+ ipv6_to_string_buf(ipv6_address.addr.ipv6, ipv6_addr, sizeof(ipv6_addr));
+ gnet_prop_get_timestamp_val(PROP_CURRENT_IP_STAMP, ¤t_ip6_stamp);
+ ip6_uptime = (gint) delta_time(shell_current_stamp, current_ip6_stamp);
+ gnet_prop_get_guint32_val(PROP_LISTEN_PORT, &listen_port);
+
+ /* Jamboozled status */
+ gnet_prop_get_boolean_val(PROP_IS_FIREWALLED, &tcp_limited);
+ gnet_prop_get_boolean_val(PROP_IS_UDP_FIREWALLED, &udp_limited);
+ if (tcp_limited && udp_limited)
+ g_strlcpy(jamboozled, "TCP,UDP", 8);
+ else if (tcp_limited)
+ g_strlcpy(jamboozled, "TCP", 8);
+ else if (udp_limited)
+ g_strlcpy(jamboozled, "UDP", 8);
+ else
+ g_strlcpy(jamboozled, "No", 8);
+
+ /* Node counts */
+ gnet_prop_get_guint32_val(PROP_NODE_ULTRA_COUNT, ¤t_ultras);
+ gnet_prop_get_guint32_val(PROP_NODE_NORMAL_COUNT, ¤t_normals);
+ gnet_prop_get_guint32_val(PROP_NODE_LEAF_COUNT, ¤t_leafs);
+ if (0 == peermode)
+ gnet_prop_get_guint32_val(PROP_MAX_ULTRAPEERS, &max_ultras);
+ else
+ gnet_prop_get_guint32_val(PROP_MAX_CONNECTIONS, &max_ultras);
+ gnet_prop_get_guint32_val(PROP_NORMAL_CONNECTIONS, &max_normals);
+ gnet_prop_get_guint32_val(PROP_MAX_LEAVES, &max_leafs);
+
+ /* Bandwidths */
+ gnet_get_bw_stats(BW_GNET_IN, &gnet_in_stats);
+ g_strlcpy(gnet_in, short_rate(gnet_in_stats.average, FALSE), 12);
+ gnet_get_bw_stats(BW_GNET_OUT, &gnet_out_stats);
+ g_strlcpy(gnet_out, short_rate(gnet_out_stats.average, FALSE), 12);
+ gnet_get_bw_stats(BW_HTTP_IN, &http_in_stats);
+ g_strlcpy(http_in, short_rate(http_in_stats.average, FALSE), 12);
+ gnet_get_bw_stats(BW_HTTP_OUT, &http_out_stats);
+ g_strlcpy(http_out, short_rate(http_out_stats.average, FALSE), 12);
+ gnet_get_bw_stats(BW_LEAF_IN, &leaf_in_stats);
+ g_strlcpy(leaf_in, short_rate(leaf_in_stats.average, FALSE), 12);
+ gnet_get_bw_stats(BW_LEAF_OUT, &leaf_out_stats);
+ g_strlcpy(leaf_out, short_rate(leaf_out_stats.average, FALSE), 12);
+
+ /* Output */
+ gm_snprintf(buf, sizeof(buf),
+ "___________________________________________________________\n"
+ "| Status |\n"
+ "|=========================================================|\n"
+ "| Mode: %-8s Last Switch: %-19s |\n"
+ "| Uptime: %-9s Last Check: %-19s |\n"
+ "|=========================================================|\n"
+ "| IP Address: %-17s Uptime: %-9s |\n"
+ "| Port: %-5d Limited: %-7s |\n"
+ "|---------------------------------------------------------|\n"
+ "| IP Address V6: %-21s Uptime: %-9s |\n"
+ "| Port: %-5d Limited: %-7s |\n"
+ "|=========================================================|\n"
+ "| Connected Peers: %-4d |\n"
+ "| Ultra %4d/%-4d Normal %4d/%-4d Leaf %4d/%-4d |\n"
+ "|=========================================================|\n"
+ "| Bandwidth: GNet HTTP Leaf |\n"
+ "|---------------------------------------------------------|\n"
+ "| In-> %11s %11s %11s |\n"
+ "| Out-> %11s %11s %11s |\n"
+ "|_________________________________________________________|\n",
+ peerstring, switchstring, short_time(session_uptime), checkstring,
+ ipv4_addr,short_time(ip4_uptime),listen_port, jamboozled,
+ ipv6_addr,short_time(ip6_uptime),listen_port, jamboozled,
+ (current_ultras + current_normals + current_leafs),
+ current_ultras, max_ultras, current_normals, max_normals,
+ current_leafs, max_leafs,
+ gnet_in, http_in, leaf_in, gnet_out, http_out, leaf_out
+ );
+
+ shell_write(sh, buf);
+
+
+ return REPLY_READY;
+}
+
+/**
+ * Close GNet connections
+ */
+static guint
+shell_exec_close(gnutella_shell_t *sh)
+{
+
+ gnet_prop_set_boolean_val(PROP_ONLINE_MODE, FALSE);
+ shell_write(sh, "Closing GNet connections\n");
+
+ return REPLY_READY;
+}
+
+/**
+ * Open GNet connections
+ */
+static guint
+shell_exec_open(gnutella_shell_t *sh)
+{
+
+ gnet_prop_set_boolean_val(PROP_ONLINE_MODE, TRUE);
+ shell_write(sh, "Opening GNet connections\n");
+
+ return REPLY_READY;
+}
+
+/**
+ * Display all properties
+ */
+static guint
+shell_exec_props(gnutella_shell_t *sh)
+{
+ guint32 current_prop;
+ char prop1[35];
+ char prop2[35];
+ char buf[80];
+
+ for (current_prop = PROP_READING_HOSTFILE; current_prop < GNET_PROPERTY_END; current_prop +=2) {
+ g_strlcpy(prop1, gnet_prop_name(current_prop), 34);
+ if (current_prop < GNET_PROPERTY_END)
+ g_strlcpy(prop2, gnet_prop_name(current_prop + 1), 34);
+ else
+ prop2[0] = '\0';
+ gm_snprintf(buf, 80, "%-34s %-34s\n", prop1, prop2);
+ shell_write(sh, buf);
+ }
+
+ return REPLY_READY;
+}
/**
* Takes a command string and tries to parse and execute it.
@@ -925,6 +1133,10 @@
"whatis <property>\n"
"horizon [ALL]\n"
"rescan\n"
+ "stat\n"
+ "close\n"
+ "open\n"
+ "props\n"
"shutdown\n"
"quit\n"
"help\n");
@@ -969,6 +1181,18 @@
case CMD_NODES:
reply_code = shell_exec_nodes(sh, args);
break;
+ case CMD_STAT:
+ reply_code = shell_exec_stat(sh);
+ break;
+ case CMD_CLOSE:
+ reply_code = shell_exec_close(sh);
+ break;
+ case CMD_OPEN:
+ reply_code = shell_exec_open(sh);
+ break;
+ case CMD_PROPS:
+ reply_code = shell_exec_props(sh);
+ break;
case CMD_ADD:
case CMD_NOOP:
case CMD_UNKNOWN:
@@ -1322,6 +1546,7 @@
{
GSList *sl, *to_remove = NULL;
+ shell_current_stamp = now;
for (sl = sl_shells; sl != NULL; sl = g_slist_next(sl)) {
gnutella_shell_t *sh = sl->data;
time_delta_t timeout = remote_shell_timeout;
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Gtk-gnutella-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtk-gnutella-devel