1 | # ---------------------------------------------------------------------- |
---|
2 | # P2P: central authority for all P2P activities |
---|
3 | # |
---|
4 | # This server is the "superpeer" that all workers contact first |
---|
5 | # to join the network. Clients and workers also contact the authority |
---|
6 | # to handle payments for services. |
---|
7 | # ---------------------------------------------------------------------- |
---|
8 | # Michael McLennan (mmclennan@purdue.edu) |
---|
9 | # ====================================================================== |
---|
10 | # Copyright (c) 2008 Purdue Research Foundation |
---|
11 | # ====================================================================== |
---|
12 | |
---|
13 | # recognize other library files in this same directory |
---|
14 | set dir [file dirname [info script]] |
---|
15 | lappend auto_path $dir |
---|
16 | |
---|
17 | # handle log file for this authority |
---|
18 | log channel debug on |
---|
19 | log channel system on |
---|
20 | |
---|
21 | # ====================================================================== |
---|
22 | # WORKER OPTIONS |
---|
23 | # ====================================================================== |
---|
24 | |
---|
25 | # set of connections for authority servers |
---|
26 | set worker_options(authority_hosts) \ |
---|
27 | {127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003} |
---|
28 | |
---|
29 | # register with the central authority at this frequency |
---|
30 | set worker_options(time_between_registers) 60000 |
---|
31 | |
---|
32 | # workers should try to connect with this many other peers |
---|
33 | set worker_options(max_peer_connections) 4 |
---|
34 | |
---|
35 | # ====================================================================== |
---|
36 | # set up a server at the first open port above 9001 |
---|
37 | set server [Server ::#auto 9001? \ |
---|
38 | -servername authority \ |
---|
39 | -onconnect authority_client_address \ |
---|
40 | -onprotocol authority_client_protocol \ |
---|
41 | ] |
---|
42 | |
---|
43 | # ---------------------------------------------------------------------- |
---|
44 | # COMMAND: authority_client_address <cid> <addr> <port> |
---|
45 | # |
---|
46 | # Invoked automatically whenever a client connects to the server. |
---|
47 | # Adds the client address/port to a list of all known clients. |
---|
48 | # Other clients searching for peers can ask for a list of all known |
---|
49 | # clients. |
---|
50 | # ---------------------------------------------------------------------- |
---|
51 | proc authority_client_address {cid addr port} { |
---|
52 | global client2address |
---|
53 | set client2address($cid) $addr |
---|
54 | } |
---|
55 | |
---|
56 | # ---------------------------------------------------------------------- |
---|
57 | # COMMAND: authority_client_protocol <cid> <protocol> |
---|
58 | # |
---|
59 | # Invoked automatically whenever a client declares its protocol |
---|
60 | # to the server. Clients speaking the "worker" protocol are |
---|
61 | # kept on a special list. |
---|
62 | # ---------------------------------------------------------------------- |
---|
63 | proc authority_client_protocol {cid protocol} { |
---|
64 | global worker_options client2address |
---|
65 | if {"DEFAULT" != $protocol} { |
---|
66 | puts $cid [list protocol $protocol] |
---|
67 | puts $cid "options [array get worker_options] ip $client2address($cid)" |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | # ====================================================================== |
---|
72 | # PROTOCOL: hub:worker/1 |
---|
73 | # ====================================================================== |
---|
74 | $server protocol hubzero:worker/1 |
---|
75 | |
---|
76 | # ---------------------------------------------------------------------- |
---|
77 | # DIRECTIVE: exception <message> |
---|
78 | # ---------------------------------------------------------------------- |
---|
79 | $server define hubzero:worker/1 exception {args} { |
---|
80 | log system "error from worker: $args" |
---|
81 | } |
---|
82 | |
---|
83 | # ---------------------------------------------------------------------- |
---|
84 | # DIRECTIVE: listening <port> |
---|
85 | # Workers use this to tell the authority what port they're listening |
---|
86 | # on. Other workers should connect to this port. |
---|
87 | # ---------------------------------------------------------------------- |
---|
88 | $server define hubzero:worker/1 listening {port} { |
---|
89 | global client2address workers |
---|
90 | variable handler |
---|
91 | variable cid |
---|
92 | |
---|
93 | # register this address on the list of workers |
---|
94 | set addr "$client2address($cid):$port" |
---|
95 | set workers($addr) "--" |
---|
96 | |
---|
97 | # name this connection for easier debugging |
---|
98 | $handler connectionName $cid $addr |
---|
99 | |
---|
100 | return "" |
---|
101 | } |
---|
102 | |
---|
103 | # ---------------------------------------------------------------------- |
---|
104 | # DIRECTIVE: peers |
---|
105 | # ---------------------------------------------------------------------- |
---|
106 | $server define hubzero:worker/1 peers {} { |
---|
107 | global workers |
---|
108 | return [list peers [array names workers]] |
---|
109 | } |
---|
110 | |
---|
111 | vwait main-loop |
---|