<commit_before>from twisted.protocols import stateful
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint

from datetime import datetime

import sys
import struct
import zlib
import time
import threading
import Queue
import uuid

import AmmoMessages_pb2

class AndroidProtocol(stateful.StatefulProtocol):
  '''
  This class implements the stateful Android <-> Gateway protocol.  It contains
  an 8-byte header with messageSize and a checksum, then a protobuf
  MessageWrapper object (of length messageSize).
  
  We use Twisted's StatefulProtocol to implement this protocol--  states are
  composed of a callback function and a size; Twisted calls the callback
  function when <size> data has been received.  The callback functions return
  the next state.
  '''
  _messageSize = 0
  _checksum = 0
  _onMessageAvailableCallback = None
  
  def getInitialState(self):
    return (self.receiveHeader, 8) #initial state receives the header
  
  def receiveHeader(self, data):
    (messageSize, checksum) = struct.unpack("<Ii", data)
    self._messageSize = messageSize
    self._checksum = checksum
    
    return (self.receiveData, self._messageSize)
    
  def receiveData(self, data):
    calculatedChecksum = zlib.crc32(data)
    if calculatedChecksum != self._checksum:
      print "Checksum error!"
    msg = AmmoMessages_pb2.MessageWrapper()
    msg.ParseFromString(data)
    
    if self._onMessageAvailableCallback != None:
      self._onMessageAvailableCallback(msg)
    
    return (self.receiveHeader, 8)
    
  def sendMessageWrapper(self, msg):
    serializedMsg = msg.SerializeToString()
    self.transport.write(struct.pack("<Ii", len(serializedMsg), zlib.crc32(serializedMsg))) #little-endian byte order for now
    self.transport.write(serializedMsg);
    
  def connectionMade(self):<commit_msg>Modify AndroidConnector for new protocol<commit_after>from twisted.protocols import stateful
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint

from datetime import datetime

import sys
import struct
import zlib
import time
import threading
import Queue
import uuid

import AmmoMessages_pb2

MAGIC_NUMBER = 0xfeedbeef

class AndroidProtocol(stateful.StatefulProtocol):
  '''
  This class implements the stateful Android <-> Gateway protocol.  It contains
  an 8-byte header with messageSize and a checksum, then a protobuf
  MessageWrapper object (of length messageSize).
  
  We use Twisted's StatefulProtocol to implement this protocol--  states are
  composed of a callback function and a size; Twisted calls the callback
  function when <size> data has been received.  The callback functions return
  the next state.
  '''
  _messageSize = 0
  _checksum = 0
  _onMessageAvailableCallback = None
  
  def getInitialState(self):
    return (self.receiveHeader, 20) #initial state receives the header
  
  def receiveHeader(self, data):
    (magicNumber, messageSize, priority, reserved1, reserved2, reserved3, checksum, headerChecksum) = struct.unpack("<IIbbbbii", data)
    calculatedHeaderChecksum = zlib.crc32(data[:16])
    if magicNumber != MAGIC_NUMBER:
      print "Invalid magic number!"
    if calculatedHeaderChecksum != self._headerChecksum:
      print "Header checksum error!"
    self._messageSize = messageSize
    self._checksum = checksum
    
    return (self.receiveData, self._messageSize)
    
  def receiveData(self, data):
    calculatedChecksum = zlib.crc32(data)
    if calculatedChecksum != self._checksum:
      print "Checksum error!"
    msg = AmmoMessages_pb2.MessageWrapper()
    msg.ParseFromString(data)
    
    if self._onMessageAvailableCallback != None:
      self._onMessageAvailableCallback(msg)
    
    return (self.receiveHeader, 8)
    
  def sendMessageWrapper(self, msg):
    serializedMsg = msg.SerializeToString()
    messageHeader = struct.pack("<IIbbbbi", MAGIC_NUMBER, len(serializedMsg), DEFAULT_PRIORITY, DEFAULT_RESERVED1, DEFAULT_RESERVED2, DEFAULT_RESERVED3, zlib.crc32(serializedMsg))
    headerChecksum = zlib.crc32(messageHeader)
    messageHeader = messageHeader + struct.pack("i", headerChecksum)
    
    self.transport.write(messageHeader) #little-endian byte order for now
    self.transport.write(serializedMsg);
    
  def connectionMade(self):
{'fraction_non_alphanumeric': 0.07347953877458738, 'fraction_numerical': 0.008591453764413294, 'mean_word_length': 4.3559322033898304, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377378', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1256, 'n_words': 376}