Difference between revisions of "Peerd.c"

From Organic Design wiki
(oops, crashes if no args supplied)
(oops - pasted wrong script in last edit!)
Line 1: Line 1:
 
// [[[[http://www.organicdesign.co.nz/peerd|peerd]]]] - nodal p2p wiki daemon
 
// [[[[http://www.organicdesign.co.nz/peerd|peerd]]]] - nodal p2p wiki daemon
// This article and all its includes are licenced under LGPL
+
// [[[[http://www.organicdesign.co.nz/peerd/files/C|C language version]]]] for *ix,OSX,NT platforms
// GPL: [[[[http://www.gnu.org/copyleft/lesser.html]]]]
+
// This article and all its includes are licenced under [[[[http://www.gnu.org/copyleft/lesser.html|LGPL]]]]
// SRC: [[[[http://www.organicdesign.co.nz/util.c]]]]
+
// SRC: [[[[http://www.organicdesign.co.nz/peerd.c]]]]
// included in [[[[http://www.organicdesign.co.nz/category:peerd/files/C|peerd]]]][[[[http://www.organicdesign.co.nz/peerd.c|/peerd.c]]]]
+
// NOTE: for automatic source syncronisation with peers in the field,
 +
//      keep [[[[Bender/fileSync]]]] up to date!
 +
// Compiled in Win32 by [[[[peerd.c/win32-makefile]]]]
 +
// Compiled in Linux and OSX by [[[[rp]]]]
 +
// [[[[peerd.c/current|Current development]]]]
  
 +
int this,loop,port = 32459;
 +
char *peer = "default";
  
// Output a log entry with timestamp
+
#include <unistd.h>
// - Message can contain format like printf but only %s and %d
+
#include <stdlib.h>
// - todo: Should use [[[[io.c]]]] to send multiplexly
+
#include <stdio.h>
char *laMsg = NULL;
+
#include <string.h>
void logAdd(char *format, ... ) {
+
#include <errno.h>
if (laMsg == NULL) laMsg = malloc(1000);
+
#include <math.h>
char *msg = laMsg;
+
#include <stdarg.h>
va_list args;
+
#include <time.h>
va_start(args,format);
+
#include <regex.h>
time_t now = time(NULL);
+
#include "util.c"          // [[[[util.c]]]]: General utils, file,log,string etc
char *nowtext = ctime(&now);
 
if (nowtext) while (*msg++ = *nowtext++);
 
*(msg-2) = ':';
 
*(msg-1) = ' ';
 
while (*format) {
 
if (strncmp(format,"%d",2)==0) {
 
msg += sprintf(msg,"%d",va_arg(args,int));
 
format += 2;
 
}
 
else if (strncmp(format,"%s",2)==0) {
 
char *arg = va_arg(args,char*);
 
while (*arg) *msg++ = *arg++;
 
format += 2;
 
}
 
else *msg++ = *format++;
 
}
 
va_end(args);
 
*msg++ = '\n';
 
*msg++ = '\0';
 
printf(laMsg);
 
}
 
  
 +
// List & Node Space
 +
#include "listSpace.c"      // [[[[listSpace.c]]]]: listSpace and some C-specific extras: hash, trie, linked-list
 +
#include "nodeSpace.c"      // [[[[nodeSpace.c]]]]: NodeSpace function declarations and initialisation
 +
#include "serialise.c"      // [[[[serialise.c]]]]: Nodal-to-text and visa-versa
  
// Return an array of strings resulting from splitting passed text at passed character
+
// Interface related
// - the array is terminated by a NULL pointer
+
#include "SDL.h"
char **split(char c,char *text) {
+
#include "SDL_image.h"
int len = strlen(text), items = 0, size = 10;
+
#include "SDL_opengl.h"    // OpenGL example [[[[http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_20Full_20Example|here]]]]
char **list = malloc(size);
+
#include "interface.c"      // [[[[interface.c]]]]: Layer model, video, audio, input, OpenGL
char *i = malloc(len+1), *j = i, *k = i, *item = i;
 
while(*j++ = *text++);
 
while(i <= k+len) {
 
if (*i == c) *i = '\0';
 
if ((*i++ == '\0')&&strlen(item)) {
 
if (items>size-2) realloc(list,size+=10);
 
list[items++] = item;
 
list[items] = NULL;
 
item = i;
 
}
 
}
 
return list;
 
}
 
  
// Replaces missing itoa() and is specialised for binary return a string of \1 and \2 characters
+
// Peer daemon setup
char *itob(int value, char *buf) {
+
#if __WIN32__
int i;
+
#include "servicate.c"      // [[[[servicate.c]]]]
char *j = buf;
+
#elif __APPLE__
for (i=1; i<=value>>1; i<<=1) *j++ = value&i ? '\1' : '\2';
+
#include "launchd.c"        // [[[[launchd.c]]]]
*j = '\0';
+
#elif __unix__
return buf;
+
#include "daemonise.c"      // [[[[daemonise.c]]]]
}
+
#endif
  
// ----------------------------------------------------------------------------------------- //
+
// Storage & distribution related
// Global Pointer List
+
#if __WIN32__
// - use insertPointer() and removePointer() to access the dynamically allocating pointer-array
+
#include <winsock.h>
// - internally uses freePush() and freePop() to maintain a linked-list of free array slots
+
#else
int psize = 100, pitem = 0;
+
#include <sys/socket.h>
void **plist = NULL;
+
#include <sys/select.h>
typedef struct fi {
+
#include <netinet/in.h>
int index;
+
#include <sys/time.h>      // for select()
struct fi *next;
+
#include <fcntl.h>          // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
} fitem;
+
#endif
fitem *flist = NULL;
+
#include "io.c"            // [[[[io.c]]]]: Main server and stream setup and functions
  
