source: trunk/p2p/authority.tcl @ 4503

Last change on this file since 4503 was 3177, checked in by mmc, 8 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

File size: 5.4 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) 2004-2012  HUBzero Foundation, LLC
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15
16# recognize other library files in this same directory
17set dir [file dirname [info script]]
18lappend auto_path $dir
19
20# handle log file for this authority
21log channel debug on
22log channel system on
23
24# ======================================================================
25#  WORKER OPTIONS
26# ======================================================================
27
28# set of connections for authority servers
29set worker_options(authority_hosts) \
30    {127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003}
31
32# register with the central authority at this frequency
33set worker_options(time_between_authority_checks) 60000
34
35# workers should try to connect with this many other peers
36set worker_options(max_peer_connections) 4
37
38# ======================================================================
39#  PROTOCOL: hubzero:authority<-worker/1
40#
41#  The worker initiates communication with the authority by sending
42#  these messages to the authority to request information about
43#  other peers.
44# ======================================================================
45p2p::protocol::register hubzero:authority<-worker/1 {
46
47    # ------------------------------------------------------------------
48    #  INCOMING: listening <port>
49    #  Workers use this to tell the authority what port they're
50    #  listening on.  Other workers should connect to this port.
51    # ------------------------------------------------------------------
52    define listening {port} {
53        global client2address workers
54        variable handler
55        variable cid
56
57        # register this address on the list of workers
58        set addr "$client2address($cid):$port"
59        set workers($addr) "--"
60
61        # name this connection for easier debugging
62        $handler connectionName $cid $addr
63
64        return ""
65    }
66
67    # ------------------------------------------------------------------
68    #  INCOMING: peers
69    # ------------------------------------------------------------------
70    define peers {} {
71        global workers
72        return [list peers [array names workers]]
73    }
74}
75
76# ======================================================================
77#  PROTOCOL: hubzero:authority<-foreman/1
78#
79#  The foreman initiates communication with the authority by sending
80#  these messages to request information about workers, to estimate
81#  requirements for a job, to put funds in escrow, and to finalize
82#  the transaction.
83# ======================================================================
84p2p::protocol::register hubzero:authority<-foreman/1 {
85    # ------------------------------------------------------------------
86    #  INCOMING: workers
87    #  Foremen use this to request the list of known workers, and then
88    #  pick one worker to act as their connection to the P2P network.
89    # ------------------------------------------------------------------
90    define workers {} {
91        global workers
92        return [list workers [array names workers]]
93    }
94}
95
96# ======================================================================
97# set up a server at the first open port above 9001
98set server [p2p::server \
99    -port 9001? \
100    -protocols {hubzero:authority<-worker hubzero:authority<-foreman} \
101    -servername authority \
102    -onconnect authority_client_address \
103    -onprotocol authority_client_protocol \
104]
105
106# ----------------------------------------------------------------------
107#  COMMAND: authority_client_address <cid> <addr> <port>
108#
109#  Invoked automatically whenever a client connects to the server.
110#  Adds the client address/port to a list of all known clients.
111#  Other clients searching for peers can ask for a list of all known
112#  clients.
113# ----------------------------------------------------------------------
114proc authority_client_address {cid addr port} {
115    global client2address
116    set client2address($cid) $addr
117}
118
119# ----------------------------------------------------------------------
120#  COMMAND: authority_client_protocol <cid> <protocol>
121#
122#  Invoked automatically whenever a client declares its protocol
123#  to the server.  Clients speaking the "worker" protocol are
124#  kept on a special list.
125# ----------------------------------------------------------------------
126proc authority_client_protocol {cid protocol} {
127    global worker_options client2address
128    switch -glob -- $protocol {
129        *<-worker* {
130            puts $cid [list protocol hubzero:worker<-authority/1]
131            puts $cid "options [array get worker_options] ip $client2address($cid)"
132        }
133        *<-foreman* {
134            puts $cid [list protocol hubzero:foreman<-authority/1]
135        }
136        DEF* {
137            # do nothing
138        }
139        default {
140            error "don't recognize protocol \"$protocol\""
141        }
142    }
143}
144
145vwait main-loop
Note: See TracBrowser for help on using the repository browser.