📄 publish.xpl
字号:
<?xml version="1.0"?><!DOCTYPE methoddef SYSTEM "rpc-method.dtd"><methoddef><name>Publish</name><version>1.1</version><signature>string struct</signature><help>Description: Accepts authenticated registrations from content Origin serverArguments: a structure containing KEY VALUE Address the origin IP address Port its TCP port Description about the content provider Programs An hash of hashes with content details Digest Fingerprint of the Origin AuthToken token to authenticate de-registration requestsReturns: a struct containing KEY VALUE ret_code int (numeric return code) ret_val string (message string) name the CDN centered on this RRDM delay delay in secs between re-publications RTSPpassword password to authenticate pull requests between streaming servers AuthToken authentication token to use in all following RPC-XML method calls</help><code language="perl"><![CDATA[#!/usr/bin/perluse CommLib qw(node_log mk_digest);################################################################################ Sub Name: Publish## Description: Accepts authenticated registrations from content Origin # server. Checks if an Origin tag is already registered# for another program, and rejects the whole request. If the# program is the same, it is treated as a re-publication# Updates timestamps about last and next publication## Arguments: a structure containing# KEY VALUE# Address the origin IP address# Port its TCP port# Description about the content provider# Programs An hash of hashes with content details# Digest Fingerprint of the Origin# AuthToken token to authenticate de-registration requests## Globals affected:# %origins hash containing origin publication values# %nonce_by_ID hash with nonce for given entity (Node/Origin) ID## Returns: a struct containing# KEY VALUE# ret_code int (numeric return code)# ret_val string (message string)# name the CDN centered on this RRDM# delay delay in secs between re-publications# RTSPpassword password to authenticate pull requests# between streaming servers# AuthToken authentication token to use in all following # RPC-XML method calls###############################################################################sub Publish { require RRDMlib; my $parm_ref = pop @_; # gets hash reference for input parameters my $prog, $prog1, $tag; my ($numnod, $delay); # number of origins and re-publication delay my ( $origin_ID, $myDigest); # gets input parameters my $origin_addr = $$parm_ref{Address}; my $origin_port = $$parm_ref{Port}; my $orig_desc = $$parm_ref{Description}; my $prog_ref = $$parm_ref{Programs}; my $hisDigest = $$parm_ref{Digest}; my $hisToken = $$parm_ref{AuthToken}; my ($retcode, $retval) = ( '200', '' ); node_log ( "*** Publish invoked by $origin_addr:$origin_port, origin = $orig_desc", 3, 'r'); # Beginning authentication Origin # Builds Origin identification $origin_ID = $origin_addr.':'.$origin_port; if ( $orig_desc ) { # It's a registration (or re-reg) request # Calculates my digest $myDigest = mk_digest ($RRDMlib::nonce_by_ID{$origin_ID}); # Checks method caller's identity if ( $hisDigest ne $myDigest ) { # identity unknown! if ( $RRDMlib::nonce_by_ID{$origin_ID} ) { # is not his first 'Publish' call node_log (" (Publish) authentication of the Origin $origin_ID failed", 1, 'r'); # Deletes respective nonce delete $RRDMlib::nonce_by_ID{$origin_ID}; } $retcode = '401'; $retval = 'Unauthorized'; # Registration ends here return { ret_code => $retcode, ret_val => $retval, name => 'noname', delay => '600', RTSPpassword => '', AuthToken => '' }; } # Request authentication succeeded: go on with registration! node_log (" (Publish) authentication of the Origin $origin_ID succeeded", 3, 'r'); } else { # Ir's a de-registration request if ( $hisToken ne $RRDMlib::auth_token ) { # dereg unauthorized node_log (" (Publish) de-Registration for Origin $origin_addr unauthorized", 1, 'r'); $retcode = '401'; $retval = 'Unauthorized'; # De-Registration ends here return { ret_code => $retcode, ret_val => $retval, name => 'noname', delay => '600', RTSPpassword => '', AuthToken => '' }; } # Request authentication succeeded # Deletes respective nonce delete $RRDMlib::nonce_by_ID{$origin_ID}; # Goes on with de-registration! node_log (" (Publish) de-Registration for Origin $origin_addr authorized", 3, 'r'); } # End authentication Origin # Checks for duplicate tags foreach $prog (keys %$prog_ref) { # all the programs to be published $tag = $$prog_ref{$prog}{'tag'}; if (defined $RRDMlib::prog_by_tag{$tag}) { # tag exists $prog1 = $RRDMlib::prog_by_tag{$tag}; if ($prog1 ne $prog) { # for a different program $retval = "Tag $tag is already published for program $prog1"; node_log ("Error from $origin_addr:$origin_port: $retval", 1, 'r'); $retcode = '425'; $retval .= ". Please choose another one."; last; } } } unless ($retcode eq '425') { # Determines maximum per-Origin delay for re-publications $numnod = (keys %RRDMlib::origins) + (keys %$prog_ref);# number of programs $delay = &RRDMlib::rereg_delay ($numnod, $RRDMconfig::rreg_pace); foreach $prog (keys %$prog_ref) { # all the programs to be published if (defined $RRDMlib::origins{$prog}) { # Updates timestamps $RRDMlib::origins{$prog}{'rtime'} = time; $RRDMlib::origins{$prog}{'utime'} = time + $delay; $retcode = '210'; # Already seen: re-Pub o de-Pub $retval = "Re-Publication of $prog"; if ( !$orig_desc ) { $retcode = '220'; # empty origin desc: is a de-Pub $retval = "De-Publication of $prog"; delete $RRDMlib::origins{$prog}; # cleans all the program data $tag = $$prog_ref{$prog}{'tag'}; # and refs delete $RRDMlib::prog_by_tag{$tag}; } } else { # new program if (!$origin_port || !$prog) { # empty port or program $retcode = '220'; $retval = "Bad parms: prog $prog, origin_port $origin_port"; } else { $retcode = '200'; $retval = "Publication of $prog "; $tag = $$prog_ref{$prog}{'tag'}; $RRDMlib::origins{$prog} = { 'ip' => $origin_addr, 'port' => $origin_port, 'tag' => $tag, 'odesc' => $orig_desc, 'transport' => $$prog_ref{$prog}{'transport'}, 'desc' => $$prog_ref{$prog}{'desc'}, 'duration' => $$prog_ref{$prog}{'duration'}, 'language' => $$prog_ref{$prog}{'language'}, 'prereq' => $$prog_ref{$prog}{'prereq'}, 'postreq' => $$prog_ref{$prog}{'postreq'}, 'audio' => $$prog_ref{$prog}{'audio'}, 'video' => $$prog_ref{$prog}{'video'}, 'rtime' => time, 'utime' => time + $delay }; $RRDMlib::prog_by_tag{$tag} = $prog;# foreach $a (sort keys %{$RRDMlib::origins{$prog}}) {# node_log ("======= $a => $RRDMlib::origins{$prog}{$a}", 1, 'r');# } } } my $logtype = 1; $logtype = 3 if ($retcode eq '210'); # only if re-reg node_log (" Publish called by $origin_addr:$origin_port: $retval", $logtype, 'r'); } # ends (de)Publications # Debug controls #&RRDMlib::show_table('origins'); if ($retcode eq '200') { # builds a rensponse $retval = "Publication successful"; # for the Origin node } elsif ($retcode eq '210') { $retval = "Re-Publication successful"; } else { $retval = "De-Publication successful"; } } # Re-evaluates per-Origin delay; may be shorter than utime timestamps $numnod = (keys %RRDMlib::origins); # number of programs $delay = &RRDMlib::rereg_delay ($numnod, $RRDMconfig::rreg_pace); return { ret_code => $retcode, ret_val => $retval, name => $RRDMconfig::oCDN_name, delay => $delay, RTSPpassword => $RRDMconfig::RTSPpassword, AuthToken => $RRDMlib::auth_token };}__END__]]></code></methoddef>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -