#NEWLINE# def _write_message( self , notification , devices , chunk_size ) : #NEWLINE# #INDENT# '\n        Writes the message for the supplied devices to\n        the APN Service SSL socket.\n        ' #NEWLINE# if ( not isinstance ( notification , Notification ) ) : #NEWLINE# #INDENT# raise TypeError ( 'notification should be an instance of ios_notifications.models.Notification' ) #NEWLINE# #UNINDENT# if ( ( not isinstance ( chunk_size , int ) ) or ( chunk_size < 1 ) ) : #NEWLINE# #INDENT# raise ValueError ( 'chunk_size must be an integer greater than zero.' ) #NEWLINE# #UNINDENT# payload = notification . payload #NEWLINE# device_length = ( devices . count ( ) if isinstance ( devices , models . query . QuerySet ) else len ( devices ) ) #NEWLINE# chunks = [ devices [ i : ( i + chunk_size ) ] for i in xrange ( 0 , device_length , chunk_size ) ] #NEWLINE# for index in xrange ( len ( chunks ) ) : #NEWLINE# #INDENT# chunk = chunks [ index ] #NEWLINE# self . _connect ( ) #NEWLINE# for device in chunk : #NEWLINE# #INDENT# if ( not device . is_active ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# self . connection . send ( self . pack_message ( payload , device ) ) #NEWLINE# #UNINDENT# except ( OpenSSL . SSL . WantWriteError , socket . error ) as e : #NEWLINE# #INDENT# if ( isinstance ( e , socket . error ) and isinstance ( e . args , tuple ) and ( e . args [ 0 ] != errno . EPIPE ) ) : #NEWLINE# #INDENT# raise e #NEWLINE# #UNINDENT# self . _disconnect ( ) #NEWLINE# i = chunk . index ( device ) #NEWLINE# self . set_devices_last_notified_at ( chunk [ : i ] ) #NEWLINE# self . _write_message ( notification , chunk [ ( i + 1 ) : ] , chunk_size ) #NEWLINE# #UNINDENT# #UNINDENT# self . _disconnect ( ) #NEWLINE# self . set_devices_last_notified_at ( chunk ) #NEWLINE# #UNINDENT# if ( notification . pk or notification . persist ) : #NEWLINE# #INDENT# notification . last_sent_at = dt_now ( ) #NEWLINE# notification . save ( )