source: trunk/p2p/authority.tcl @ 1251

Last change on this file since 1251 was 1251, checked in by mmc, 15 years ago

Added first cut of P2P network for job management. See README in this
directory for details.

File size: 4.1 KB
Line 
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
14set dir [file dirname [info script]]
15lappend auto_path $dir
16
17# handle log file for this authority
18log channel debug on
19log channel system on
20
21# ======================================================================
22#  WORKER OPTIONS
23# ======================================================================
24
25# set of connections for authority servers
26set 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
30set worker_options(time_between_registers) 60000
31
32# workers should try to connect with this many other peers
33set worker_options(max_peer_connections) 4
34
35# ======================================================================
36# set up a server at the first open port above 9001
37set 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# ----------------------------------------------------------------------
51proc 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# ----------------------------------------------------------------------
63proc 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
111vwait main-loop
Note: See TracBrowser for help on using the repository browser.