[Zope-CVS] CVS: Packages/pcgi/MrCreosote - README.MrCreosote:1.1 creosote.c:1.1 creosote.h:1.1 creosote.py:1.1

Fred L. Drake, Jr. fred@zope.com
Thu, 20 Mar 2003 15:39:56 -0500


Update of /cvs-repository/Packages/pcgi/MrCreosote
In directory cvs.zope.org:/tmp/cvs-serv8256/MrCreosote

Added Files:
	README.MrCreosote creosote.c creosote.h creosote.py 
Log Message:
Make the PCGI support code available via CVS without having to maintain and
distribute it as part of the Zope application server.  The appserver still
contains the necessary support to allow a PCGI-capable web server to talk
to it.


=== Added File Packages/pcgi/MrCreosote/README.MrCreosote ===
README.MrCreosote - JeffBauer@bigfoot.com - May 21, 1998

MrCreosote is a lightweight message-passing idiom I use
  to debug multi-processor/multi-user code sometimes.i
  It's basically just a matter of tossing out datagrams
  (connectionless sockets).  I'll document it later, in
  case anyone finds it useful for debugging.  For now,
  just ignore the contents of this directory.



=== Added File Packages/pcgi/MrCreosote/creosote.c ===
/* creosote.c */

#include "creosote.h"

struct MrCreosote CreosoteServer;

void closeMrCreosote()
{
    struct MrCreosote *c = &CreosoteServer;
    if (c->socket)
    {
#ifdef UNIX
        close(c->socket);
#endif
#ifdef WIN32
        closesocket(c->socket);
        WSACleanup();
#endif
    }
    c->socket = 0;
}

#ifdef UNIX
int initializeMrCreosote()
{
    struct MrCreosote *c = &CreosoteServer;
    SOCKADDR_IN addr, *serv;
    creosote_socket *s;

    serv = &(c->serv_addr);
    s = &(c->socket);

    c->port = CREOSOTE_PORT;
    strcpy(c->host, CREOSOTE_HOST);

    bzero(serv, sizeof(*serv));
    serv->sin_family = AF_INET;
    serv->sin_addr.s_addr = inet_addr(c->host);
    serv->sin_port = htons(c->port);

    if ((*s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        return(-1);
    }

    bzero((char *) &addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(0);

    if (bind(*s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    {
        return(-1);
    }

    return(0);
}
#endif
#ifdef WIN32
int initializeMrCreosote()
{
    struct MrCreosote *c = &CreosoteServer;
    SOCKADDR_IN *serv;
    creosote_socket *s;
    WSADATA WsaData;
    int enable = 1;

    s = &(c->socket);

    if (!c->port)
    {
        c->port = CREOSOTE_PORT;
    }

    if (!c->host[0])
    {
        strcpy(c->host, CREOSOTE_HOST);
    }

    if (0 != WSAStartup(MAKEWORD(2,0), &WsaData))
    {
        return(-1);
    }

    /* Set the address */
    serv = &(c->serv_addr);
    serv->sin_family = AF_INET;
    serv->sin_addr.s_addr = inet_addr(c->host);
    serv->sin_port = htons((short)c->port);

    /* Create socket */
    *s = socket(AF_INET, SOCK_DGRAM, 0);
    if (INVALID_SOCKET == *s) { return(-1); }

    /* Permit the socket to broadcast */
    if (SOCKET_ERROR == setsockopt(*s, SOL_SOCKET, SO_BROADCAST, (char *) &enable, sizeof(enable)))
    {
        return(-1);
    }

    return(0);
}
#endif

void setMrCreosoteHost(char *host)
{
    /* 
       If not using localhost, setMrCreosoteHost() must be called
       before initializeMrCreosote(). 
    */
    strcpy(CreosoteServer.host, host);
}

void setMrCreosotePort(int port)
{
    /* must be done before initializeMrCreosote() is called */
    CreosoteServer.port = port;
}

void spewMrCreosote(char *msg)
{
    struct MrCreosote *c = &CreosoteServer;
    sendto(c->socket, msg, strlen(msg), 0, (struct sockaddr *) &(c->serv_addr), sizeof(c->serv_addr));
}

#if 0
int main(int argc, char *argv[])
{
    if ( (initializeMrCreosote()) < 0)
    {
        printf("initializeMrCreosote() failed\n");
        exit(1);
    }
    spew("Hello, world");
    closeMrCreosote();
    return(0);
}
#endif


=== Added File Packages/pcgi/MrCreosote/creosote.h ===
/* creosote.h */

#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>

#define CREOSOTE_HOST  "127.0.0.1"
#define CREOSOTE_PORT  7739
#define spew(x) spewMrCreosote(x)

#ifdef UNIX
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef int         creosote_socket;
typedef struct sockaddr_in SOCKADDR_IN;
#endif

#ifdef WIN32
#include <windows.h>
#include <iostream.h>
#include <winsock.h>
typedef SOCKET      creosote_socket;
#endif

struct MrCreosote
{
    char host[256];
    int port;
    creosote_socket socket;
    SOCKADDR_IN serv_addr;
};

/* Declarations */
void closeMrCreosote();
int initializeMrCreosote();
void spewMrCreosote(char *msg);


=== Added File Packages/pcgi/MrCreosote/creosote.py ===
#!/usr/bin/env python
# creosote.py - lightweight message passing with datagrams
# JeffBauer@bigfoot.com

import sys, socket

BUFSIZE = 1024
PORT = 7739

def spew(msg, host='localhost', port=PORT):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(('', 0))
    s.sendto(msg, (host, port))

def bucket(port=PORT, logfile=None):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(('', port))
    print 'creosote bucket waiting on port: %s' % port
    if logfile:
        f = open(logfile, 'a+')
    while 1:
        data, addr = s.recvfrom(BUFSIZE)
        print `data`[1:-1]
        if logfile:
            f.write(`data`[1:-1]+'\n')
            f.flush()

class MrCreosote:
    """lightweight message passing with datagrams"""
    def __init__(self, host='localhost', port=PORT):
        self.host = host
        self.port = port
        self.client = None
        self.disabled = 0
        self.redirect_server = None
        self.redirect_client = None
    def bucket(self, logfile=None):
        bucket(self.port, logfile)
    def redirector(self, host, port=PORT):
        if self.disabled:
            return
        if self.redirect_server == None:
            self.redirect_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.redirect_server.bind(('', self.port))
        if self.redirect_client == None:
            self.redirect_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.redirect_client.bind(('', 0))
        while 1:
            data, addr = self.redirect_server.recvfrom(BUFSIZE)
            self.redirect_client.sendto(data, (host, port))
    def spew(self, msg):
        if self.disabled:
            return
        if self.client == None:
            self.client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.client.bind(('', 0))
        self.client.sendto(msg, (self.host, self.port))

if __name__ == '__main__':
    """usage: creosote [message]"""
    if len(sys.argv) < 2:
        bucket()
    else:
        from string import joinfields
        spew(joinfields(sys.argv[1:],' '))