Debian Codegraph @

awklib/eg/network/mobag.awk

raw
1 BEGIN {
2 if (ARGC != 2) {
3 print "MOBAG - a simple mobile agent"
4 print "CALL:\n gawk -f mobag.awk mobag.awk"
5 print "IN:\n the name of this script as a command-line parameter"
6 print "PARAM:\n -v MyOrigin=myhost.com"
7 print "OUT:\n the result on stdout"
8 print "JK 29.03.1998 01.04.1998"
9 exit
10 }
11 if (MyOrigin == "") {
12 "uname -n" | getline MyOrigin
13 close("uname -n")
14 }
15 }
16 #ReadMySelf
17 /^function / { FUNC = $2 }
18 /^END/ || /^#ReadMySelf/ { FUNC = $1 }
19 FUNC != "" { MOBFUN[FUNC] = MOBFUN[FUNC] RS $0 }
20 (FUNC != "") && (/^}/ || /^#EndOfMySelf/) \
21 { FUNC = "" }
22 #EndOfMySelf
23 function migrate(Destination, MobCode, Label) {
24 MOBVAR["Label"] = Label
25 MOBVAR["Destination"] = Destination
26 RS = ORS = "\r\n"
27 HttpService = "/inet/tcp/0/" Destination
28 for (i in MOBFUN)
29 MobCode = (MobCode "\n" MOBFUN[i])
30 MobCode = MobCode "\n\nBEGIN {"
31 for (i in MOBVAR)
32 MobCode = (MobCode "\n MOBVAR[\"" i "\"] = \"" MOBVAR[i] "\"")
33 MobCode = MobCode "\n}\n"
34 print "POST /cgi-bin/PostAgent.sh HTTP/1.0" |& HttpService
35 print "Content-length:", length(MobCode) ORS |& HttpService
36 printf "%s", MobCode |& HttpService
37 while ((HttpService |& getline) > 0)
38 print $0
39 close(HttpService)
40 }
41 END {
42 if (ARGC != 2) exit # stop when called with wrong parameters
43 if (MyOrigin != "") # is this the originating host?
44 MyInit() # if so, initialize the application
45 else # we are on a host with migrated data
46 MyJob() # so we do our job
47 }
48 function MyInit() {
49 MOBVAR["MyOrigin"] = MyOrigin
50 MOBVAR["Machines"] = "localhost/80 max/80 moritz/80 castor/80"
51 split(MOBVAR["Machines"], Machines) # which host is the first?
52 migrate(Machines[1], "", "") # go to the first host
53 while (("/inet/tcp/8080/0/0" |& getline) > 0) # wait for result
54 print $0 # print result
55 close("/inet/tcp/8080/0/0")
56 }
57 function MyJob() {
58 # forget this host
59 sub(MOBVAR["Destination"], "", MOBVAR["Machines"])
60 MOBVAR["Result"]=MOBVAR["Result"] SUBSEP SUBSEP MOBVAR["Destination"] ":"
61 while (("who" | getline) > 0) # who is logged in?
62 MOBVAR["Result"] = MOBVAR["Result"] SUBSEP $0
63 close("who")
64 if (index(MOBVAR["Machines"], "/") > 0) { # any more machines to visit?
65 split(MOBVAR["Machines"], Machines) # which host is next?
66 migrate(Machines[1], "", "") # go there
67 } else { # no more machines
68 gsub(SUBSEP, "\n", MOBVAR["Result"]) # send result to origin
69 print MOBVAR["Result"] |& "/inet/tcp/0/" MOBVAR["MyOrigin"] "/8080"
70 close("/inet/tcp/0/" MOBVAR["MyOrigin"] "/8080")
71 }
72 }