DialogSlave.pm
上传用户:market2
上传日期:2018-11-18
资源大小:18786k
文件大小:2k
源码类别:

外挂编程

开发平台:

Windows_Unix

  1. package Bus::DialogSlave;
  2. use strict;
  3. use Scalar::Util qw(refaddr);
  4. use Exception::Class ('Bus::DialogSave::AlreadyAccepted');
  5. sub new {
  6. my ($class, $args) = @_;
  7. my $self = bless $args, $class;
  8. return $self;
  9. }
  10. sub DESTROY {
  11. my ($self) = @_;
  12. if (!$self->{accepted}) {
  13. $self->{bus}->send("REFUSED", {
  14. TO  => $self->{peerID},
  15. SEQ => $self->{seq},
  16. IRY => 1
  17. });
  18. } else {
  19. $self->{bus}->onMessageReceived->remove($self->{receivedEvent});
  20. }
  21. }
  22. sub accept {
  23. my ($self) = @_;
  24. if ($self->{accepted}) {
  25. Bus::DialogSave::AlreadyAccepted->throw("The dialog is already accepted.");
  26. } else {
  27. $self->{accepted} = 1;
  28. $self->{receivedEvent} = $self->{bus}->onMessageReceived->add($self, &messageReceived);
  29. $self->{dialogID} = refaddr($self);
  30. $self->{bus}->send("ACCEPTED", {
  31. TO       => $self->{peerID},
  32. SEQ      => $self->{seq},
  33. IRY      => 1,
  34. dialogID => $self->{dialogID}
  35. });
  36. }
  37. }
  38. sub getQuery {
  39. my ($self) = @_;
  40. if ($self->{query}) {
  41. my $query = $self->{query};
  42. delete $self->{query};
  43. return @{$query};
  44. } else {
  45. return undef;
  46. }
  47. }
  48. sub reply {
  49. my ($self, $MID, $args) = @_;
  50. my %args2;
  51. %args2 = %{$args} if ($args);
  52. $args2{TO}  = $self->{peerID};
  53. $args2{SEQ} = $self->{currentSeq};
  54. $args2{IRY} = 1;
  55. $self->{bus}->send($MID, %args2);
  56. }
  57. sub messageReceived {
  58. my ($self, undef, $message) = @_;
  59. my $args = $message->{args};
  60. if ($args->{dialogID} eq $self->{dialogID} && $args->{FROM} eq $self->{peerID}) {
  61. # We received a new query for this dialog.
  62. $self->{query} = [$message->{messageID}, $args];
  63. $self->{currentSeq} = $args->{SEQ};
  64. }
  65. }
  66. 1;