Difference between revisions of "Peerd.c"

From Organic Design wiki
(oops, crashes if no args supplied)
m
 
(49 intermediate revisions by 2 users not shown)
Line 1: Line 1:
// [[[[http://www.organicdesign.co.nz/peerd|peerd]]]] - nodal p2p wiki daemon
+
{{legacy}}
// This article and all its includes are licenced under LGPL
+
<source lang="c">
// GPL: [[[[http://www.gnu.org/copyleft/lesser.html]]]]
+
// http://www.organicdesign.co.nz/peerd - nodal p2p wiki daemon
// SRC: [[[[http://www.organicdesign.co.nz/util.c]]]]
+
// Source: http://www.organicdesign.co.nz/peerd/files/C - the C language version for *ix,OSX,NT platforms
// included in [[[[http://www.organicdesign.co.nz/category:peerd/files/C|peerd]]]][[[[http://www.organicdesign.co.nz/peerd.c|/peerd.c]]]]
+
// This article and all its includes are licenced under http://www.gnu.org/copyleft/lesser.html
 +
// Compiled in Win32 by peerd.c/win32-makefile to http://www.organicdesign.co.nz/wiki/images/a/a2/Peerd.msi
 +
// Compiled in Linux and OSX by rp to peerd.deb and peerd.dmg
  
 +
// Globals
 +
int this = 0,parent,grandpa,port = 80;
 +
char *peer = "default";
 +
char *file = (char*)0;
  
// Output a log entry with timestamp
+
// Reserved nodes used by nodal reduction process (tmp)
// - Message can contain format like printf but only %s and %d
+
#define nodeTRUE  1  // [[[[nodeTRUE]]]]
// - todo: Should use [[[[io.c]]]] to send multiplexly
+
#define nodeNEXT  1  // [[[[nodeNEXT]]]]
char *laMsg = NULL;
+
#define nodePREV  2  // [[[[nodePREV]]]]
void logAdd(char *format, ... ) {
+
#define nodeCODE  3  // [[[[nodeCODE]]]] is a [[[[association|pseudo node-value]]]] to determine is the nodes non-nodal-data is a function-ref
if (laMsg == NULL) laMsg = malloc(1000);
+
#define nodePARENT 4  // [[[[nodePARENT]]]] updated by [[[[nodal reduction]]]], the node for which thsi is the [[[[focus]]]]
char *msg = laMsg;
+
#define nodeTRIE  5  // [[[[nodeTRIE]]]] use a different root for trie/hash so it can't interfere with serialisation of nodal-root
va_list args;
+
#define nodeFIRST  6  // [[[[nodeFIRST]]]]
va_start(args,format);
+
#define nodeLAST  7  // [[[[nodeLAST]]]]
time_t now = time(NULL);
+
#define nodeMAKE  8  // [[[[nodeMAKE]]]]
char *nowtext = ctime(&now);
+
#define nodeINIT  9  // [[[[nodeINIT]]]]
if (nowtext) while (*msg++ = *nowtext++);
+
#define nodeMAIN  10 // [[[[nodeMAIN]]]]
*(msg-2) = ':';
+
#define nodeEXIT  11 // [[[[nodeEXIT]]]]
*(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);
 
}
 
  
  