// add a new pointer to the list, try using slots from the free-list before extending array
+
int main(int argc, char **argv) {
int insertPointer(void *ptr) {
 
int index;
 
if (plist == NULL) plist = malloc(psize*sizeof(void*));
 
if (flist) {
 
fitem *kill = flist;
 
flist = (fitem*)kill->next;
 
index = kill->index;
 
free(kill);
 
}
 
else if ((index = ++pitem) > psize) realloc(plist,psize += 100);
 
plist[index] = ptr;
 
return index;
 
}
 
  
// push the index onto the free-list, returning the pointer at that index
+
regex_t *preg = malloc(1000);
void *removePointer(int index) {
+
regcomp(preg, "[cmprs]at", 0);
fitem *newfree = (fitem*)malloc(sizeof(fitem));
 
newfree->next = flist ? flist : NULL;
 
flist = newfree;
 
return plist[newfree->index = index];
 
}
 
  
 +
logAdd("");            // Blank line in log to separate peerd sessions
 +
peerdInit();            // Set up as a daemon or service
 +
listInit();            // Initialise list-space and hash/trie functions
 +
nodeInit();            // Set up initial nodal structure for reduction loop
 +
args(argc,argv);        // Handle command-line args and globals like peer and port
 +
ifInit();              // Initialise interface aspects (video, audio, input, OpenGL)
 +
ioInit();              // Set up listening socket on specified port
  
// Store the command-line args in the listSpace hash, eg char *name = *hash("name")
+
// Main nodal reduction loop
void **hash(unsigned char*);
+
logAdd("Handing program execution over to nodal reduction...");
void args(int argc, char **argv) {
+
for (this=0;1;this=0) nodeReduce();
  
if (argc>1) {
 
int l = strlen(argv[1]);
 
peer = strncpy(malloc(l+1),argv[1],l);
 
while(argc-->2) {
 
char **arg = split('=',argv[argc]);
 
*hash(*arg) = arg[1]?strncpy(malloc(l=strlen(arg[1])+1),arg[1],l):"1";
 
free(arg);
 
}
 
}
 
 
// Make a global peer name and port number
 
char *p = *hash("port");
 
if (p) port = atoi(p);
 
 
#if __unix__
 
// Set name as seen in ps list if ux
 
char *tmp = *argv;
 
for (l=40;l--;) *tmp++ = '\0';
 
sprintf(*argv,"peerd: %s (http%d)",peer,port);
 
#endif
 
 
}
 
}

Revision as of 07:28, 21 August 2006

// [[[[1]]]] - nodal p2p wiki daemon // [[[language version]]] for *ix,OSX,NT platforms // This article and all its includes are licenced under [[[[2]]]] // SRC: [[[[3]]]] // NOTE: for automatic source syncronisation with peers in the field, // keep [[Bender/fileSync]] up to date! // Compiled in Win32 by [[peerd.c/win32-makefile]] // Compiled in Linux and OSX by [[rp]] // [[Current development]]

int this,loop,port = 32459; char *peer = "default";

  1. include <unistd.h>
  2. include <stdlib.h>
  3. include <stdio.h>
  4. include <string.h>
  5. include <errno.h>
  6. include <math.h>
  7. include <stdarg.h>
  8. include <time.h>
  9. include <regex.h>
  10. include "util.c" // [[util.c]]: General utils, file,log,string etc

// List & Node Space

  1. include "listSpace.c" // [[listSpace.c]]: listSpace and some C-specific extras: hash, trie, linked-list
  2. include "nodeSpace.c" // [[nodeSpace.c]]: NodeSpace function declarations and initialisation
  3. include "serialise.c" // [[serialise.c]]: Nodal-to-text and visa-versa

// Interface related

  1. include "SDL.h"
  2. include "SDL_image.h"
  3. include "SDL_opengl.h" // OpenGL example [[[[4]]]]
  4. include "interface.c" // [[interface.c]]: Layer model, video, audio, input, OpenGL

// Peer daemon setup

  1. if __WIN32__
  2. include "servicate.c" // [[servicate.c]]
  3. elif __APPLE__
  4. include "launchd.c" // [[launchd.c]]
  5. elif __unix__
  6. include "daemonise.c" // [[daemonise.c]]
  7. endif

// Storage & distribution related

  1. if __WIN32__
  2. include <winsock.h>
  3. else
  4. include <sys/socket.h>
  5. include <sys/select.h>
  6. include <netinet/in.h>
  7. include <sys/time.h> // for select()
  8. include <fcntl.h> // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
  9. endif
  10. include "io.c" // [[io.c]]: Main server and stream setup and functions

int main(int argc, char **argv) {

regex_t *preg = malloc(1000); regcomp(preg, "[cmprs]at", 0);

logAdd(""); // Blank line in log to separate peerd sessions peerdInit(); // Set up as a daemon or service listInit(); // Initialise list-space and hash/trie functions nodeInit(); // Set up initial nodal structure for reduction loop args(argc,argv); // Handle command-line args and globals like peer and port ifInit(); // Initialise interface aspects (video, audio, input, OpenGL) ioInit(); // Set up listening socket on specified port

// Main nodal reduction loop logAdd("Handing program execution over to nodal reduction..."); for (this=0;1;this=0) nodeReduce();

}