Browse files

awkbot now initializes with module system.

  * Created pre-load filter for modules which parses awk sources (by searching
    AWKPATH) for special #use comments similar to those supported by runawk.
  * Made awkbot support proper daemonization (bin/awkbot is no longer a
    blocking executable, but instead a controller).
  * Removed crude IKC system which depended on special kernel events.
    * IKC is being moved to a series of modules which support the functionality
      necessary for any two amsterdam powered applications to message
      one-another.
  * kernel now uses the awk command used to start itself as the "awk" command
    when creating new modules.
  • Loading branch information...
1 parent f70d96c commit d023167a314dbbc0e8b7177b240236e53554e649 @ssmccoy committed Aug 10, 2011
Showing with 152 additions and 70 deletions.
  1. +22 −0 .gitignore
  2. +84 −33 bin/awkbot
  3. +17 −6 etc/awkbot.conf
  4. +8 −1 src/awkbot.awk
  5. +17 −9 src/client.awk
  6. +1 −1 src/client_test.awk
  7. +0 −1 src/config.h
  8. +3 −19 src/kernel.awk
View
22 .gitignore
@@ -0,0 +1,22 @@
+*.log
+*.log.*
+*.ipr
+*.iws
+*.iml
+.project
+.classpath
+.settings
+.metadata
+junit*.properties
+projectFilesBackup
+target
+.externalToolBuilders
+maven-eclipse.xml
+bin-groovy
+# OS X metadata file
+.DS_Store
+# Intellij IDEA plugin for JIRA
+atlassian-ide-plugin*
+*.db4
+test-output
+*.sw[op]
View
117 bin/awkbot
@@ -1,42 +1,93 @@
#!/bin/sh
-# awkpp is becoming a maintenance nightmare, and I'm able to ditch it by using
-# cpp and it's highly deprecated "import" statement instead.
-
-while true
-do
- # Run the bootstrap, this doesn't do jack but allocate a file.
- bootstrap=`tempfile`
- cpp -I /usr/share/awk -I src src/awkbot-boot.awk 2> /dev/null > $bootstrap
- livedata=`awk -f $bootstrap /dev/null`
- status=$?
-
- if [ $status -ne 0 ]
- then echo "Bootstrap script ($bootstrap) exited prematurely, please inspect"
- exit 1
+
+BIN="`dirname $0`"
+
+# Instance Settings
+AWKPATH=""
+AWKBOT_HOME="`dirname $BIN`"
+LOGDIR="$AWKBOT_HOME/logs"
+
+# Bootstrap Settings
+command="awk -f $AWKBOT_HOME/src/kernel.awk awkbot.awk"
+name=`basename $0`
+
+if [ "" = "$AWKPATH" ]
+then
+ if [ -d "/usr/share/awk" ]
+ then AWKPATH="/usr/share/awk"
fi
+fi
- # Done with this one
- rm $bootstrap
+export AWKPATH="$AWKBOT_HOME/src:$AWKPATH"
- awkbot=`tempfile`
- cpp -I /usr/share/awk/ -I src src/awkbot.awk 2> /dev/null > $awkbot
+# Daemon Management
+pidfile="$AWKBOT_HOME/run/$name.pid"
- # Give our continuous input through a pipe to tail. tail -f never exits.
- # Note, that this means awkbot never exits unless killed from the outside.
- rm $livedata
- mkfifo $livedata
- chmod 666 $livedata
+if [ -e $pidfile ]
+then pid=`cat $pidfile`
+else pid=0
+fi
- awk -f $awkbot $livedata
+case $1 in
+ 'start')
+ if [ $pid -eq 0 ]
+ then $command &
+ echo $! > $pidfile
+ else echo "It appears $name is already running (pid $pid)"
+ echo "Please run \"$0 cleanup\" if this is not true"
+ fi
+ ;;
- status=$?
+ 'stop')
+ if [ $pid -eq 0 ]
+ then echo "It doesn't appear that $name is running"
+ else echo "$name is shutting down, this may take a while."
+ echo "if $name's shutdown process is too slow, run $0 $1 again"
+ echo "to stop the process forcefully, but without proper cleanup"
- if [ $status -ne 0 ]
- then echo "Generated script ($awkbot) exited prematurely, please inspect"
- exit 1
- fi
+ if kill -0 $pid 2> "$LOGDIR/stderr.log"
+ then kill -INT $pid
+
+ echo -n "Waiting for process to terminate..."
+
+ # Wait for the process to shut down by testing if it's
+ # reachable once a second. This is in lieu of using something
+ # like waitpid, since the daemon won't be one of our child
+ # processes.
+ while kill -0 $pid 2> "$LOGDIR/stderr.log"
+ do echo -n "."
+ sleep 1
+ done
+
+ rm $pidfile
+ echo
+ else echo "It appears $name has crashed, doing cleaup instead"
+ $0 cleanup
+ fi
+
+ fi
+ ;;
+
+ 'cleanup')
+ if [ -e $pidfile ]
+ then rm $pidfile
+ fi
+ ;;
+
+ 'reload')
+ echo "Attempting to reload $name"
+ if kill -0 $pid 2> "$LOGDIR/stderr.log"
+ then kill -HUP $pid
+ echo "Forcing reload of $name, this may take some time"
+ else echo "It appears that $name isn't running"
+ fi
+ ;;
-# Clean up our mess
- rm $awkbot
- rm $livedata
-done
+ *)
+ echo "Usage: $0 [ start | stop | cleanup | reload ]"
+ echo " start - Start the $name daemon"
+ echo " stop - Stop the $name daemon"
+ echo " cleanup - Cleanup a after a dead $name daemon"
+ echo " reload - Perform a full reload of $name"
+ ;;
+esac
View
23 etc/awkbot.conf
@@ -1,25 +1,36 @@
+logfile logs/awkbot.log
+loglevel DEBUG
+
+# Comment out to turn off listening.
+sockname /tmp/awkbot
+
# IRC Stuff
<irc>
- nickname awkbot
- altnick awkbot-
- username awkbot
- realname AWK IRC bot
+ nickname amstest
+ altnick amstest-
+ username amstest
+ realname AWK IRC bot: Build on the awk module system
# server irc.isprime.com:6667
- server irc.freenode.net:6667
+ server irc.freenode.net
+ port 6667
# port 6667
# channel awk perl pike
# channel c++
- channel awk
+ channel #awkbot-test
# channel blacksun
# debug 1
startup PRIVMSG NickServ :identify darwin
</irc>
+
# Mysql settings
# The awk preprocessor uses this
mysql on
+# The new awkbot uses this configuration to determine the database module.
+database awkbot_db_mysql.awk
+
<mysql>
username awkbot
password awkbot
View
9 src/awkbot.awk
@@ -22,7 +22,7 @@ BEGIN {
assert(config("irc.port"), "port not specified in config")
}
-function awkbot_init (stream, logfile,loglevel) {
+function awkbot_init (stream, logfile,loglevel,sockname) {
# Set up the logger first, since everything else will try and write to it.
kernel_load("logger.awk", "log")
@@ -47,6 +47,13 @@ function awkbot_init (stream, logfile,loglevel) {
kernel_send("database", "running", 1)
kernel_send("database", "livefeed", stream)
+ sockname = config("sockname")
+
+ if ("" != sockname) {
+ kernel_load("listener.awk", "listener")
+ kernel_send("listener", "listen", sockname)
+ }
+
awkbot_connect()
}
View
26 src/client.awk
@@ -6,6 +6,9 @@
# this stuff is worth it, you can buy me a beer in return. Scott S. McCoy
# -----------------------------------------------------------------------------
+#use mkfifo.awk
+#use tempfile
+
BEGIN {
connection = ""
# TODO We obviously need an mkfifo
@@ -19,18 +22,21 @@ function client_send (component, message, a1,a2,a3,a4,a5,a6,a7,a8) {
fflush(connection)
}
+function remove (filename) {
+ system("exec rm " filename)
+}
+
# Message the kernel that we need to send them a message
-function client_connect (stream) {
+function client_connect (listener) {
connection = stream
- tempfile_command | getline fifo
- close(tempfile_command)
+ fifo = tempfile("client")
- system("rm " fifo)
- system("mkfifo " fifo)
+ remove(fifo)
- # It's a pipe, so we need cat...hah!
- client_send("kernel", "attach", fifo, "exec cat > "fifo)
+ mkfifo(fifo)
+
+ print "connect", fifo >> listener
# Cause the selector to start reading from our special fifo the other
# kernel has been made aware of
@@ -45,8 +51,10 @@ function client_listen (component, event, handler) {
}
## Read a message our component recieved, and publish it as an event.
-function client_read (message, a1,a2,a3,a4,a5,a6,a7,a8,a9) {
- kernel_publish(a1,a2,a3,a4,a5,a6,a7,a8,a9)
+function client_read (file, message ,a) {
+ split(message, a)
+
+ kernel_publish(a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9])
}
function client_disconnect (source) {
View
2 src/client_test.awk
@@ -1,7 +1,7 @@
BEGIN {
- kernel = "/tmp/tmp.iWNsajpzlD"
+ kernel = "/tmp/awkbot"
}
"init" == $1 {
View
1 src/config.h
@@ -1 +0,0 @@
-#define GAWK 1
View
22 src/kernel.awk
@@ -90,21 +90,6 @@ function kernel_message (module, message, a1,a2,a3,a4,a5,a6,a7,a8,a9) {
fflush(kernel["process", module])
}
-# Attach an aribtrary pipe as module name.
-function kernel_attach (name, pipe) {
- printf "kernel->attach(\"%s\",\"%s\")\n", name, pipe >> "/dev/stderr"
- if (!kernel["process", name]) {
- kernel["process", name] = pipe
-
- kernel_message(name, "init", FILENAME)
- }
- else {
- print "error", "A module of that name already exists" | pipe
- print "fini" | pipe
- close(pipe)
- }
-}
-
## Load a module.
function kernel_load (source, name ,depends,input,words,filename,loaded) {
filename = find(source)
@@ -299,9 +284,6 @@ function kernel_init ( i,m,module) {
else if ("init" == $2) {
kernel_init()
}
- else if ("attach" == $2) {
- kernel_attach($3,$4)
- }
}
"kernel" != $1 {
@@ -329,7 +311,9 @@ function kernel_parse_args ( i,modname,m) {
BEGIN {
FS = OFS = SUBSEP
- awk = "awk"
+ # Use the awk that was used to start the kernel.
+ awk = ARGV[0]
+
kernel_parse_args()
kernel_start()
}

0 comments on commit d023167

Please sign in to comment.