// Return an array of strings resulting from splitting passed text at passed character
+
#include <unistd.h>
// - the array is terminated by a NULL pointer
+
#include <stdlib.h>
char **split(char c,char *text) {
+
#include <stdio.h>
int len = strlen(text), items = 0, size = 10;
+
#include <string.h>
char **list = malloc(size);
+
#include <errno.h>
char *i = malloc(len+1), *j = i, *k = i, *item = i;
+
#include <math.h>
while(*j++ = *text++);
+
#include <stdarg.h>
while(i <= k+len) {
+
#include <time.h>
if (*i == c) *i = '\0';
+
#include <regex.h>
if ((*i++ == '\0')&&strlen(item)) {
+
#include "util.h"          // [[[[util.c]]]]: General utils, file,log,string etc
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
 
char *itob(int value, char *buf) {
 
int i;
 
char *j = buf;
 
for (i=1; i<=value>>1; i<<=1) *j++ = value&i ? '\1' : '\2';
 
*j = '\0';
 
return buf;
 
}
 
  
// ----------------------------------------------------------------------------------------- //
+
// List & Node Space
// Global Pointer List
+
#include "listSpace.h"      // [[[[listSpace.c]]]]: listSpace and some C-specific extras: hash, trie, linked-list
// - use insertPointer() and removePointer() to access the dynamically allocating pointer-array
+
#include "nodeSpace.h"      // [[[[nodeSpace.c]]]]: NodeSpace function declarations and initialisation
// - internally uses freePush() and freePop() to maintain a linked-list of free array slots
+
#include "serialise.h"      // [[[[serialise.c]]]]: Nodal-to-text and visa-versa
int psize = 100, pitem = 0;
 
void **plist = NULL;
 
typedef struct fi {
 
int index;
 
struct fi *next;
 
} fitem;
 
fitem *flist = NULL;
 
  
// add a new pointer to the list, try using slots from the free-list before extending array
+
// Interface related
int insertPointer(void *ptr) {
+
#include "SDL.h"
int index;
+
#include "SDL_image.h"
if (plist == NULL) plist = malloc(psize*sizeof(void*));
+
#include "SDL_ttf.h"
if (flist) {
+
//#include "SDL_opengl.h"    // OpenGL example [[[[http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_20Full_20Example|here]]]]
fitem *kill = flist;
+
#include "interface.h"      // [[[[interface.c]]]]: Layer model, video, audio, input, OpenGL
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
+
// Peer daemon setup
void *removePointer(int index) {
+
#if __WIN32__
fitem *newfree = (fitem*)malloc(sizeof(fitem));
+
#include "servicate.h"      // [[[[servicate.c]]]]
newfree->next = flist ? flist : NULL;
+
#elif __APPLE__
flist = newfree;
+
#include "launchd.h"        // [[[[launchd.c]]]]
return plist[newfree->index = index];
+
#elif __unix__
}
+
#include "daemonise.h"      // [[[[daemonise.c]]]]
 +
#endif
  
 +
// [[[[Communications]]]] related
 +
#if __WIN32__
 +
#include <winsock.h>
 +
#else
 +
#include <sys/socket.h>    // see [[[[http://www.opengroup.org/onlinepubs/009695399/functions/select.html|select()]]]]
 +
#include <sys/select.h>
 +
#include <netinet/in.h>
 +
#include <sys/time.h>      // for select() related stuff
 +
#include <fcntl.h>          // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
 +
#include <netdb.h>          // for [[[[http://www.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html|hostent struct]]]]
 +
#endif
 +
#include "io.h"            // [[[[io.c]]]]: Main server and stream setup and functions
  
// Store the command-line args in the listSpace hash, eg char *name = *hash("name")
+
int main(int argc, char **argv) {
void **hash(unsigned char*);
 
void args(int argc, char **argv) {
 
  
if (argc>1) {
+
logAdd("");            // Blank line in log to separate peerd sessions
int l = strlen(argv[1]);
+
peerdInit();           // Set up as a daemon or service
peer = strncpy(malloc(l+1),argv[1],l);
+
listInit();             // Initialise list-space and hash/trie functions
while(argc-->2) {
+
nodeInit();            // Set up initial nodal structure for reduction loop
char **arg = split('=',argv[argc]);
+
args(argc,argv);       // Handle command-line args and globals like peer and port
*hash(*arg) = arg[1]?strncpy(malloc(l=strlen(arg[1])+1),arg[1],l):"1";
+
ifInit();              // Initialise interface aspects (video, audio, input, OpenGL)
free(arg);
+
ioInit();               // Set up listening socket on specified port
}
 
}
 
  
// Make a global peer name and port number
+
// Main [[[[nodal reduction]]]] loop
char *p = *hash("port");
+
// - maintains [[[[this]]]], [[[[parent]]]] and [[[[grandpa]]]] globals
if (p) port = atoi(p);
+
logAdd("Handing program execution over to nodal reduction...");
 +
while(1) nodeReduce();
  
#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
 
 
}
 
}
 +
</source>
 +
[[Category:C]]

Latest revision as of 15:17, 6 July 2015

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
// http://www.organicdesign.co.nz/peerd - nodal p2p wiki daemon
// Source: http://www.organicdesign.co.nz/peerd/files/C - the C language version for *ix,OSX,NT platforms
// This article and all its includes are licenced under http://www.gnu.org/copyleft/lesser.html
// Compiled in Win32 by peerd.c/win32-makefile to http://www.organicdesign.co.nz/wiki/images/a/a2/Peerd.msi
// Compiled in Linux and OSX by rp to peerd.deb and peerd.dmg

// Globals
int this = 0,parent,grandpa,port = 80;
char *peer = "default";
char *file = (char*)0;

// Reserved nodes used by nodal reduction process (tmp)
#define nodeTRUE   1  // [[[[nodeTRUE]]]]
#define nodeNEXT   1  // [[[[nodeNEXT]]]]
#define nodePREV   2  // [[[[nodePREV]]]]
#define nodeCODE   3  // [[[[nodeCODE]]]] is a [[[[association|pseudo node-value]]]] to determine is the nodes non-nodal-data is a function-ref
#define nodePARENT 4  // [[[[nodePARENT]]]] updated by [[[[nodal reduction]]]], the node for which thsi is the [[[[focus]]]]
#define nodeTRIE   5  // [[[[nodeTRIE]]]] use a different root for trie/hash so it can't interfere with serialisation of nodal-root
#define nodeFIRST  6  // [[[[nodeFIRST]]]]
#define nodeLAST   7  // [[[[nodeLAST]]]]
#define nodeMAKE   8  // [[[[nodeMAKE]]]]
#define nodeINIT   9  // [[[[nodeINIT]]]]
#define nodeMAIN   10 // [[[[nodeMAIN]]]]
#define nodeEXIT   11 // [[[[nodeEXIT]]]]


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <time.h>
#include <regex.h>
#include "util.h"           // [[[[util.c]]]]: General utils, file,log,string etc

// List & Node Space
#include "listSpace.h"      // [[[[listSpace.c]]]]: listSpace and some C-specific extras: hash, trie, linked-list
#include "nodeSpace.h"      // [[[[nodeSpace.c]]]]: NodeSpace function declarations and initialisation
#include "serialise.h"      // [[[[serialise.c]]]]: Nodal-to-text and visa-versa

// Interface related
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
//#include "SDL_opengl.h"     // OpenGL example [[[[http://www.libsdl.org/cgi/docwiki.cgi/OpenGL_20Full_20Example|here]]]]
#include "interface.h"      // [[[[interface.c]]]]: Layer model, video, audio, input, OpenGL

// Peer daemon setup
#if __WIN32__
#include "servicate.h"      // [[[[servicate.c]]]]
#elif __APPLE__
#include "launchd.h"        // [[[[launchd.c]]]]
#elif __unix__
#include "daemonise.h"      // [[[[daemonise.c]]]]
#endif

// [[[[Communications]]]] related
#if __WIN32__
#include <winsock.h>
#else
#include <sys/socket.h>     // see [[[[http://www.opengroup.org/onlinepubs/009695399/functions/select.html|select()]]]]
#include <sys/select.h>
#include <netinet/in.h>
#include <sys/time.h>       // for select() related stuff
#include <fcntl.h>          // O_RDWR, O_NONBLOCK, F_GETFL, F_SETFL
#include <netdb.h>          // for [[[[http://www.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html|hostent struct]]]]
#endif
#include "io.h"             // [[[[io.c]]]]: Main server and stream setup and functions

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

	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
	// - maintains [[[[this]]]], [[[[parent]]]] and [[[[grandpa]]]] globals
	logAdd("Handing program execution over to nodal reduction...");
	while(1) nodeReduce();

	}