1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 10:35:10 +00:00

net stuff

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@110 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
Brenden Matthews 2005-08-15 00:35:51 +00:00
parent 5968a83614
commit 30cfbc684c
9 changed files with 173 additions and 100 deletions

View File

@ -7,6 +7,8 @@
*/
#include "conky.h"
#include "remoted.h"
#include "remotec.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -176,7 +178,7 @@ void update_stuff()
}
prepare_update();
/* client(); this is approximately where the client should be called */
#define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
if (NEED(INFO_UPTIME))
@ -229,8 +231,6 @@ void update_stuff()
last_metar_update = current_update_time;
}
#endif
if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS)) &&
current_update_time - last_meminfo_update > 6.9) {
update_meminfo();

View File

@ -3,11 +3,7 @@
*
* This program is licensed under BSD license, read COPYING
*
<<<<<<< conky.c
* $Id$
=======
* $Id$
>>>>>>> 1.58
*/
#include "conky.h"
@ -2801,6 +2797,7 @@ static void draw_string(const char *s)
if (out_to_console) {
printf("%s\n", s);
}
/* daemon_run(s); the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
strcpy(tmpstring1, s);
pos = 0;
added = 0;

View File

@ -427,8 +427,9 @@ char *get_freq()
int i;
char *cpuspeed;
if ((cpuspeed = (char *) malloc(16)) == NULL)
exit(1);
if ((cpuspeed = (char *) malloc(16)) == NULL) {
CRIT_ERR("get_freq()");
}
i = 0;
if ((i = get_cpu_speed()) > 0) {

70
ftp.c
View File

@ -934,73 +934,3 @@ int disconnectFtp(void)
ftpFd = -1;
return (0);
}
#ifdef STANDALONE
void ftpList(void *userData, const char *filename, const char *attrib,
const char *owner, const char *group, unsigned long size,
int links, int year, const char *month, int day, int minute)
{
printf("%s %s %s %ld %s\n", attrib, owner, group, size, filename);
}
void ftpData(void *userData, const char *data, int len)
{
if (userData == NULL)
return;
if (len <= 0) {
fclose(userData);
return;
}
fwrite(data, len, 1, userData);
}
int main(int argc, char **argv)
{
int res;
FILE *output;
const char *tstfile = "tstfile";
initFtp();
if (argc > 1) {
res = connectFtp(argv[1], 0);
if (argc > 2)
tstfile = argv[2];
} else
res = connectFtp("localhost", 0);
if (res < 0) {
fprintf(stderr, "Couldn't connect to localhost\n");
exit(1);
}
res = changeFtpDirectory("/linux");
if (res < 0) {
fprintf(stderr, "disconnected\n");
disconnectFtp();
exit(1);
}
if (res == 0) {
fprintf(stderr, "/linux : CWD failed\n");
} else {
fprintf(stderr, "/linux : CWD successful\n");
}
res = changeFtpDirectory("/toto");
if (res < 0) {
fprintf(stderr, "disconnected\n");
disconnectFtp();
exit(1);
}
if (res == 0) {
fprintf(stderr, "/toto : CWD failed\n");
} else {
fprintf(stderr, "/toto : CWD successful\n");
}
listFtp(ftpList, NULL);
output = fopen("/tmp/tstdata", "w");
if (output != NULL) {
if (getFtp(ftpData, (void *) output, tstfile) < 0)
fprintf(stderr, "Failed to get file %s\n",
tstfile);
}
disconnectFtp();
exit(0);
}
#endif /* STANDALONE */

View File

@ -172,7 +172,7 @@ void update_metar()
pthread_cancel(thread1);
status = 2;
iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
} else { /* status must be 1 */
} else if (status == 1) { /* status must be 1 */
pthread_join(thread1, NULL);
status = 2;
iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);

View File

@ -11,3 +11,59 @@
okay, nothing here right now. thanks for coming out
*/
#include "conky.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 3490 // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
void client()
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
if ((he=gethostbyname("localhost")) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}

View File

@ -10,4 +10,6 @@
okay, nothing here right now. thanks for coming out
*/
*/
void client();

121
remoted.c
View File

@ -1,39 +1,124 @@
/*
* Conky, a system monitor, based on torsmo
*
* This program is licensed under BSD license, read COPYING
*
* $Id$
*/
* Conky, a system monitor, based on torsmo
*
* This program is licensed under BSD license, read COPYING
*
* $Id$
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
static pthread_t daemon;
static pthread_t daemon_thread;
static int daemon_status = 0;
static char *data;
/* okay, heres how it will basically work.
* when something connects, it will first send the conkyrc on the local (daemonized) server
* after this, it will simply continue to send all the buffered text to the remote client
* http://analyser.oli.tudelft.nl/beej/mirror/net/html/
* http://www.kegel.com/c10k.html
*/
* when something connects, it will first send the conkyrc on the local (daemonized) server
* after this, it will simply continue to send all the buffered text to the remote client
* http://analyser.oli.tudelft.nl/beej/mirror/net/html/
* http://www.kegel.com/c10k.html
*/
void *daemon_thread()
#define MYPORT 3490 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
void sigchld_handler(int s)
{
/* do something */
return NULL;
while(wait(NULL) > 0);
}
static void daemon_loop()
void *daemon_loop()
{
/* do something */
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int sin_size;
struct sigaction sa;
int yes=1;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
== -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
if (send(new_fd, data, 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
return 0;
}
void daemon_run(const char *s)
{
/* create thread, keep an eye on it */
data = s;
int iret;
if (!daemon_status) {
daemon_status = 1;
iret = pthread_create(&daemon, NULL, daemon_thread, NULL);
iret = pthread_create(&daemon_thread, NULL, daemon_loop, NULL);
} else if (daemon_status == 1) {
/* thread is still running, we'll just wait for it to finish for now */
pthread_join(daemon, NULL);
pthread_join(daemon_thread, NULL);
daemon_status = 0;
} else {
/* something else */

View File

@ -6,4 +6,6 @@
* $Id$
*/
static void daemon_loop();
#include "conky.h"
void daemon_run(const char *s);