3 # process-consensus - read a current consensus document, inserting the
4 # information into a database then calling
5 # update-named-status.rb to update the name-binding
8 # Copyright (c) 2007 Peter Palfrader
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 require 'update-named-status'
34 $db = Db
.new($CONFIG['database']['dbhost'], $CONFIG['database']['dbname'], $CONFIG['database']['user'], $CONFIG['database']['password'])
39 def parse_consensus consensus
42 consensus
.each
do |line
|
43 (key
, value
) = line
.split(' ',2)
45 when "valid-after", "published": ts
= DateTime
.parse(value
)
47 (nick
, fpr
, _
) = value
.split(' ', 3)
49 next if nick
== 'unnamed'
52 'fingerprint' => (fpr
+'=').unpack('m').first
.unpack('H*').first
56 throw "Did not find a timestamp" unless ts
57 throw "Did not find any routers" unless routers
.size
> 0
61 def insert_routers_into_db(router
, table
, field
, value
)
63 row
= $db.query_row("SELECT #{pk} FROM #{table} WHERE #{field}=?", value
)
67 r
= { field
=> value
}
68 $db.insert_row( table
, r
)
73 def handle_one_consensus(c
)
74 puts
"parsing..." if $verbose
75 timestamp
, routers
= parse_consensus c
76 puts
"storing..." if $verbose
78 routers
.each
do |router
|
79 fpr
= router
['fingerprint']
81 $router_cache[fpr
] = router_id
= ($router_cache[fpr
] or insert_routers_into_db(router
, 'router', 'fingerprint', router
['fingerprint']))
82 $nickname_cache[nick
] = nickname_id
= ($nickname_cache[nick
] or insert_routers_into_db(router
, 'nickname', 'nick', router
['nick']))
85 'router_claims_nickname',
86 { 'last_seen' => timestamp
.to_s
},
87 { 'router_id' => router_id
, 'nickname_id' => nickname_id
} )
90 $db.insert('router_claims_nickname',
92 'first_seen' => timestamp
.to_s
,
93 'last_seen' => timestamp
.to_s
,
94 'router_id' => router_id
, 'nickname_id' => nickname_id
} )
97 throw "Update of router_claims_nickname returned unexpected number of affected rows(#{row})"
102 $db.transaction_begin
103 if ARGV.first
== '-v'
109 handle_one_consensus
STDIN.readlines
112 ARGV.each
do |filename
|
113 puts filename
if $verbose
114 handle_one_consensus File
.new(filename
).readlines
115 puts
"updating..." if $verbose
119 $db.transaction_commit