Advertisement
tantraputra

Nohmad.pm

Oct 19th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.41 KB | None | 0 0
  1. package Nohmad;
  2.  
  3. use Modern::Perl '2012';
  4. use DateTime;
  5. use MIME::Base64;
  6. use Log::Log4perl;
  7. use bytes;
  8.  
  9. use Database;
  10. #-------------------------------------------------------------------------------
  11. #  Initialize logger
  12. #-------------------------------------------------------------------------------
  13. Log::Log4perl::init("log4perl.conf");
  14. my $logger = Log::Log4perl->get_logger();
  15. $Log::Log4perl::JOIN_MSG_ARRAY_CHAR = " <*> ";
  16.  
  17. #===  FUNCTION  ================================================================
  18. #         NAME: packet
  19. #      PURPOSE: Entry point and main function
  20. #   PARAMETERS: $heap : POE session heap
  21. #               $buf : content of a package, raw binary data
  22. #               $devices : list of devices with active sessions and their data
  23. #               $sid : POE session ID
  24. #      RETURNS: $reply
  25. #  DESCRIPTION: Main function that calls sub functions
  26. #       THROWS: no exceptions
  27. #     COMMENTS: none
  28. #     SEE ALSO: n/a
  29. #===============================================================================
  30. sub packet {
  31.     my ( $heap, $buf, $devices, $sid ) = @_;
  32.  
  33.     my ( @header ) = unpack("H2" x 3, $buf);
  34.     my $msg_len = hex shift @header;
  35.     my $pkg_id = hex shift @header;
  36.     my $pkg_type = shift @header;
  37.     my $pkg = substr $buf, 3;
  38.  
  39.     my $reply;
  40.  
  41.     given ($pkg_type) {
  42.         when ('00') { $reply = _error( $heap, $pkg ); }
  43.         when ('01') { $reply = _identification( $heap, $pkg, $devices, $sid ); }
  44.         when ('02') { $reply = _hello( $heap, $pkg ); }
  45.         when ('03') { $reply = _fingerprint_receipt( $heap, $pkg, $pkg_id ); }
  46.         when ('10') { $reply = _transactions( $heap, $pkg, $pkg_id ); }
  47.         when ('20') { $reply = _enrol_result( $pkg, $pkg_id, $heap ); }
  48.         when ('21') { $reply = _templates( $pkg, $pkg_id, $heap->{imei} ); }
  49.         when ('22') { $reply = _enrolment_complete( $heap, $pkg_id ); }
  50.         when ('fe') { $reply = _enrol_request_admin( $devices, $pkg ); }
  51.         when ('ff') { $reply = _update_firmware( $heap, $pkg ); }
  52.         default {
  53. #-------------------------------------------------------------------------------
  54. #  LOGLEVEL ERROR
  55. #-------------------------------------------------------------------------------
  56.             $logger->error( "Unknown type: " . unpack( "(H2)*", $pkg ),
  57.                 $heap->{imei} || '' );
  58.         }
  59.     }
  60.  
  61.     return $reply;
  62. }
  63.  
  64. <!-- snip -->
  65.  
  66. sub _pending_jobs {
  67.     my ( $heap ) = @_;
  68.  
  69.     _check_stale_lock( $heap );
  70.  
  71.     if ( exists $heap->{block_processing} ) {
  72.         $heap->{socket_wheel}->put( $heap->{blocked_payload} )
  73.           if exists $heap->{blocked_payload};
  74. #-------------------------------------------------------------------------------
  75. #  LOGLEVEL DEBUG
  76. #-------------------------------------------------------------------------------
  77.         $logger->debug( "=[HELLO] NOAC block processing =", $heap->{imei} )
  78.           if $heap->{block_processing};
  79.         return;
  80.     }
  81.  
  82.     if ( exists $heap->{pending_processes} ) {
  83. #-------------------------------------------------------------------------------
  84. #  LOGLEVEL DEBUG
  85. #-------------------------------------------------------------------------------
  86.         $logger->debug( "=[HELLO] NOAC pending processes =", $heap->{imei} )
  87.           if exists $heap->{pending_processes};
  88.         return;
  89.     }
  90.  
  91.     #Run the check
  92.     return Database::_check4pending( $heap->{imei} );
  93. }
  94.  
  95. <!-- snip -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement