所谓服务器,大致也就是这个样子了。接下来还需要实现一个协议,待续
写这个东西的初衷,是为了解决任务的调度问题。
任务依赖资源,任务也会产生资源。client在server上注册“interesting some resources”;client产生资源后,给server “resource ready”通知,server转发 notify给所有的关注者
直接可运行的示例脚本见附件,我的运行环境centOS5.5
#!/bin/sh#这个是服务器和客户端都需要的,绑定通信通道
Q="/tmp/Q$$"
if [ -p $Q ]
thenecho "fifo [$Q] already exists"exit -1
fi#只能由服务器端来创建(mkfifo)
mkfifo $Q#服务器的部分,listen负责接收请求,并作简单的处理(如协议解析之类的工作)
listen(){
while :
doread msgif [ "$msg" = "exit" ]thenexit 0elif [ "$msg" = "" ]thencontinueelseaccept "$msg"fi
done <$Q
}#覆盖accept来实现实际的功能
accept(){
echo "recv [$1]"
}#启动服务
trap "" HUP INT QUIT TSTP{echo "s: $$.$!"listen
}&wait
rm -rf $Qecho 'done'exit 0
#客户端的部分,发送请求
send(){
msg=$*
if [ -p $Q ]
thenecho $msg >$Q &
elseecho "fifo [$Q] no exists"return -1
fi
}{echo "c: $$.$!"for i in `seq 0 5`dosend "hello ${i}"sleep 3sdone# sleep 15sexit 0
}&#echo 'exit' >$Q &
工具utils.sh:
left(){ str=$1len=$2pad=$3len=$(($len - `expr length $str`))while [ $len -gt 0 ]dostr="${pad}${str}"len=$(($len - 1))done#log "[$1] [$2] [$3] > [$str]"echo $str
}if [ "$LOG_FILE" == "" ]
thenecho "LOG_FILE is required"exit -1
fi
touch $LOG_FILE
log(){msg=$1echo "`date '+%Y-%m-%d %H:%M:%S'` - $$ $msg" >> $LOG_FILEreturn 0
}