id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
7,800
estnltk/estnltk
estnltk/np_chunker.py
NounPhraseChunker._getPOS
def _getPOS( self, token, onlyFirst = True ): ''' Returns POS of the current token. ''' if onlyFirst: return token[ANALYSIS][0][POSTAG] else: return [ a[POSTAG] for a in token[ANALYSIS] ]
python
def _getPOS( self, token, onlyFirst = True ): ''' Returns POS of the current token. ''' if onlyFirst: return token[ANALYSIS][0][POSTAG] else: return [ a[POSTAG] for a in token[ANALYSIS] ]
[ "def", "_getPOS", "(", "self", ",", "token", ",", "onlyFirst", "=", "True", ")", ":", "if", "onlyFirst", ":", "return", "token", "[", "ANALYSIS", "]", "[", "0", "]", "[", "POSTAG", "]", "else", ":", "return", "[", "a", "[", "POSTAG", "]", "for", ...
Returns POS of the current token.
[ "Returns", "POS", "of", "the", "current", "token", "." ]
28ae334a68a0673072febc318635f04da0dcc54a
https://github.com/estnltk/estnltk/blob/28ae334a68a0673072febc318635f04da0dcc54a/estnltk/np_chunker.py#L201-L207
7,801
estnltk/estnltk
estnltk/dividing.py
divide
def divide(elements, by, translate=False, sep=' '): """Divide lists `elements` and `by`. All elements are grouped into N bins, where N denotes the elements in `by` list. Parameters ---------- elements: list of dict Elements to be grouped into bins. by: list of dict Elements defining the bins. translate: bool (default: False) When dividing, also translate start and end positions of elements. sep: str (default ' ') In case of multispans, what is the default text separator. This is required in order to tag correct start, end positions of elements. """ outer_spans = [spans(elem) for elem in by] return divide_by_spans(elements, outer_spans, translate=translate, sep=sep)
python
def divide(elements, by, translate=False, sep=' '): outer_spans = [spans(elem) for elem in by] return divide_by_spans(elements, outer_spans, translate=translate, sep=sep)
[ "def", "divide", "(", "elements", ",", "by", ",", "translate", "=", "False", ",", "sep", "=", "' '", ")", ":", "outer_spans", "=", "[", "spans", "(", "elem", ")", "for", "elem", "in", "by", "]", "return", "divide_by_spans", "(", "elements", ",", "out...
Divide lists `elements` and `by`. All elements are grouped into N bins, where N denotes the elements in `by` list. Parameters ---------- elements: list of dict Elements to be grouped into bins. by: list of dict Elements defining the bins. translate: bool (default: False) When dividing, also translate start and end positions of elements. sep: str (default ' ') In case of multispans, what is the default text separator. This is required in order to tag correct start, end positions of elements.
[ "Divide", "lists", "elements", "and", "by", ".", "All", "elements", "are", "grouped", "into", "N", "bins", "where", "N", "denotes", "the", "elements", "in", "by", "list", "." ]
28ae334a68a0673072febc318635f04da0dcc54a
https://github.com/estnltk/estnltk/blob/28ae334a68a0673072febc318635f04da0dcc54a/estnltk/dividing.py#L338-L355
7,802
estnltk/estnltk
estnltk/disambiguator.py
Disambiguator.__isListOfTexts
def __isListOfTexts(self, docs): """ Checks whether the input is a list of strings or Text-s; """ return isinstance(docs, list) and \ all(isinstance(d, (basestring, Text)) for d in docs)
python
def __isListOfTexts(self, docs): return isinstance(docs, list) and \ all(isinstance(d, (basestring, Text)) for d in docs)
[ "def", "__isListOfTexts", "(", "self", ",", "docs", ")", ":", "return", "isinstance", "(", "docs", ",", "list", ")", "and", "all", "(", "isinstance", "(", "d", ",", "(", "basestring", ",", "Text", ")", ")", "for", "d", "in", "docs", ")" ]
Checks whether the input is a list of strings or Text-s;
[ "Checks", "whether", "the", "input", "is", "a", "list", "of", "strings", "or", "Text", "-", "s", ";" ]
28ae334a68a0673072febc318635f04da0dcc54a
https://github.com/estnltk/estnltk/blob/28ae334a68a0673072febc318635f04da0dcc54a/estnltk/disambiguator.py#L135-L139
7,803
estnltk/estnltk
estnltk/wordnet_tagger.py
WordnetTagger.tag_text
def tag_text(self, text, **kwargs): """Annotates `analysis` entries in `corpus` with a list of lemmas` synsets and queried WordNet data in a 'wordnet' entry. Note ---- Annotates every `analysis` entry with a `wordnet`:{`synsets`:[..]}. Parameters ---------- text: estnltk.text.Text Representation of a corpus in a disassembled form for automatic text analysis with word-level `analysis` entry. E.g. corpus disassembled into paragraphs, sentences, words ({'paragraphs':[{'sentences':[{'words':[{'analysis':{...}},..]},..]},..]}). pos : boolean, optional If True, annotates each synset with a correspnding `pos` (part-of-speech) tag. variants : boolean, optional If True, annotates each synset with a list of all its variants' (lemmas') literals. var_sense : boolean, optional If True and `variants` is True, annotates each variant/lemma with its sense number. var_definition : boolean, optional If True and `variants` is True, annotates each variant/lemma with its definition. Definitions often missing in WordNet. var_examples : boolean, optional If True and `variants` is True, annotates each variant/lemma with a list of its examples. Examples often missing in WordNet. relations : list of str, optional Holds interested relations. Legal relations are as follows: `antonym`, `be_in_state`, `belongs_to_class`, `causes`, `fuzzynym`, `has_holo_location`, `has_holo_madeof`, `has_holo_member`, `has_holo_part`, `has_holo_portion`, `has_holonym`, `has_hyperonym`, `has_hyponym`, `has_instance`, `has_mero_location`, `has_mero_madeof`, `has_mero_member`, `has_mero_part`, `has_mero_portion`, `has_meronym`, `has_subevent`, `has_xpos_hyperonym`, `has_xpos_hyponym`, `involved`, `involved_agent`, `involved_instrument`, `involved_location`, `involved_patient`, `involved_target_direction`, `is_caused_by`, `is_subevent_of`, `near_antonym`, `near_synonym`, `role`, `role_agent`, `role_instrument`, `role_location`, `role_patient`, `role_target_direction`, `state_of`, `xpos_fuzzynym`, `xpos_near_antonym`, `xpos_near_synonym`. Annotates each synset with related synsets' indices with respect to queried relations. Returns ------- estnltk.text.Text In-place annotated `text`. """ for analysis_match in text.analysis: for candidate in analysis_match: if candidate['partofspeech'] in PYVABAMORF_TO_WORDNET_POS_MAP: # Wordnet contains data about the given lemma and pos combination - will annotate. wordnet_obj = {} tag_synsets(wordnet_obj, candidate, **kwargs) return text
python
def tag_text(self, text, **kwargs): for analysis_match in text.analysis: for candidate in analysis_match: if candidate['partofspeech'] in PYVABAMORF_TO_WORDNET_POS_MAP: # Wordnet contains data about the given lemma and pos combination - will annotate. wordnet_obj = {} tag_synsets(wordnet_obj, candidate, **kwargs) return text
[ "def", "tag_text", "(", "self", ",", "text", ",", "*", "*", "kwargs", ")", ":", "for", "analysis_match", "in", "text", ".", "analysis", ":", "for", "candidate", "in", "analysis_match", ":", "if", "candidate", "[", "'partofspeech'", "]", "in", "PYVABAMORF_T...
Annotates `analysis` entries in `corpus` with a list of lemmas` synsets and queried WordNet data in a 'wordnet' entry. Note ---- Annotates every `analysis` entry with a `wordnet`:{`synsets`:[..]}. Parameters ---------- text: estnltk.text.Text Representation of a corpus in a disassembled form for automatic text analysis with word-level `analysis` entry. E.g. corpus disassembled into paragraphs, sentences, words ({'paragraphs':[{'sentences':[{'words':[{'analysis':{...}},..]},..]},..]}). pos : boolean, optional If True, annotates each synset with a correspnding `pos` (part-of-speech) tag. variants : boolean, optional If True, annotates each synset with a list of all its variants' (lemmas') literals. var_sense : boolean, optional If True and `variants` is True, annotates each variant/lemma with its sense number. var_definition : boolean, optional If True and `variants` is True, annotates each variant/lemma with its definition. Definitions often missing in WordNet. var_examples : boolean, optional If True and `variants` is True, annotates each variant/lemma with a list of its examples. Examples often missing in WordNet. relations : list of str, optional Holds interested relations. Legal relations are as follows: `antonym`, `be_in_state`, `belongs_to_class`, `causes`, `fuzzynym`, `has_holo_location`, `has_holo_madeof`, `has_holo_member`, `has_holo_part`, `has_holo_portion`, `has_holonym`, `has_hyperonym`, `has_hyponym`, `has_instance`, `has_mero_location`, `has_mero_madeof`, `has_mero_member`, `has_mero_part`, `has_mero_portion`, `has_meronym`, `has_subevent`, `has_xpos_hyperonym`, `has_xpos_hyponym`, `involved`, `involved_agent`, `involved_instrument`, `involved_location`, `involved_patient`, `involved_target_direction`, `is_caused_by`, `is_subevent_of`, `near_antonym`, `near_synonym`, `role`, `role_agent`, `role_instrument`, `role_location`, `role_patient`, `role_target_direction`, `state_of`, `xpos_fuzzynym`, `xpos_near_antonym`, `xpos_near_synonym`. Annotates each synset with related synsets' indices with respect to queried relations. Returns ------- estnltk.text.Text In-place annotated `text`.
[ "Annotates", "analysis", "entries", "in", "corpus", "with", "a", "list", "of", "lemmas", "synsets", "and", "queried", "WordNet", "data", "in", "a", "wordnet", "entry", "." ]
28ae334a68a0673072febc318635f04da0dcc54a
https://github.com/estnltk/estnltk/blob/28ae334a68a0673072febc318635f04da0dcc54a/estnltk/wordnet_tagger.py#L22-L67
7,804
estnltk/estnltk
estnltk/tools/cnllconverter.py
get_texts_and_labels
def get_texts_and_labels(sentence_chunk): """Given a sentence chunk, extract original texts and labels.""" words = sentence_chunk.split('\n') texts = [] labels = [] for word in words: word = word.strip() if len(word) > 0: toks = word.split('\t') texts.append(toks[0].strip()) labels.append(toks[-1].strip()) return texts, labels
python
def get_texts_and_labels(sentence_chunk): words = sentence_chunk.split('\n') texts = [] labels = [] for word in words: word = word.strip() if len(word) > 0: toks = word.split('\t') texts.append(toks[0].strip()) labels.append(toks[-1].strip()) return texts, labels
[ "def", "get_texts_and_labels", "(", "sentence_chunk", ")", ":", "words", "=", "sentence_chunk", ".", "split", "(", "'\\n'", ")", "texts", "=", "[", "]", "labels", "=", "[", "]", "for", "word", "in", "words", ":", "word", "=", "word", ".", "strip", "(",...
Given a sentence chunk, extract original texts and labels.
[ "Given", "a", "sentence", "chunk", "extract", "original", "texts", "and", "labels", "." ]
28ae334a68a0673072febc318635f04da0dcc54a
https://github.com/estnltk/estnltk/blob/28ae334a68a0673072febc318635f04da0dcc54a/estnltk/tools/cnllconverter.py#L12-L23
7,805
estnltk/estnltk
estnltk/tools/cnllconverter.py
convert
def convert(document): """Convert a document to a Text object""" raw_tokens = [] curpos = 0 text_spans = [] all_labels = [] sent_spans = [] word_texts = [] for sentence in document: startpos = curpos for idx, (text, label) in enumerate(sentence): raw_tokens.append(text) word_texts.append(text) all_labels.append(label) text_spans.append((curpos, curpos+len(text))) curpos += len(text) if idx < len(sentence) - 1: raw_tokens.append(' ') curpos += 1 sent_spans.append((startpos, curpos)) raw_tokens.append('\n') curpos += 1 return { TEXT: ''.join(raw_tokens), WORDS: [{TEXT: text, START: start, END: end, LABEL: label} for text, (start, end), label in zip(word_texts, text_spans, all_labels)], SENTENCES: [{START: start, END:end} for start, end in sent_spans] }
python
def convert(document): raw_tokens = [] curpos = 0 text_spans = [] all_labels = [] sent_spans = [] word_texts = [] for sentence in document: startpos = curpos for idx, (text, label) in enumerate(sentence): raw_tokens.append(text) word_texts.append(text) all_labels.append(label) text_spans.append((curpos, curpos+len(text))) curpos += len(text) if idx < len(sentence) - 1: raw_tokens.append(' ') curpos += 1 sent_spans.append((startpos, curpos)) raw_tokens.append('\n') curpos += 1 return { TEXT: ''.join(raw_tokens), WORDS: [{TEXT: text, START: start, END: end, LABEL: label} for text, (start, end), label in zip(word_texts, text_spans, all_labels)], SENTENCES: [{START: start, END:end} for start, end in sent_spans] }
[ "def", "convert", "(", "document", ")", ":", "raw_tokens", "=", "[", "]", "curpos", "=", "0", "text_spans", "=", "[", "]", "all_labels", "=", "[", "]", "sent_spans", "=", "[", "]", "word_texts", "=", "[", "]", "for", "sentence", "in", "document", ":"...
Convert a document to a Text object
[ "Convert", "a", "document", "to", "a", "Text", "object" ]
28ae334a68a0673072febc318635f04da0dcc54a
https://github.com/estnltk/estnltk/blob/28ae334a68a0673072febc318635f04da0dcc54a/estnltk/tools/cnllconverter.py#L36-L62
7,806
agoragames/haigha
haigha/classes/transaction_class.py
TransactionClass.select
def select(self, cb=None): ''' Set this channel to use transactions. ''' if not self._enabled: self._enabled = True self.send_frame(MethodFrame(self.channel_id, 90, 10)) self._select_cb.append(cb) self.channel.add_synchronous_cb(self._recv_select_ok)
python
def select(self, cb=None): ''' Set this channel to use transactions. ''' if not self._enabled: self._enabled = True self.send_frame(MethodFrame(self.channel_id, 90, 10)) self._select_cb.append(cb) self.channel.add_synchronous_cb(self._recv_select_ok)
[ "def", "select", "(", "self", ",", "cb", "=", "None", ")", ":", "if", "not", "self", ".", "_enabled", ":", "self", ".", "_enabled", "=", "True", "self", ".", "send_frame", "(", "MethodFrame", "(", "self", ".", "channel_id", ",", "90", ",", "10", ")...
Set this channel to use transactions.
[ "Set", "this", "channel", "to", "use", "transactions", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/transaction_class.py#L54-L62
7,807
agoragames/haigha
haigha/classes/transaction_class.py
TransactionClass.commit
def commit(self, cb=None): ''' Commit the current transaction. Caller can specify a callback to use when the transaction is committed. ''' # Could call select() but spec 1.9.2.3 says to raise an exception if not self.enabled: raise self.TransactionsNotEnabled() self.send_frame(MethodFrame(self.channel_id, 90, 20)) self._commit_cb.append(cb) self.channel.add_synchronous_cb(self._recv_commit_ok)
python
def commit(self, cb=None): ''' Commit the current transaction. Caller can specify a callback to use when the transaction is committed. ''' # Could call select() but spec 1.9.2.3 says to raise an exception if not self.enabled: raise self.TransactionsNotEnabled() self.send_frame(MethodFrame(self.channel_id, 90, 20)) self._commit_cb.append(cb) self.channel.add_synchronous_cb(self._recv_commit_ok)
[ "def", "commit", "(", "self", ",", "cb", "=", "None", ")", ":", "# Could call select() but spec 1.9.2.3 says to raise an exception", "if", "not", "self", ".", "enabled", ":", "raise", "self", ".", "TransactionsNotEnabled", "(", ")", "self", ".", "send_frame", "(",...
Commit the current transaction. Caller can specify a callback to use when the transaction is committed.
[ "Commit", "the", "current", "transaction", ".", "Caller", "can", "specify", "a", "callback", "to", "use", "when", "the", "transaction", "is", "committed", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/transaction_class.py#L69-L80
7,808
agoragames/haigha
haigha/classes/transaction_class.py
TransactionClass.rollback
def rollback(self, cb=None): ''' Abandon all message publications and acks in the current transaction. Caller can specify a callback to use when the transaction has been aborted. ''' # Could call select() but spec 1.9.2.5 says to raise an exception if not self.enabled: raise self.TransactionsNotEnabled() self.send_frame(MethodFrame(self.channel_id, 90, 30)) self._rollback_cb.append(cb) self.channel.add_synchronous_cb(self._recv_rollback_ok)
python
def rollback(self, cb=None): ''' Abandon all message publications and acks in the current transaction. Caller can specify a callback to use when the transaction has been aborted. ''' # Could call select() but spec 1.9.2.5 says to raise an exception if not self.enabled: raise self.TransactionsNotEnabled() self.send_frame(MethodFrame(self.channel_id, 90, 30)) self._rollback_cb.append(cb) self.channel.add_synchronous_cb(self._recv_rollback_ok)
[ "def", "rollback", "(", "self", ",", "cb", "=", "None", ")", ":", "# Could call select() but spec 1.9.2.5 says to raise an exception", "if", "not", "self", ".", "enabled", ":", "raise", "self", ".", "TransactionsNotEnabled", "(", ")", "self", ".", "send_frame", "(...
Abandon all message publications and acks in the current transaction. Caller can specify a callback to use when the transaction has been aborted.
[ "Abandon", "all", "message", "publications", "and", "acks", "in", "the", "current", "transaction", ".", "Caller", "can", "specify", "a", "callback", "to", "use", "when", "the", "transaction", "has", "been", "aborted", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/transaction_class.py#L87-L99
7,809
agoragames/haigha
haigha/connection.py
Connection.synchronous
def synchronous(self): ''' True if transport is synchronous or the connection has been forced into synchronous mode, False otherwise. ''' if self._transport is None: if self._close_info and len(self._close_info['reply_text']) > 0: raise ConnectionClosed("connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) raise ConnectionClosed("connection is closed") return self.transport.synchronous or self._synchronous
python
def synchronous(self): ''' True if transport is synchronous or the connection has been forced into synchronous mode, False otherwise. ''' if self._transport is None: if self._close_info and len(self._close_info['reply_text']) > 0: raise ConnectionClosed("connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) raise ConnectionClosed("connection is closed") return self.transport.synchronous or self._synchronous
[ "def", "synchronous", "(", "self", ")", ":", "if", "self", ".", "_transport", "is", "None", ":", "if", "self", ".", "_close_info", "and", "len", "(", "self", ".", "_close_info", "[", "'reply_text'", "]", ")", ">", "0", ":", "raise", "ConnectionClosed", ...
True if transport is synchronous or the connection has been forced into synchronous mode, False otherwise.
[ "True", "if", "transport", "is", "synchronous", "or", "the", "connection", "has", "been", "forced", "into", "synchronous", "mode", "False", "otherwise", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L191-L202
7,810
agoragames/haigha
haigha/connection.py
Connection.connect
def connect(self, host, port): ''' Connect to a host and port. ''' # Clear the connect state immediately since we're no longer connected # at this point. self._connected = False # Only after the socket has connected do we clear this state; closed # must be False so that writes can be buffered in writePacket(). The # closed state might have been set to True due to a socket error or a # redirect. self._host = "%s:%d" % (host, port) self._closed = False self._close_info = { 'reply_code': 0, 'reply_text': 'failed to connect to %s' % (self._host), 'class_id': 0, 'method_id': 0 } self._transport.connect((host, port)) self._transport.write(PROTOCOL_HEADER) self._last_octet_time = time.time() if self._synchronous_connect: # Have to queue this callback just after connect, it can't go # into the constructor because the channel needs to be # "always there" for frame processing, but the synchronous # callback can't be added until after the protocol header has # been written. This SHOULD be registered before the protocol # header is written, in the case where the header bytes are # written, but this thread/greenlet/context does not return until # after another thread/greenlet/context has read and processed the # recv_start frame. Without more re-write to add_sync_cb though, # it will block on reading responses that will never arrive # because the protocol header isn't written yet. TBD if needs # refactoring. Could encapsulate entirely here, wherein # read_frames exits if protocol header not yet written. Like other # synchronous behaviors, adding this callback will result in a # blocking frame read and process loop until _recv_start and any # subsequent synchronous callbacks have been processed. In the # event that this is /not/ a synchronous transport, but the # caller wants the connect to be synchronous so as to ensure that # the connection is ready, then do a read frame loop here. self._channels[0].add_synchronous_cb(self._channels[0]._recv_start) while not self._connected: self.read_frames()
python
def connect(self, host, port): ''' Connect to a host and port. ''' # Clear the connect state immediately since we're no longer connected # at this point. self._connected = False # Only after the socket has connected do we clear this state; closed # must be False so that writes can be buffered in writePacket(). The # closed state might have been set to True due to a socket error or a # redirect. self._host = "%s:%d" % (host, port) self._closed = False self._close_info = { 'reply_code': 0, 'reply_text': 'failed to connect to %s' % (self._host), 'class_id': 0, 'method_id': 0 } self._transport.connect((host, port)) self._transport.write(PROTOCOL_HEADER) self._last_octet_time = time.time() if self._synchronous_connect: # Have to queue this callback just after connect, it can't go # into the constructor because the channel needs to be # "always there" for frame processing, but the synchronous # callback can't be added until after the protocol header has # been written. This SHOULD be registered before the protocol # header is written, in the case where the header bytes are # written, but this thread/greenlet/context does not return until # after another thread/greenlet/context has read and processed the # recv_start frame. Without more re-write to add_sync_cb though, # it will block on reading responses that will never arrive # because the protocol header isn't written yet. TBD if needs # refactoring. Could encapsulate entirely here, wherein # read_frames exits if protocol header not yet written. Like other # synchronous behaviors, adding this callback will result in a # blocking frame read and process loop until _recv_start and any # subsequent synchronous callbacks have been processed. In the # event that this is /not/ a synchronous transport, but the # caller wants the connect to be synchronous so as to ensure that # the connection is ready, then do a read frame loop here. self._channels[0].add_synchronous_cb(self._channels[0]._recv_start) while not self._connected: self.read_frames()
[ "def", "connect", "(", "self", ",", "host", ",", "port", ")", ":", "# Clear the connect state immediately since we're no longer connected", "# at this point.", "self", ".", "_connected", "=", "False", "# Only after the socket has connected do we clear this state; closed", "# must...
Connect to a host and port.
[ "Connect", "to", "a", "host", "and", "port", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L204-L252
7,811
agoragames/haigha
haigha/connection.py
Connection.disconnect
def disconnect(self): ''' Disconnect from the current host, but do not update the closed state. After the transport is disconnected, the closed state will be True if this is called after a protocol shutdown, or False if the disconnect was in error. TODO: do we really need closed vs. connected states? this only adds complication and the whole reconnect process has been scrapped anyway. ''' self._connected = False if self._transport is not None: try: self._transport.disconnect() except Exception: self.logger.error( "Failed to disconnect from %s", self._host, exc_info=True) raise finally: self._transport = None
python
def disconnect(self): ''' Disconnect from the current host, but do not update the closed state. After the transport is disconnected, the closed state will be True if this is called after a protocol shutdown, or False if the disconnect was in error. TODO: do we really need closed vs. connected states? this only adds complication and the whole reconnect process has been scrapped anyway. ''' self._connected = False if self._transport is not None: try: self._transport.disconnect() except Exception: self.logger.error( "Failed to disconnect from %s", self._host, exc_info=True) raise finally: self._transport = None
[ "def", "disconnect", "(", "self", ")", ":", "self", ".", "_connected", "=", "False", "if", "self", ".", "_transport", "is", "not", "None", ":", "try", ":", "self", ".", "_transport", ".", "disconnect", "(", ")", "except", "Exception", ":", "self", ".",...
Disconnect from the current host, but do not update the closed state. After the transport is disconnected, the closed state will be True if this is called after a protocol shutdown, or False if the disconnect was in error. TODO: do we really need closed vs. connected states? this only adds complication and the whole reconnect process has been scrapped anyway.
[ "Disconnect", "from", "the", "current", "host", "but", "do", "not", "update", "the", "closed", "state", ".", "After", "the", "transport", "is", "disconnected", "the", "closed", "state", "will", "be", "True", "if", "this", "is", "called", "after", "a", "pro...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L254-L274
7,812
agoragames/haigha
haigha/connection.py
Connection._next_channel_id
def _next_channel_id(self): '''Return the next possible channel id. Is a circular enumeration.''' self._channel_counter += 1 if self._channel_counter >= self._channel_max: self._channel_counter = 1 return self._channel_counter
python
def _next_channel_id(self): '''Return the next possible channel id. Is a circular enumeration.''' self._channel_counter += 1 if self._channel_counter >= self._channel_max: self._channel_counter = 1 return self._channel_counter
[ "def", "_next_channel_id", "(", "self", ")", ":", "self", ".", "_channel_counter", "+=", "1", "if", "self", ".", "_channel_counter", ">=", "self", ".", "_channel_max", ":", "self", ".", "_channel_counter", "=", "1", "return", "self", ".", "_channel_counter" ]
Return the next possible channel id. Is a circular enumeration.
[ "Return", "the", "next", "possible", "channel", "id", ".", "Is", "a", "circular", "enumeration", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L307-L312
7,813
agoragames/haigha
haigha/connection.py
Connection.channel
def channel(self, channel_id=None, synchronous=False): """ Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. If channel_id is not None but no channel exists for that id, will raise InvalidChannel. If there are already too many channels open, will raise TooManyChannels. If synchronous=True, then the channel will act synchronous in all cases where a protocol method supports `nowait=False`, or where there is an implied callback in the protocol. """ if channel_id is None: # adjust for channel 0 if len(self._channels) - 1 >= self._channel_max: raise Connection.TooManyChannels( "%d channels already open, max %d", len(self._channels) - 1, self._channel_max) channel_id = self._next_channel_id() while channel_id in self._channels: channel_id = self._next_channel_id() elif channel_id in self._channels: return self._channels[channel_id] else: raise Connection.InvalidChannel( "%s is not a valid channel id", channel_id) # Call open() here so that ConnectionChannel doesn't have it called. # Could also solve this other ways, but it's a HACK regardless. rval = Channel( self, channel_id, self._class_map, synchronous=synchronous) self._channels[channel_id] = rval rval.add_close_listener(self._channel_closed) rval.open() return rval
python
def channel(self, channel_id=None, synchronous=False): if channel_id is None: # adjust for channel 0 if len(self._channels) - 1 >= self._channel_max: raise Connection.TooManyChannels( "%d channels already open, max %d", len(self._channels) - 1, self._channel_max) channel_id = self._next_channel_id() while channel_id in self._channels: channel_id = self._next_channel_id() elif channel_id in self._channels: return self._channels[channel_id] else: raise Connection.InvalidChannel( "%s is not a valid channel id", channel_id) # Call open() here so that ConnectionChannel doesn't have it called. # Could also solve this other ways, but it's a HACK regardless. rval = Channel( self, channel_id, self._class_map, synchronous=synchronous) self._channels[channel_id] = rval rval.add_close_listener(self._channel_closed) rval.open() return rval
[ "def", "channel", "(", "self", ",", "channel_id", "=", "None", ",", "synchronous", "=", "False", ")", ":", "if", "channel_id", "is", "None", ":", "# adjust for channel 0", "if", "len", "(", "self", ".", "_channels", ")", "-", "1", ">=", "self", ".", "_...
Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. If channel_id is not None but no channel exists for that id, will raise InvalidChannel. If there are already too many channels open, will raise TooManyChannels. If synchronous=True, then the channel will act synchronous in all cases where a protocol method supports `nowait=False`, or where there is an implied callback in the protocol.
[ "Fetch", "a", "Channel", "object", "identified", "by", "the", "numeric", "channel_id", "or", "create", "that", "object", "if", "it", "doesn", "t", "already", "exist", ".", "If", "channel_id", "is", "not", "None", "but", "no", "channel", "exists", "for", "t...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L314-L348
7,814
agoragames/haigha
haigha/connection.py
Connection.read_frames
def read_frames(self): ''' Read frames from the transport and process them. Some transports may choose to do this in the background, in several threads, and so on. ''' # It's possible in a concurrent environment that our transport handle # has gone away, so handle that cleanly. # TODO: Consider moving this block into Translator base class. In many # ways it belongs there. One of the problems though is that this is # essentially the read loop. Each Transport has different rules for # how to kick this off, and in the case of gevent, this is how a # blocking call to read from the socket is kicked off. if self._transport is None: return # Send a heartbeat (if needed) self._channels[0].send_heartbeat() data = self._transport.read(self._heartbeat) current_time = time.time() if data is None: # Wait for 2 heartbeat intervals before giving up. See AMQP 4.2.7: # "If a peer detects no incoming traffic (i.e. received octets) for two heartbeat intervals or longer, # it should close the connection" if self._heartbeat and (current_time-self._last_octet_time > 2*self._heartbeat): msg = 'Heartbeats not received from %s for %d seconds' % (self._host, 2*self._heartbeat) self.transport_closed(msg=msg) raise ConnectionClosed('Connection is closed: ' + msg) return self._last_octet_time = current_time reader = Reader(data) p_channels = set() try: for frame in Frame.read_frames(reader): if self._debug > 1: self.logger.debug("READ: %s", frame) self._frames_read += 1 ch = self.channel(frame.channel_id) ch.buffer_frame(frame) p_channels.add(ch) except Frame.FrameError as e: # Frame error in the peer, disconnect self.close(reply_code=501, reply_text='frame error from %s : %s' % ( self._host, str(e)), class_id=0, method_id=0, disconnect=True) raise ConnectionClosed("connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) # NOTE: we process channels after buffering unused data in order to # preserve the integrity of the input stream in case a channel needs to # read input, such as when a channel framing error necessitates the use # of the synchronous channel.close method. See `Channel.process_frames`. # # HACK: read the buffer contents and re-buffer. Would prefer to pass # buffer back, but there's no good way of asking the total size of the # buffer, comparing to tell(), and then re-buffering. There's also no # ability to clear the buffer up to the current position. It would be # awesome if we could free that memory without a new allocation. if reader.tell() < len(data): self._transport.buffer(data[reader.tell():]) self._transport.process_channels(p_channels)
python
def read_frames(self): ''' Read frames from the transport and process them. Some transports may choose to do this in the background, in several threads, and so on. ''' # It's possible in a concurrent environment that our transport handle # has gone away, so handle that cleanly. # TODO: Consider moving this block into Translator base class. In many # ways it belongs there. One of the problems though is that this is # essentially the read loop. Each Transport has different rules for # how to kick this off, and in the case of gevent, this is how a # blocking call to read from the socket is kicked off. if self._transport is None: return # Send a heartbeat (if needed) self._channels[0].send_heartbeat() data = self._transport.read(self._heartbeat) current_time = time.time() if data is None: # Wait for 2 heartbeat intervals before giving up. See AMQP 4.2.7: # "If a peer detects no incoming traffic (i.e. received octets) for two heartbeat intervals or longer, # it should close the connection" if self._heartbeat and (current_time-self._last_octet_time > 2*self._heartbeat): msg = 'Heartbeats not received from %s for %d seconds' % (self._host, 2*self._heartbeat) self.transport_closed(msg=msg) raise ConnectionClosed('Connection is closed: ' + msg) return self._last_octet_time = current_time reader = Reader(data) p_channels = set() try: for frame in Frame.read_frames(reader): if self._debug > 1: self.logger.debug("READ: %s", frame) self._frames_read += 1 ch = self.channel(frame.channel_id) ch.buffer_frame(frame) p_channels.add(ch) except Frame.FrameError as e: # Frame error in the peer, disconnect self.close(reply_code=501, reply_text='frame error from %s : %s' % ( self._host, str(e)), class_id=0, method_id=0, disconnect=True) raise ConnectionClosed("connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) # NOTE: we process channels after buffering unused data in order to # preserve the integrity of the input stream in case a channel needs to # read input, such as when a channel framing error necessitates the use # of the synchronous channel.close method. See `Channel.process_frames`. # # HACK: read the buffer contents and re-buffer. Would prefer to pass # buffer back, but there's no good way of asking the total size of the # buffer, comparing to tell(), and then re-buffering. There's also no # ability to clear the buffer up to the current position. It would be # awesome if we could free that memory without a new allocation. if reader.tell() < len(data): self._transport.buffer(data[reader.tell():]) self._transport.process_channels(p_channels)
[ "def", "read_frames", "(", "self", ")", ":", "# It's possible in a concurrent environment that our transport handle", "# has gone away, so handle that cleanly.", "# TODO: Consider moving this block into Translator base class. In many", "# ways it belongs there. One of the problems though is that t...
Read frames from the transport and process them. Some transports may choose to do this in the background, in several threads, and so on.
[ "Read", "frames", "from", "the", "transport", "and", "process", "them", ".", "Some", "transports", "may", "choose", "to", "do", "this", "in", "the", "background", "in", "several", "threads", "and", "so", "on", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L393-L458
7,815
agoragames/haigha
haigha/connection.py
Connection._flush_buffered_frames
def _flush_buffered_frames(self): ''' Callback when protocol has been initialized on channel 0 and we're ready to send out frames to set up any channels that have been created. ''' # In the rare case (a bug) where this is called but send_frame thinks # they should be buffered, don't clobber. frames = self._output_frame_buffer self._output_frame_buffer = [] for frame in frames: self.send_frame(frame)
python
def _flush_buffered_frames(self): ''' Callback when protocol has been initialized on channel 0 and we're ready to send out frames to set up any channels that have been created. ''' # In the rare case (a bug) where this is called but send_frame thinks # they should be buffered, don't clobber. frames = self._output_frame_buffer self._output_frame_buffer = [] for frame in frames: self.send_frame(frame)
[ "def", "_flush_buffered_frames", "(", "self", ")", ":", "# In the rare case (a bug) where this is called but send_frame thinks", "# they should be buffered, don't clobber.", "frames", "=", "self", ".", "_output_frame_buffer", "self", ".", "_output_frame_buffer", "=", "[", "]", ...
Callback when protocol has been initialized on channel 0 and we're ready to send out frames to set up any channels that have been created.
[ "Callback", "when", "protocol", "has", "been", "initialized", "on", "channel", "0", "and", "we", "re", "ready", "to", "send", "out", "frames", "to", "set", "up", "any", "channels", "that", "have", "been", "created", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L460-L471
7,816
agoragames/haigha
haigha/connection.py
Connection.send_frame
def send_frame(self, frame): ''' Send a single frame. If there is no transport or we're not connected yet, append to the output buffer, else send immediately to the socket. This is called from within the MethodFrames. ''' if self._closed: if self._close_info and len(self._close_info['reply_text']) > 0: raise ConnectionClosed("connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) raise ConnectionClosed("connection is closed") if self._transport is None or \ (not self._connected and frame.channel_id != 0): self._output_frame_buffer.append(frame) return if self._debug > 1: self.logger.debug("WRITE: %s", frame) buf = bytearray() frame.write_frame(buf) if len(buf) > self._frame_max: self.close( reply_code=501, reply_text='attempted to send frame of %d bytes, frame max %d' % ( len(buf), self._frame_max), class_id=0, method_id=0, disconnect=True) raise ConnectionClosed( "connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) self._transport.write(buf) self._frames_written += 1
python
def send_frame(self, frame): ''' Send a single frame. If there is no transport or we're not connected yet, append to the output buffer, else send immediately to the socket. This is called from within the MethodFrames. ''' if self._closed: if self._close_info and len(self._close_info['reply_text']) > 0: raise ConnectionClosed("connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) raise ConnectionClosed("connection is closed") if self._transport is None or \ (not self._connected and frame.channel_id != 0): self._output_frame_buffer.append(frame) return if self._debug > 1: self.logger.debug("WRITE: %s", frame) buf = bytearray() frame.write_frame(buf) if len(buf) > self._frame_max: self.close( reply_code=501, reply_text='attempted to send frame of %d bytes, frame max %d' % ( len(buf), self._frame_max), class_id=0, method_id=0, disconnect=True) raise ConnectionClosed( "connection is closed: %s : %s" % (self._close_info['reply_code'], self._close_info['reply_text'])) self._transport.write(buf) self._frames_written += 1
[ "def", "send_frame", "(", "self", ",", "frame", ")", ":", "if", "self", ".", "_closed", ":", "if", "self", ".", "_close_info", "and", "len", "(", "self", ".", "_close_info", "[", "'reply_text'", "]", ")", ">", "0", ":", "raise", "ConnectionClosed", "("...
Send a single frame. If there is no transport or we're not connected yet, append to the output buffer, else send immediately to the socket. This is called from within the MethodFrames.
[ "Send", "a", "single", "frame", ".", "If", "there", "is", "no", "transport", "or", "we", "re", "not", "connected", "yet", "append", "to", "the", "output", "buffer", "else", "send", "immediately", "to", "the", "socket", ".", "This", "is", "called", "from"...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L473-L508
7,817
agoragames/haigha
haigha/connection.py
ConnectionChannel.dispatch
def dispatch(self, frame): ''' Override the default dispatch since we don't need the rest of the stack. ''' if frame.type() == HeartbeatFrame.type(): self.send_heartbeat() elif frame.type() == MethodFrame.type(): if frame.class_id == 10: cb = self._method_map.get(frame.method_id) if cb: method = self.clear_synchronous_cb(cb) method(frame) else: raise Channel.InvalidMethod( "unsupported method %d on channel %d", frame.method_id, self.channel_id) else: raise Channel.InvalidClass( "class %d is not supported on channel %d", frame.class_id, self.channel_id) else: raise Frame.InvalidFrameType( "frame type %d is not supported on channel %d", frame.type(), self.channel_id)
python
def dispatch(self, frame): ''' Override the default dispatch since we don't need the rest of the stack. ''' if frame.type() == HeartbeatFrame.type(): self.send_heartbeat() elif frame.type() == MethodFrame.type(): if frame.class_id == 10: cb = self._method_map.get(frame.method_id) if cb: method = self.clear_synchronous_cb(cb) method(frame) else: raise Channel.InvalidMethod( "unsupported method %d on channel %d", frame.method_id, self.channel_id) else: raise Channel.InvalidClass( "class %d is not supported on channel %d", frame.class_id, self.channel_id) else: raise Frame.InvalidFrameType( "frame type %d is not supported on channel %d", frame.type(), self.channel_id)
[ "def", "dispatch", "(", "self", ",", "frame", ")", ":", "if", "frame", ".", "type", "(", ")", "==", "HeartbeatFrame", ".", "type", "(", ")", ":", "self", ".", "send_heartbeat", "(", ")", "elif", "frame", ".", "type", "(", ")", "==", "MethodFrame", ...
Override the default dispatch since we don't need the rest of the stack.
[ "Override", "the", "default", "dispatch", "since", "we", "don", "t", "need", "the", "rest", "of", "the", "stack", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L534-L560
7,818
agoragames/haigha
haigha/connection.py
ConnectionChannel.send_heartbeat
def send_heartbeat(self): ''' Send a heartbeat if needed. Tracks last heartbeat send time. ''' # Note that this does not take into account the time that we last # sent a frame. Hearbeats are so small the effect should be quite # limited. Also note that we're looking for something near to our # scheduled interval, because if this is exact, then we'll likely # actually send a heartbeat at twice the period, which could cause # a broker to kill the connection if the period is large enough. The # 90% bound is arbitrary but seems a sensible enough default. if self.connection._heartbeat: if time.time() >= (self._last_heartbeat_send + 0.9 * self.connection._heartbeat): self.send_frame(HeartbeatFrame(self.channel_id)) self._last_heartbeat_send = time.time()
python
def send_heartbeat(self): ''' Send a heartbeat if needed. Tracks last heartbeat send time. ''' # Note that this does not take into account the time that we last # sent a frame. Hearbeats are so small the effect should be quite # limited. Also note that we're looking for something near to our # scheduled interval, because if this is exact, then we'll likely # actually send a heartbeat at twice the period, which could cause # a broker to kill the connection if the period is large enough. The # 90% bound is arbitrary but seems a sensible enough default. if self.connection._heartbeat: if time.time() >= (self._last_heartbeat_send + 0.9 * self.connection._heartbeat): self.send_frame(HeartbeatFrame(self.channel_id)) self._last_heartbeat_send = time.time()
[ "def", "send_heartbeat", "(", "self", ")", ":", "# Note that this does not take into account the time that we last", "# sent a frame. Hearbeats are so small the effect should be quite", "# limited. Also note that we're looking for something near to our", "# scheduled interval, because if this is e...
Send a heartbeat if needed. Tracks last heartbeat send time.
[ "Send", "a", "heartbeat", "if", "needed", ".", "Tracks", "last", "heartbeat", "send", "time", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L568-L583
7,819
agoragames/haigha
haigha/connection.py
ConnectionChannel._send_start_ok
def _send_start_ok(self): '''Send the start_ok message.''' args = Writer() args.write_table(self.connection._properties) args.write_shortstr(self.connection._login_method) args.write_longstr(self.connection._login_response) args.write_shortstr(self.connection._locale) self.send_frame(MethodFrame(self.channel_id, 10, 11, args)) self.add_synchronous_cb(self._recv_tune)
python
def _send_start_ok(self): '''Send the start_ok message.''' args = Writer() args.write_table(self.connection._properties) args.write_shortstr(self.connection._login_method) args.write_longstr(self.connection._login_response) args.write_shortstr(self.connection._locale) self.send_frame(MethodFrame(self.channel_id, 10, 11, args)) self.add_synchronous_cb(self._recv_tune)
[ "def", "_send_start_ok", "(", "self", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_table", "(", "self", ".", "connection", ".", "_properties", ")", "args", ".", "write_shortstr", "(", "self", ".", "connection", ".", "_login_method", ")", ...
Send the start_ok message.
[ "Send", "the", "start_ok", "message", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connection.py#L589-L598
7,820
agoragames/haigha
haigha/classes/exchange_class.py
ExchangeClass._cleanup
def _cleanup(self): ''' Cleanup local data. ''' self._declare_cb = None self._delete_cb = None super(ExchangeClass, self)._cleanup()
python
def _cleanup(self): ''' Cleanup local data. ''' self._declare_cb = None self._delete_cb = None super(ExchangeClass, self)._cleanup()
[ "def", "_cleanup", "(", "self", ")", ":", "self", ".", "_declare_cb", "=", "None", "self", ".", "_delete_cb", "=", "None", "super", "(", "ExchangeClass", ",", "self", ")", ".", "_cleanup", "(", ")" ]
Cleanup local data.
[ "Cleanup", "local", "data", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/exchange_class.py#L34-L40
7,821
agoragames/haigha
haigha/classes/exchange_class.py
ExchangeClass.delete
def delete(self, exchange, if_unused=False, nowait=True, ticket=None, cb=None): ''' Delete an exchange. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(exchange).\ write_bits(if_unused, nowait) self.send_frame(MethodFrame(self.channel_id, 40, 20, args)) if not nowait: self._delete_cb.append(cb) self.channel.add_synchronous_cb(self._recv_delete_ok)
python
def delete(self, exchange, if_unused=False, nowait=True, ticket=None, cb=None): ''' Delete an exchange. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(exchange).\ write_bits(if_unused, nowait) self.send_frame(MethodFrame(self.channel_id, 40, 20, args)) if not nowait: self._delete_cb.append(cb) self.channel.add_synchronous_cb(self._recv_delete_ok)
[ "def", "delete", "(", "self", ",", "exchange", ",", "if_unused", "=", "False", ",", "nowait", "=", "True", ",", "ticket", "=", "None", ",", "cb", "=", "None", ")", ":", "nowait", "=", "nowait", "and", "self", ".", "allow_nowait", "(", ")", "and", "...
Delete an exchange.
[ "Delete", "an", "exchange", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/exchange_class.py#L72-L87
7,822
agoragames/haigha
haigha/transports/socket_transport.py
SocketTransport.connect
def connect(self, (host, port), klass=socket.socket): '''Connect assuming a host and port tuple. :param tuple: A tuple containing host and port for a connection. :param klass: A implementation of socket.socket. :raises socket.gaierror: If no address can be resolved. :raises socket.error: If no connection can be made. ''' self._host = "%s:%s" % (host, port) for info in socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP): family, socktype, proto, _, sockaddr = info self._sock = klass(family, socktype, proto) self._sock.settimeout(self.connection._connect_timeout) if self.connection._sock_opts: _sock_opts = self.connection._sock_opts for (level, optname), value in _sock_opts.iteritems(): self._sock.setsockopt(level, optname, value) try: self._sock.connect(sockaddr) except socket.error: self.connection.logger.exception( "Failed to connect to %s:", sockaddr, ) continue # After connecting, switch to full-blocking mode. self._sock.settimeout(None) break else: raise
python
def connect(self, (host, port), klass=socket.socket): '''Connect assuming a host and port tuple. :param tuple: A tuple containing host and port for a connection. :param klass: A implementation of socket.socket. :raises socket.gaierror: If no address can be resolved. :raises socket.error: If no connection can be made. ''' self._host = "%s:%s" % (host, port) for info in socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP): family, socktype, proto, _, sockaddr = info self._sock = klass(family, socktype, proto) self._sock.settimeout(self.connection._connect_timeout) if self.connection._sock_opts: _sock_opts = self.connection._sock_opts for (level, optname), value in _sock_opts.iteritems(): self._sock.setsockopt(level, optname, value) try: self._sock.connect(sockaddr) except socket.error: self.connection.logger.exception( "Failed to connect to %s:", sockaddr, ) continue # After connecting, switch to full-blocking mode. self._sock.settimeout(None) break else: raise
[ "def", "connect", "(", "self", ",", "(", "host", ",", "port", ")", ",", "klass", "=", "socket", ".", "socket", ")", ":", "self", ".", "_host", "=", "\"%s:%s\"", "%", "(", "host", ",", "port", ")", "for", "info", "in", "socket", ".", "getaddrinfo", ...
Connect assuming a host and port tuple. :param tuple: A tuple containing host and port for a connection. :param klass: A implementation of socket.socket. :raises socket.gaierror: If no address can be resolved. :raises socket.error: If no connection can be made.
[ "Connect", "assuming", "a", "host", "and", "port", "tuple", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/transports/socket_transport.py#L27-L64
7,823
agoragames/haigha
haigha/transports/socket_transport.py
SocketTransport.read
def read(self, timeout=None): ''' Read from the transport. If timeout>0, will only block for `timeout` seconds. ''' e = None if not hasattr(self, '_sock'): return None try: # Note that we ignore both None and 0, i.e. we either block with a # timeout or block completely and let gevent sort it out. if timeout: self._sock.settimeout(timeout) else: self._sock.settimeout(None) data = self._sock.recv( self._sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)) if len(data): if self.connection.debug > 1: self.connection.logger.debug( 'read %d bytes from %s' % (len(data), self._host)) if len(self._buffer): self._buffer.extend(data) data = self._buffer self._buffer = bytearray() return data # Note that no data means the socket is closed and we'll mark that # below except socket.timeout as e: # Note that this is implemented differently and though it would be # caught as an EnvironmentError, it has no errno. Not sure whose # fault that is. return None except EnvironmentError as e: # thrown if we have a timeout and no data if e.errno in (errno.EAGAIN, errno.EWOULDBLOCK, errno.EINTR): return None self.connection.logger.exception( 'error reading from %s' % (self._host)) self.connection.transport_closed( msg='error reading from %s' % (self._host)) if e: raise
python
def read(self, timeout=None): ''' Read from the transport. If timeout>0, will only block for `timeout` seconds. ''' e = None if not hasattr(self, '_sock'): return None try: # Note that we ignore both None and 0, i.e. we either block with a # timeout or block completely and let gevent sort it out. if timeout: self._sock.settimeout(timeout) else: self._sock.settimeout(None) data = self._sock.recv( self._sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)) if len(data): if self.connection.debug > 1: self.connection.logger.debug( 'read %d bytes from %s' % (len(data), self._host)) if len(self._buffer): self._buffer.extend(data) data = self._buffer self._buffer = bytearray() return data # Note that no data means the socket is closed and we'll mark that # below except socket.timeout as e: # Note that this is implemented differently and though it would be # caught as an EnvironmentError, it has no errno. Not sure whose # fault that is. return None except EnvironmentError as e: # thrown if we have a timeout and no data if e.errno in (errno.EAGAIN, errno.EWOULDBLOCK, errno.EINTR): return None self.connection.logger.exception( 'error reading from %s' % (self._host)) self.connection.transport_closed( msg='error reading from %s' % (self._host)) if e: raise
[ "def", "read", "(", "self", ",", "timeout", "=", "None", ")", ":", "e", "=", "None", "if", "not", "hasattr", "(", "self", ",", "'_sock'", ")", ":", "return", "None", "try", ":", "# Note that we ignore both None and 0, i.e. we either block with a", "# timeout or ...
Read from the transport. If timeout>0, will only block for `timeout` seconds.
[ "Read", "from", "the", "transport", ".", "If", "timeout", ">", "0", "will", "only", "block", "for", "timeout", "seconds", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/transports/socket_transport.py#L66-L115
7,824
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.set_return_listener
def set_return_listener(self, cb): ''' Set a callback for basic.return listening. Will be called with a single Message argument. The return_info attribute of the Message will have the following properties: 'channel': Channel instance 'reply_code': reply code (int) 'reply_text': reply text 'exchange': exchange name 'routing_key': routing key RabbitMQ NOTE: if the channel was in confirmation mode when the message was published, then basic.return will still be followed by basic.ack later. :param cb: callable cb(Message); pass None to reset ''' if cb is not None and not callable(cb): raise ValueError('return_listener callback must either be None or ' 'a callable, but got: %r' % (cb,)) self._return_listener = cb
python
def set_return_listener(self, cb): ''' Set a callback for basic.return listening. Will be called with a single Message argument. The return_info attribute of the Message will have the following properties: 'channel': Channel instance 'reply_code': reply code (int) 'reply_text': reply text 'exchange': exchange name 'routing_key': routing key RabbitMQ NOTE: if the channel was in confirmation mode when the message was published, then basic.return will still be followed by basic.ack later. :param cb: callable cb(Message); pass None to reset ''' if cb is not None and not callable(cb): raise ValueError('return_listener callback must either be None or ' 'a callable, but got: %r' % (cb,)) self._return_listener = cb
[ "def", "set_return_listener", "(", "self", ",", "cb", ")", ":", "if", "cb", "is", "not", "None", "and", "not", "callable", "(", "cb", ")", ":", "raise", "ValueError", "(", "'return_listener callback must either be None or '", "'a callable, but got: %r'", "%", "(",...
Set a callback for basic.return listening. Will be called with a single Message argument. The return_info attribute of the Message will have the following properties: 'channel': Channel instance 'reply_code': reply code (int) 'reply_text': reply text 'exchange': exchange name 'routing_key': routing key RabbitMQ NOTE: if the channel was in confirmation mode when the message was published, then basic.return will still be followed by basic.ack later. :param cb: callable cb(Message); pass None to reset
[ "Set", "a", "callback", "for", "basic", ".", "return", "listening", ".", "Will", "be", "called", "with", "a", "single", "Message", "argument", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L60-L82
7,825
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.qos
def qos(self, prefetch_size=0, prefetch_count=0, is_global=False): ''' Set QoS on this channel. ''' args = Writer() args.write_long(prefetch_size).\ write_short(prefetch_count).\ write_bit(is_global) self.send_frame(MethodFrame(self.channel_id, 60, 10, args)) self.channel.add_synchronous_cb(self._recv_qos_ok)
python
def qos(self, prefetch_size=0, prefetch_count=0, is_global=False): ''' Set QoS on this channel. ''' args = Writer() args.write_long(prefetch_size).\ write_short(prefetch_count).\ write_bit(is_global) self.send_frame(MethodFrame(self.channel_id, 60, 10, args)) self.channel.add_synchronous_cb(self._recv_qos_ok)
[ "def", "qos", "(", "self", ",", "prefetch_size", "=", "0", ",", "prefetch_count", "=", "0", ",", "is_global", "=", "False", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_long", "(", "prefetch_size", ")", ".", "write_short", "(", "prefe...
Set QoS on this channel.
[ "Set", "QoS", "on", "this", "channel", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L96-L106
7,826
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.consume
def consume(self, queue, consumer, consumer_tag='', no_local=False, no_ack=True, exclusive=False, nowait=True, ticket=None, cb=None): ''' Start a queue consumer. If `cb` is supplied, will be called when broker confirms that consumer is registered. ''' nowait = nowait and self.allow_nowait() and not cb if nowait and consumer_tag == '': consumer_tag = self._generate_consumer_tag() args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_shortstr(consumer_tag).\ write_bits(no_local, no_ack, exclusive, nowait).\ write_table({}) # unused according to spec self.send_frame(MethodFrame(self.channel_id, 60, 20, args)) if not nowait: self._pending_consumers.append((consumer, cb)) self.channel.add_synchronous_cb(self._recv_consume_ok) else: self._consumer_cb[consumer_tag] = consumer
python
def consume(self, queue, consumer, consumer_tag='', no_local=False, no_ack=True, exclusive=False, nowait=True, ticket=None, cb=None): ''' Start a queue consumer. If `cb` is supplied, will be called when broker confirms that consumer is registered. ''' nowait = nowait and self.allow_nowait() and not cb if nowait and consumer_tag == '': consumer_tag = self._generate_consumer_tag() args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_shortstr(consumer_tag).\ write_bits(no_local, no_ack, exclusive, nowait).\ write_table({}) # unused according to spec self.send_frame(MethodFrame(self.channel_id, 60, 20, args)) if not nowait: self._pending_consumers.append((consumer, cb)) self.channel.add_synchronous_cb(self._recv_consume_ok) else: self._consumer_cb[consumer_tag] = consumer
[ "def", "consume", "(", "self", ",", "queue", ",", "consumer", ",", "consumer_tag", "=", "''", ",", "no_local", "=", "False", ",", "no_ack", "=", "True", ",", "exclusive", "=", "False", ",", "nowait", "=", "True", ",", "ticket", "=", "None", ",", "cb"...
Start a queue consumer. If `cb` is supplied, will be called when broker confirms that consumer is registered.
[ "Start", "a", "queue", "consumer", ".", "If", "cb", "is", "supplied", "will", "be", "called", "when", "broker", "confirms", "that", "consumer", "is", "registered", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L112-L136
7,827
agoragames/haigha
haigha/classes/basic_class.py
BasicClass._lookup_consumer_tag_by_consumer
def _lookup_consumer_tag_by_consumer(self, consumer): '''Look up consumer tag given its consumer function NOTE: this protected method may be called by derived classes :param callable consumer: consumer function :returns: matching consumer tag or None :rtype: str or None ''' for (tag, func) in self._consumer_cb.iteritems(): if func == consumer: return tag
python
def _lookup_consumer_tag_by_consumer(self, consumer): '''Look up consumer tag given its consumer function NOTE: this protected method may be called by derived classes :param callable consumer: consumer function :returns: matching consumer tag or None :rtype: str or None ''' for (tag, func) in self._consumer_cb.iteritems(): if func == consumer: return tag
[ "def", "_lookup_consumer_tag_by_consumer", "(", "self", ",", "consumer", ")", ":", "for", "(", "tag", ",", "func", ")", "in", "self", ".", "_consumer_cb", ".", "iteritems", "(", ")", ":", "if", "func", "==", "consumer", ":", "return", "tag" ]
Look up consumer tag given its consumer function NOTE: this protected method may be called by derived classes :param callable consumer: consumer function :returns: matching consumer tag or None :rtype: str or None
[ "Look", "up", "consumer", "tag", "given", "its", "consumer", "function" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L179-L191
7,828
agoragames/haigha
haigha/classes/basic_class.py
BasicClass._purge_consumer_by_tag
def _purge_consumer_by_tag(self, consumer_tag): '''Purge consumer entry from this basic instance NOTE: this protected method may be called by derived classes :param str consumer_tag: ''' try: del self._consumer_cb[consumer_tag] except KeyError: self.logger.warning( 'no callback registered for consumer tag " %s "', consumer_tag) else: self.logger.info('purged consumer with tag " %s "', consumer_tag)
python
def _purge_consumer_by_tag(self, consumer_tag): '''Purge consumer entry from this basic instance NOTE: this protected method may be called by derived classes :param str consumer_tag: ''' try: del self._consumer_cb[consumer_tag] except KeyError: self.logger.warning( 'no callback registered for consumer tag " %s "', consumer_tag) else: self.logger.info('purged consumer with tag " %s "', consumer_tag)
[ "def", "_purge_consumer_by_tag", "(", "self", ",", "consumer_tag", ")", ":", "try", ":", "del", "self", ".", "_consumer_cb", "[", "consumer_tag", "]", "except", "KeyError", ":", "self", ".", "logger", ".", "warning", "(", "'no callback registered for consumer tag ...
Purge consumer entry from this basic instance NOTE: this protected method may be called by derived classes :param str consumer_tag:
[ "Purge", "consumer", "entry", "from", "this", "basic", "instance" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L193-L206
7,829
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.publish
def publish(self, msg, exchange, routing_key, mandatory=False, immediate=False, ticket=None): ''' publish a message. ''' args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(exchange).\ write_shortstr(routing_key).\ write_bits(mandatory, immediate) self.send_frame(MethodFrame(self.channel_id, 60, 40, args)) self.send_frame( HeaderFrame(self.channel_id, 60, 0, len(msg), msg.properties)) f_max = self.channel.connection.frame_max for f in ContentFrame.create_frames(self.channel_id, msg.body, f_max): self.send_frame(f)
python
def publish(self, msg, exchange, routing_key, mandatory=False, immediate=False, ticket=None): ''' publish a message. ''' args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(exchange).\ write_shortstr(routing_key).\ write_bits(mandatory, immediate) self.send_frame(MethodFrame(self.channel_id, 60, 40, args)) self.send_frame( HeaderFrame(self.channel_id, 60, 0, len(msg), msg.properties)) f_max = self.channel.connection.frame_max for f in ContentFrame.create_frames(self.channel_id, msg.body, f_max): self.send_frame(f)
[ "def", "publish", "(", "self", ",", "msg", ",", "exchange", ",", "routing_key", ",", "mandatory", "=", "False", ",", "immediate", "=", "False", ",", "ticket", "=", "None", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_short", "(", "t...
publish a message.
[ "publish", "a", "message", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L208-L225
7,830
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.return_msg
def return_msg(self, reply_code, reply_text, exchange, routing_key): ''' Return a failed message. Not named "return" because python interpreter can't deal with that. ''' args = Writer() args.write_short(reply_code).\ write_shortstr(reply_text).\ write_shortstr(exchange).\ write_shortstr(routing_key) self.send_frame(MethodFrame(self.channel_id, 60, 50, args))
python
def return_msg(self, reply_code, reply_text, exchange, routing_key): ''' Return a failed message. Not named "return" because python interpreter can't deal with that. ''' args = Writer() args.write_short(reply_code).\ write_shortstr(reply_text).\ write_shortstr(exchange).\ write_shortstr(routing_key) self.send_frame(MethodFrame(self.channel_id, 60, 50, args))
[ "def", "return_msg", "(", "self", ",", "reply_code", ",", "reply_text", ",", "exchange", ",", "routing_key", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_short", "(", "reply_code", ")", ".", "write_shortstr", "(", "reply_text", ")", ".", ...
Return a failed message. Not named "return" because python interpreter can't deal with that.
[ "Return", "a", "failed", "message", ".", "Not", "named", "return", "because", "python", "interpreter", "can", "t", "deal", "with", "that", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L227-L238
7,831
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.get
def get(self, queue, consumer=None, no_ack=True, ticket=None): ''' Ask to fetch a single message from a queue. If a consumer is supplied, the consumer will be called with either a Message argument, or None if there is no message in queue. If a synchronous transport, Message or None is returned. ''' args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_bit(no_ack) self._get_cb.append(consumer) self.send_frame(MethodFrame(self.channel_id, 60, 70, args)) return self.channel.add_synchronous_cb(self._recv_get_response)
python
def get(self, queue, consumer=None, no_ack=True, ticket=None): ''' Ask to fetch a single message from a queue. If a consumer is supplied, the consumer will be called with either a Message argument, or None if there is no message in queue. If a synchronous transport, Message or None is returned. ''' args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_bit(no_ack) self._get_cb.append(consumer) self.send_frame(MethodFrame(self.channel_id, 60, 70, args)) return self.channel.add_synchronous_cb(self._recv_get_response)
[ "def", "get", "(", "self", ",", "queue", ",", "consumer", "=", "None", ",", "no_ack", "=", "True", ",", "ticket", "=", "None", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_short", "(", "ticket", "or", "self", ".", "default_ticket", ...
Ask to fetch a single message from a queue. If a consumer is supplied, the consumer will be called with either a Message argument, or None if there is no message in queue. If a synchronous transport, Message or None is returned.
[ "Ask", "to", "fetch", "a", "single", "message", "from", "a", "queue", ".", "If", "a", "consumer", "is", "supplied", "the", "consumer", "will", "be", "called", "with", "either", "a", "Message", "argument", "or", "None", "if", "there", "is", "no", "message...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L266-L280
7,832
agoragames/haigha
haigha/classes/basic_class.py
BasicClass._recv_get_response
def _recv_get_response(self, method_frame): ''' Handle either get_ok or get_empty. This is a hack because the synchronous callback stack is expecting one method to satisfy the expectation. To keep that loop as tight as possible, work within those constraints. Use of get is not recommended anyway. ''' if method_frame.method_id == 71: return self._recv_get_ok(method_frame) elif method_frame.method_id == 72: return self._recv_get_empty(method_frame)
python
def _recv_get_response(self, method_frame): ''' Handle either get_ok or get_empty. This is a hack because the synchronous callback stack is expecting one method to satisfy the expectation. To keep that loop as tight as possible, work within those constraints. Use of get is not recommended anyway. ''' if method_frame.method_id == 71: return self._recv_get_ok(method_frame) elif method_frame.method_id == 72: return self._recv_get_empty(method_frame)
[ "def", "_recv_get_response", "(", "self", ",", "method_frame", ")", ":", "if", "method_frame", ".", "method_id", "==", "71", ":", "return", "self", ".", "_recv_get_ok", "(", "method_frame", ")", "elif", "method_frame", ".", "method_id", "==", "72", ":", "ret...
Handle either get_ok or get_empty. This is a hack because the synchronous callback stack is expecting one method to satisfy the expectation. To keep that loop as tight as possible, work within those constraints. Use of get is not recommended anyway.
[ "Handle", "either", "get_ok", "or", "get_empty", ".", "This", "is", "a", "hack", "because", "the", "synchronous", "callback", "stack", "is", "expecting", "one", "method", "to", "satisfy", "the", "expectation", ".", "To", "keep", "that", "loop", "as", "tight"...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L282-L292
7,833
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.ack
def ack(self, delivery_tag, multiple=False): ''' Acknowledge delivery of a message. If multiple=True, acknowledge up-to and including delivery_tag. ''' args = Writer() args.write_longlong(delivery_tag).\ write_bit(multiple) self.send_frame(MethodFrame(self.channel_id, 60, 80, args))
python
def ack(self, delivery_tag, multiple=False): ''' Acknowledge delivery of a message. If multiple=True, acknowledge up-to and including delivery_tag. ''' args = Writer() args.write_longlong(delivery_tag).\ write_bit(multiple) self.send_frame(MethodFrame(self.channel_id, 60, 80, args))
[ "def", "ack", "(", "self", ",", "delivery_tag", ",", "multiple", "=", "False", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_longlong", "(", "delivery_tag", ")", ".", "write_bit", "(", "multiple", ")", "self", ".", "send_frame", "(", "...
Acknowledge delivery of a message. If multiple=True, acknowledge up-to and including delivery_tag.
[ "Acknowledge", "delivery", "of", "a", "message", ".", "If", "multiple", "=", "True", "acknowledge", "up", "-", "to", "and", "including", "delivery_tag", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L309-L318
7,834
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.reject
def reject(self, delivery_tag, requeue=False): ''' Reject a message. ''' args = Writer() args.write_longlong(delivery_tag).\ write_bit(requeue) self.send_frame(MethodFrame(self.channel_id, 60, 90, args))
python
def reject(self, delivery_tag, requeue=False): ''' Reject a message. ''' args = Writer() args.write_longlong(delivery_tag).\ write_bit(requeue) self.send_frame(MethodFrame(self.channel_id, 60, 90, args))
[ "def", "reject", "(", "self", ",", "delivery_tag", ",", "requeue", "=", "False", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_longlong", "(", "delivery_tag", ")", ".", "write_bit", "(", "requeue", ")", "self", ".", "send_frame", "(", ...
Reject a message.
[ "Reject", "a", "message", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L320-L328
7,835
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.recover_async
def recover_async(self, requeue=False): ''' Redeliver all unacknowledged messages on this channel. This method is deprecated in favour of the synchronous recover/recover-ok ''' args = Writer() args.write_bit(requeue) self.send_frame(MethodFrame(self.channel_id, 60, 100, args))
python
def recover_async(self, requeue=False): ''' Redeliver all unacknowledged messages on this channel. This method is deprecated in favour of the synchronous recover/recover-ok ''' args = Writer() args.write_bit(requeue) self.send_frame(MethodFrame(self.channel_id, 60, 100, args))
[ "def", "recover_async", "(", "self", ",", "requeue", "=", "False", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_bit", "(", "requeue", ")", "self", ".", "send_frame", "(", "MethodFrame", "(", "self", ".", "channel_id", ",", "60", ",", ...
Redeliver all unacknowledged messages on this channel. This method is deprecated in favour of the synchronous recover/recover-ok
[ "Redeliver", "all", "unacknowledged", "messages", "on", "this", "channel", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L330-L340
7,836
agoragames/haigha
haigha/classes/basic_class.py
BasicClass.recover
def recover(self, requeue=False, cb=None): ''' Ask server to redeliver all unacknowledged messages. ''' args = Writer() args.write_bit(requeue) # The XML spec is incorrect; this method is always synchronous # http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2011-January/010738.html self._recover_cb.append(cb) self.send_frame(MethodFrame(self.channel_id, 60, 110, args)) self.channel.add_synchronous_cb(self._recv_recover_ok)
python
def recover(self, requeue=False, cb=None): ''' Ask server to redeliver all unacknowledged messages. ''' args = Writer() args.write_bit(requeue) # The XML spec is incorrect; this method is always synchronous # http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2011-January/010738.html self._recover_cb.append(cb) self.send_frame(MethodFrame(self.channel_id, 60, 110, args)) self.channel.add_synchronous_cb(self._recv_recover_ok)
[ "def", "recover", "(", "self", ",", "requeue", "=", "False", ",", "cb", "=", "None", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_bit", "(", "requeue", ")", "# The XML spec is incorrect; this method is always synchronous", "# http://lists.rabbit...
Ask server to redeliver all unacknowledged messages.
[ "Ask", "server", "to", "redeliver", "all", "unacknowledged", "messages", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L342-L353
7,837
agoragames/haigha
haigha/classes/basic_class.py
BasicClass._read_msg
def _read_msg(self, method_frame, with_consumer_tag=False, with_message_count=False): ''' Support method to read a Message from the current frame buffer. Will return a Message, or re-queue current frames and raise a FrameUnderflow. Takes an optional argument on whether to read the consumer tag so it can be used for both deliver and get-ok. ''' header_frame, body = self._reap_msg_frames(method_frame) if with_consumer_tag: consumer_tag = method_frame.args.read_shortstr() delivery_tag = method_frame.args.read_longlong() redelivered = method_frame.args.read_bit() exchange = method_frame.args.read_shortstr() routing_key = method_frame.args.read_shortstr() if with_message_count: message_count = method_frame.args.read_long() delivery_info = { 'channel': self.channel, 'delivery_tag': delivery_tag, 'redelivered': redelivered, 'exchange': exchange, 'routing_key': routing_key, } if with_consumer_tag: delivery_info['consumer_tag'] = consumer_tag if with_message_count: delivery_info['message_count'] = message_count return Message(body=body, delivery_info=delivery_info, **header_frame.properties)
python
def _read_msg(self, method_frame, with_consumer_tag=False, with_message_count=False): ''' Support method to read a Message from the current frame buffer. Will return a Message, or re-queue current frames and raise a FrameUnderflow. Takes an optional argument on whether to read the consumer tag so it can be used for both deliver and get-ok. ''' header_frame, body = self._reap_msg_frames(method_frame) if with_consumer_tag: consumer_tag = method_frame.args.read_shortstr() delivery_tag = method_frame.args.read_longlong() redelivered = method_frame.args.read_bit() exchange = method_frame.args.read_shortstr() routing_key = method_frame.args.read_shortstr() if with_message_count: message_count = method_frame.args.read_long() delivery_info = { 'channel': self.channel, 'delivery_tag': delivery_tag, 'redelivered': redelivered, 'exchange': exchange, 'routing_key': routing_key, } if with_consumer_tag: delivery_info['consumer_tag'] = consumer_tag if with_message_count: delivery_info['message_count'] = message_count return Message(body=body, delivery_info=delivery_info, **header_frame.properties)
[ "def", "_read_msg", "(", "self", ",", "method_frame", ",", "with_consumer_tag", "=", "False", ",", "with_message_count", "=", "False", ")", ":", "header_frame", ",", "body", "=", "self", ".", "_reap_msg_frames", "(", "method_frame", ")", "if", "with_consumer_tag...
Support method to read a Message from the current frame buffer. Will return a Message, or re-queue current frames and raise a FrameUnderflow. Takes an optional argument on whether to read the consumer tag so it can be used for both deliver and get-ok.
[ "Support", "method", "to", "read", "a", "Message", "from", "the", "current", "frame", "buffer", ".", "Will", "return", "a", "Message", "or", "re", "-", "queue", "current", "frames", "and", "raise", "a", "FrameUnderflow", ".", "Takes", "an", "optional", "ar...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/basic_class.py#L360-L392
7,838
agoragames/haigha
haigha/channel_pool.py
ChannelPool.publish
def publish(self, *args, **kwargs): ''' Publish a message. Caller can supply an optional callback which will be fired when the transaction is committed. Tries very hard to avoid closed and inactive channels, but a ChannelError or ConnectionError may still be raised. ''' user_cb = kwargs.pop('cb', None) # If the first channel we grab is inactive, continue fetching until # we get an active channel, then put the inactive channels back in # the pool. Try to keep the overhead to a minimum. channel = self._get_channel() if channel and not channel.active: inactive_channels = set() while channel and not channel.active: inactive_channels.add(channel) channel = self._get_channel() self._free_channels.update(inactive_channels) # When the transaction is committed, add the channel back to the pool # and call any user-defined callbacks. If there is anything in queue, # pop it and call back to publish(). Only do so if the channel is # still active though, because otherwise the message will end up at # the back of the queue, breaking the original order. def committed(): self._free_channels.add(channel) if channel.active and not channel.closed: self._process_queue() if user_cb is not None: user_cb() if channel: channel.publish_synchronous(*args, cb=committed, **kwargs) else: kwargs['cb'] = user_cb self._queue.append((args, kwargs))
python
def publish(self, *args, **kwargs): ''' Publish a message. Caller can supply an optional callback which will be fired when the transaction is committed. Tries very hard to avoid closed and inactive channels, but a ChannelError or ConnectionError may still be raised. ''' user_cb = kwargs.pop('cb', None) # If the first channel we grab is inactive, continue fetching until # we get an active channel, then put the inactive channels back in # the pool. Try to keep the overhead to a minimum. channel = self._get_channel() if channel and not channel.active: inactive_channels = set() while channel and not channel.active: inactive_channels.add(channel) channel = self._get_channel() self._free_channels.update(inactive_channels) # When the transaction is committed, add the channel back to the pool # and call any user-defined callbacks. If there is anything in queue, # pop it and call back to publish(). Only do so if the channel is # still active though, because otherwise the message will end up at # the back of the queue, breaking the original order. def committed(): self._free_channels.add(channel) if channel.active and not channel.closed: self._process_queue() if user_cb is not None: user_cb() if channel: channel.publish_synchronous(*args, cb=committed, **kwargs) else: kwargs['cb'] = user_cb self._queue.append((args, kwargs))
[ "def", "publish", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "user_cb", "=", "kwargs", ".", "pop", "(", "'cb'", ",", "None", ")", "# If the first channel we grab is inactive, continue fetching until", "# we get an active channel, then put the in...
Publish a message. Caller can supply an optional callback which will be fired when the transaction is committed. Tries very hard to avoid closed and inactive channels, but a ChannelError or ConnectionError may still be raised.
[ "Publish", "a", "message", ".", "Caller", "can", "supply", "an", "optional", "callback", "which", "will", "be", "fired", "when", "the", "transaction", "is", "committed", ".", "Tries", "very", "hard", "to", "avoid", "closed", "and", "inactive", "channels", "b...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel_pool.py#L35-L72
7,839
agoragames/haigha
haigha/channel_pool.py
ChannelPool._process_queue
def _process_queue(self): ''' If there are any message in the queue, process one of them. ''' if len(self._queue): args, kwargs = self._queue.popleft() self.publish(*args, **kwargs)
python
def _process_queue(self): ''' If there are any message in the queue, process one of them. ''' if len(self._queue): args, kwargs = self._queue.popleft() self.publish(*args, **kwargs)
[ "def", "_process_queue", "(", "self", ")", ":", "if", "len", "(", "self", ".", "_queue", ")", ":", "args", ",", "kwargs", "=", "self", ".", "_queue", ".", "popleft", "(", ")", "self", ".", "publish", "(", "*", "args", ",", "*", "*", "kwargs", ")"...
If there are any message in the queue, process one of them.
[ "If", "there", "are", "any", "message", "in", "the", "queue", "process", "one", "of", "them", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel_pool.py#L74-L80
7,840
agoragames/haigha
haigha/channel_pool.py
ChannelPool._get_channel
def _get_channel(self): ''' Fetch a channel from the pool. Will return a new one if necessary. If a channel in the free pool is closed, will remove it. Will return None if we hit the cap. Will clean up any channels that were published to but closed due to error. ''' while len(self._free_channels): rval = self._free_channels.pop() if not rval.closed: return rval # don't adjust _channels value because the callback will do that # and we don't want to double count it. if not self._size or self._channels < self._size: rval = self._connection.channel() self._channels += 1 rval.add_close_listener(self._channel_closed_cb) return rval
python
def _get_channel(self): ''' Fetch a channel from the pool. Will return a new one if necessary. If a channel in the free pool is closed, will remove it. Will return None if we hit the cap. Will clean up any channels that were published to but closed due to error. ''' while len(self._free_channels): rval = self._free_channels.pop() if not rval.closed: return rval # don't adjust _channels value because the callback will do that # and we don't want to double count it. if not self._size or self._channels < self._size: rval = self._connection.channel() self._channels += 1 rval.add_close_listener(self._channel_closed_cb) return rval
[ "def", "_get_channel", "(", "self", ")", ":", "while", "len", "(", "self", ".", "_free_channels", ")", ":", "rval", "=", "self", ".", "_free_channels", ".", "pop", "(", ")", "if", "not", "rval", ".", "closed", ":", "return", "rval", "# don't adjust _chan...
Fetch a channel from the pool. Will return a new one if necessary. If a channel in the free pool is closed, will remove it. Will return None if we hit the cap. Will clean up any channels that were published to but closed due to error.
[ "Fetch", "a", "channel", "from", "the", "pool", ".", "Will", "return", "a", "new", "one", "if", "necessary", ".", "If", "a", "channel", "in", "the", "free", "pool", "is", "closed", "will", "remove", "it", ".", "Will", "return", "None", "if", "we", "h...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel_pool.py#L82-L100
7,841
agoragames/haigha
haigha/frames/content_frame.py
ContentFrame.create_frames
def create_frames(self, channel_id, buf, frame_max): ''' A generator which will create frames from a buffer given a max frame size. ''' size = frame_max - 8 # 8 bytes overhead for frame header and footer offset = 0 while True: payload = buf[offset:(offset + size)] if len(payload) == 0: break offset += size yield ContentFrame(channel_id, payload) if offset >= len(buf): break
python
def create_frames(self, channel_id, buf, frame_max): ''' A generator which will create frames from a buffer given a max frame size. ''' size = frame_max - 8 # 8 bytes overhead for frame header and footer offset = 0 while True: payload = buf[offset:(offset + size)] if len(payload) == 0: break offset += size yield ContentFrame(channel_id, payload) if offset >= len(buf): break
[ "def", "create_frames", "(", "self", ",", "channel_id", ",", "buf", ",", "frame_max", ")", ":", "size", "=", "frame_max", "-", "8", "# 8 bytes overhead for frame header and footer", "offset", "=", "0", "while", "True", ":", "payload", "=", "buf", "[", "offset"...
A generator which will create frames from a buffer given a max frame size.
[ "A", "generator", "which", "will", "create", "frames", "from", "a", "buffer", "given", "a", "max", "frame", "size", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/frames/content_frame.py#L30-L45
7,842
agoragames/haigha
haigha/transports/event_transport.py
EventTransport.connect
def connect(self, (host, port)): ''' Connect assuming a host and port tuple. Implemented as non-blocking, and will close the transport if there's an error ''' self._host = "%s:%s" % (host, port) self._sock = EventSocket( read_cb=self._sock_read_cb, close_cb=self._sock_close_cb, error_cb=self._sock_error_cb, debug=self.connection.debug, logger=self.connection.logger) if self.connection._sock_opts: for k, v in self.connection._sock_opts.iteritems(): family, type = k self._sock.setsockopt(family, type, v) self._sock.setblocking(False) self._sock.connect( (host, port), timeout=self.connection._connect_timeout) self._heartbeat_timeout = None
python
def connect(self, (host, port)): ''' Connect assuming a host and port tuple. Implemented as non-blocking, and will close the transport if there's an error ''' self._host = "%s:%s" % (host, port) self._sock = EventSocket( read_cb=self._sock_read_cb, close_cb=self._sock_close_cb, error_cb=self._sock_error_cb, debug=self.connection.debug, logger=self.connection.logger) if self.connection._sock_opts: for k, v in self.connection._sock_opts.iteritems(): family, type = k self._sock.setsockopt(family, type, v) self._sock.setblocking(False) self._sock.connect( (host, port), timeout=self.connection._connect_timeout) self._heartbeat_timeout = None
[ "def", "connect", "(", "self", ",", "(", "host", ",", "port", ")", ")", ":", "self", ".", "_host", "=", "\"%s:%s\"", "%", "(", "host", ",", "port", ")", "self", ".", "_sock", "=", "EventSocket", "(", "read_cb", "=", "self", ".", "_sock_read_cb", ",...
Connect assuming a host and port tuple. Implemented as non-blocking, and will close the transport if there's an error
[ "Connect", "assuming", "a", "host", "and", "port", "tuple", ".", "Implemented", "as", "non", "-", "blocking", "and", "will", "close", "the", "transport", "if", "there", "s", "an", "error" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/transports/event_transport.py#L49-L68
7,843
agoragames/haigha
haigha/transports/event_transport.py
EventTransport.read
def read(self, timeout=None): ''' Read from the transport. If no data is available, should return None. The timeout is ignored as this returns only data that has already been buffered locally. ''' # NOTE: copying over this comment from Connection, because there is # knowledge captured here, even if the details are stale # Because of the timer callback to dataRead when we re-buffered, # there's a chance that in between we've lost the socket. If that's # the case, just silently return as some code elsewhere would have # already notified us. That bug could be fixed by improving the # message reading so that we consume all possible messages and ensure # that only a partial message was rebuffered, so that we can rely on # the next read event to read the subsequent message. if not hasattr(self, '_sock'): return None # This is sort of a hack because we're faking that data is ready, but # it works for purposes of supporting timeouts if timeout: if self._heartbeat_timeout: self._heartbeat_timeout.delete() self._heartbeat_timeout = \ event.timeout(timeout, self._sock_read_cb, self._sock) elif self._heartbeat_timeout: self._heartbeat_timeout.delete() self._heartbeat_timeout = None return self._sock.read()
python
def read(self, timeout=None): ''' Read from the transport. If no data is available, should return None. The timeout is ignored as this returns only data that has already been buffered locally. ''' # NOTE: copying over this comment from Connection, because there is # knowledge captured here, even if the details are stale # Because of the timer callback to dataRead when we re-buffered, # there's a chance that in between we've lost the socket. If that's # the case, just silently return as some code elsewhere would have # already notified us. That bug could be fixed by improving the # message reading so that we consume all possible messages and ensure # that only a partial message was rebuffered, so that we can rely on # the next read event to read the subsequent message. if not hasattr(self, '_sock'): return None # This is sort of a hack because we're faking that data is ready, but # it works for purposes of supporting timeouts if timeout: if self._heartbeat_timeout: self._heartbeat_timeout.delete() self._heartbeat_timeout = \ event.timeout(timeout, self._sock_read_cb, self._sock) elif self._heartbeat_timeout: self._heartbeat_timeout.delete() self._heartbeat_timeout = None return self._sock.read()
[ "def", "read", "(", "self", ",", "timeout", "=", "None", ")", ":", "# NOTE: copying over this comment from Connection, because there is", "# knowledge captured here, even if the details are stale", "# Because of the timer callback to dataRead when we re-buffered,", "# there's a chance that...
Read from the transport. If no data is available, should return None. The timeout is ignored as this returns only data that has already been buffered locally.
[ "Read", "from", "the", "transport", ".", "If", "no", "data", "is", "available", "should", "return", "None", ".", "The", "timeout", "is", "ignored", "as", "this", "returns", "only", "data", "that", "has", "already", "been", "buffered", "locally", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/transports/event_transport.py#L70-L99
7,844
agoragames/haigha
haigha/frames/header_frame.py
HeaderFrame.parse
def parse(self, channel_id, payload): ''' Parse a header frame for a channel given a Reader payload. ''' class_id = payload.read_short() weight = payload.read_short() size = payload.read_longlong() properties = {} # The AMQP spec is overly-complex when it comes to handling header # frames. The spec says that in addition to the first 16bit field, # additional ones can follow which /may/ then be in the property list # (because bit flags aren't in the list). Properly implementing custom # values requires the ability change the properties and their types, # which someone is welcome to do, but seriously, what's the point? # Because the complexity of parsing and writing this frame directly # impacts the speed at which messages can be processed, there are two # branches for both a fast parse which assumes no changes to the # properties and a slow parse. For now it's up to someone using custom # headers to flip the flag. if self.DEFAULT_PROPERTIES: flag_bits = payload.read_short() for key, proptype, rfunc, wfunc, mask in self.PROPERTIES: if flag_bits & mask: properties[key] = rfunc(payload) else: flags = [] while True: flag_bits = payload.read_short() flags.append(flag_bits) if flag_bits & 1 == 0: break shift = 0 for key, proptype, rfunc, wfunc, mask in self.PROPERTIES: if shift == 0: if not flags: break flag_bits, flags = flags[0], flags[1:] shift = 15 if flag_bits & (1 << shift): properties[key] = rfunc(payload) shift -= 1 return HeaderFrame(channel_id, class_id, weight, size, properties)
python
def parse(self, channel_id, payload): ''' Parse a header frame for a channel given a Reader payload. ''' class_id = payload.read_short() weight = payload.read_short() size = payload.read_longlong() properties = {} # The AMQP spec is overly-complex when it comes to handling header # frames. The spec says that in addition to the first 16bit field, # additional ones can follow which /may/ then be in the property list # (because bit flags aren't in the list). Properly implementing custom # values requires the ability change the properties and their types, # which someone is welcome to do, but seriously, what's the point? # Because the complexity of parsing and writing this frame directly # impacts the speed at which messages can be processed, there are two # branches for both a fast parse which assumes no changes to the # properties and a slow parse. For now it's up to someone using custom # headers to flip the flag. if self.DEFAULT_PROPERTIES: flag_bits = payload.read_short() for key, proptype, rfunc, wfunc, mask in self.PROPERTIES: if flag_bits & mask: properties[key] = rfunc(payload) else: flags = [] while True: flag_bits = payload.read_short() flags.append(flag_bits) if flag_bits & 1 == 0: break shift = 0 for key, proptype, rfunc, wfunc, mask in self.PROPERTIES: if shift == 0: if not flags: break flag_bits, flags = flags[0], flags[1:] shift = 15 if flag_bits & (1 << shift): properties[key] = rfunc(payload) shift -= 1 return HeaderFrame(channel_id, class_id, weight, size, properties)
[ "def", "parse", "(", "self", ",", "channel_id", ",", "payload", ")", ":", "class_id", "=", "payload", ".", "read_short", "(", ")", "weight", "=", "payload", ".", "read_short", "(", ")", "size", "=", "payload", ".", "read_longlong", "(", ")", "properties"...
Parse a header frame for a channel given a Reader payload.
[ "Parse", "a", "header", "frame", "for", "a", "channel", "given", "a", "Reader", "payload", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/frames/header_frame.py#L71-L115
7,845
agoragames/haigha
haigha/frames/frame.py
Frame.read_frames
def read_frames(cls, reader): ''' Read one or more frames from an IO stream. Buffer must support file object interface. After reading, caller will need to check if there are bytes remaining in the stream. If there are, then that implies that there is one or more incomplete frames and more data needs to be read. The position of the cursor in the frame stream will mark the point at which the last good frame was read. If the caller is expecting a sequence of frames and only received a part of that sequence, they are responsible for buffering those frames until the rest of the frames in the sequence have arrived. ''' rval = deque() while True: frame_start_pos = reader.tell() try: frame = Frame._read_frame(reader) except Reader.BufferUnderflow: # No more data in the stream frame = None except Reader.ReaderError as e: # Some other format error raise Frame.FormatError, str(e), sys.exc_info()[-1] except struct.error as e: raise Frame.FormatError, str(e), sys.exc_info()[-1] if frame is None: reader.seek(frame_start_pos) break rval.append(frame) return rval
python
def read_frames(cls, reader): ''' Read one or more frames from an IO stream. Buffer must support file object interface. After reading, caller will need to check if there are bytes remaining in the stream. If there are, then that implies that there is one or more incomplete frames and more data needs to be read. The position of the cursor in the frame stream will mark the point at which the last good frame was read. If the caller is expecting a sequence of frames and only received a part of that sequence, they are responsible for buffering those frames until the rest of the frames in the sequence have arrived. ''' rval = deque() while True: frame_start_pos = reader.tell() try: frame = Frame._read_frame(reader) except Reader.BufferUnderflow: # No more data in the stream frame = None except Reader.ReaderError as e: # Some other format error raise Frame.FormatError, str(e), sys.exc_info()[-1] except struct.error as e: raise Frame.FormatError, str(e), sys.exc_info()[-1] if frame is None: reader.seek(frame_start_pos) break rval.append(frame) return rval
[ "def", "read_frames", "(", "cls", ",", "reader", ")", ":", "rval", "=", "deque", "(", ")", "while", "True", ":", "frame_start_pos", "=", "reader", ".", "tell", "(", ")", "try", ":", "frame", "=", "Frame", ".", "_read_frame", "(", "reader", ")", "exce...
Read one or more frames from an IO stream. Buffer must support file object interface. After reading, caller will need to check if there are bytes remaining in the stream. If there are, then that implies that there is one or more incomplete frames and more data needs to be read. The position of the cursor in the frame stream will mark the point at which the last good frame was read. If the caller is expecting a sequence of frames and only received a part of that sequence, they are responsible for buffering those frames until the rest of the frames in the sequence have arrived.
[ "Read", "one", "or", "more", "frames", "from", "an", "IO", "stream", ".", "Buffer", "must", "support", "file", "object", "interface", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/frames/frame.py#L49-L84
7,846
agoragames/haigha
haigha/frames/frame.py
Frame._read_frame
def _read_frame(cls, reader): ''' Read a single frame from a Reader. Will return None if there is an incomplete frame in the stream. Raise MissingFooter if there's a problem reading the footer byte. ''' frame_type = reader.read_octet() channel_id = reader.read_short() size = reader.read_long() payload = Reader(reader, reader.tell(), size) # Seek to end of payload reader.seek(size, 1) ch = reader.read_octet() # footer if ch != 0xce: raise Frame.FormatError( 'Framing error, unexpected byte: %x. frame type %x. channel %d, payload size %d', ch, frame_type, channel_id, size) frame_class = cls._frame_type_map.get(frame_type) if not frame_class: raise Frame.InvalidFrameType("Unknown frame type %x", frame_type) return frame_class.parse(channel_id, payload)
python
def _read_frame(cls, reader): ''' Read a single frame from a Reader. Will return None if there is an incomplete frame in the stream. Raise MissingFooter if there's a problem reading the footer byte. ''' frame_type = reader.read_octet() channel_id = reader.read_short() size = reader.read_long() payload = Reader(reader, reader.tell(), size) # Seek to end of payload reader.seek(size, 1) ch = reader.read_octet() # footer if ch != 0xce: raise Frame.FormatError( 'Framing error, unexpected byte: %x. frame type %x. channel %d, payload size %d', ch, frame_type, channel_id, size) frame_class = cls._frame_type_map.get(frame_type) if not frame_class: raise Frame.InvalidFrameType("Unknown frame type %x", frame_type) return frame_class.parse(channel_id, payload)
[ "def", "_read_frame", "(", "cls", ",", "reader", ")", ":", "frame_type", "=", "reader", ".", "read_octet", "(", ")", "channel_id", "=", "reader", ".", "read_short", "(", ")", "size", "=", "reader", ".", "read_long", "(", ")", "payload", "=", "Reader", ...
Read a single frame from a Reader. Will return None if there is an incomplete frame in the stream. Raise MissingFooter if there's a problem reading the footer byte.
[ "Read", "a", "single", "frame", "from", "a", "Reader", ".", "Will", "return", "None", "if", "there", "is", "an", "incomplete", "frame", "in", "the", "stream", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/frames/frame.py#L87-L112
7,847
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitExchangeClass.unbind
def unbind(self, exchange, source, routing_key='', nowait=True, arguments={}, ticket=None, cb=None): ''' Unbind an exchange from another. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(exchange).\ write_shortstr(source).\ write_shortstr(routing_key).\ write_bit(nowait).\ write_table(arguments or {}) self.send_frame(MethodFrame(self.channel_id, 40, 40, args)) if not nowait: self._unbind_cb.append(cb) self.channel.add_synchronous_cb(self._recv_unbind_ok)
python
def unbind(self, exchange, source, routing_key='', nowait=True, arguments={}, ticket=None, cb=None): ''' Unbind an exchange from another. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(exchange).\ write_shortstr(source).\ write_shortstr(routing_key).\ write_bit(nowait).\ write_table(arguments or {}) self.send_frame(MethodFrame(self.channel_id, 40, 40, args)) if not nowait: self._unbind_cb.append(cb) self.channel.add_synchronous_cb(self._recv_unbind_ok)
[ "def", "unbind", "(", "self", ",", "exchange", ",", "source", ",", "routing_key", "=", "''", ",", "nowait", "=", "True", ",", "arguments", "=", "{", "}", ",", "ticket", "=", "None", ",", "cb", "=", "None", ")", ":", "nowait", "=", "nowait", "and", ...
Unbind an exchange from another.
[ "Unbind", "an", "exchange", "from", "another", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L117-L135
7,848
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitBasicClass.publish
def publish(self, *args, **kwargs): ''' Publish a message. Will return the id of the message if publisher confirmations are enabled, else will return 0. ''' if self.channel.confirm._enabled: self._msg_id += 1 super(RabbitBasicClass, self).publish(*args, **kwargs) return self._msg_id
python
def publish(self, *args, **kwargs): ''' Publish a message. Will return the id of the message if publisher confirmations are enabled, else will return 0. ''' if self.channel.confirm._enabled: self._msg_id += 1 super(RabbitBasicClass, self).publish(*args, **kwargs) return self._msg_id
[ "def", "publish", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "channel", ".", "confirm", ".", "_enabled", ":", "self", ".", "_msg_id", "+=", "1", "super", "(", "RabbitBasicClass", ",", "self", ")", ".", "pub...
Publish a message. Will return the id of the message if publisher confirmations are enabled, else will return 0.
[ "Publish", "a", "message", ".", "Will", "return", "the", "id", "of", "the", "message", "if", "publisher", "confirmations", "are", "enabled", "else", "will", "return", "0", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L196-L204
7,849
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitBasicClass._recv_ack
def _recv_ack(self, method_frame): '''Receive an ack from the broker.''' if self._ack_listener: delivery_tag = method_frame.args.read_longlong() multiple = method_frame.args.read_bit() if multiple: while self._last_ack_id < delivery_tag: self._last_ack_id += 1 self._ack_listener(self._last_ack_id) else: self._last_ack_id = delivery_tag self._ack_listener(self._last_ack_id)
python
def _recv_ack(self, method_frame): '''Receive an ack from the broker.''' if self._ack_listener: delivery_tag = method_frame.args.read_longlong() multiple = method_frame.args.read_bit() if multiple: while self._last_ack_id < delivery_tag: self._last_ack_id += 1 self._ack_listener(self._last_ack_id) else: self._last_ack_id = delivery_tag self._ack_listener(self._last_ack_id)
[ "def", "_recv_ack", "(", "self", ",", "method_frame", ")", ":", "if", "self", ".", "_ack_listener", ":", "delivery_tag", "=", "method_frame", ".", "args", ".", "read_longlong", "(", ")", "multiple", "=", "method_frame", ".", "args", ".", "read_bit", "(", "...
Receive an ack from the broker.
[ "Receive", "an", "ack", "from", "the", "broker", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L206-L217
7,850
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitBasicClass.nack
def nack(self, delivery_tag, multiple=False, requeue=False): '''Send a nack to the broker.''' args = Writer() args.write_longlong(delivery_tag).\ write_bits(multiple, requeue) self.send_frame(MethodFrame(self.channel_id, 60, 120, args))
python
def nack(self, delivery_tag, multiple=False, requeue=False): '''Send a nack to the broker.''' args = Writer() args.write_longlong(delivery_tag).\ write_bits(multiple, requeue) self.send_frame(MethodFrame(self.channel_id, 60, 120, args))
[ "def", "nack", "(", "self", ",", "delivery_tag", ",", "multiple", "=", "False", ",", "requeue", "=", "False", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_longlong", "(", "delivery_tag", ")", ".", "write_bits", "(", "multiple", ",", "...
Send a nack to the broker.
[ "Send", "a", "nack", "to", "the", "broker", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L219-L225
7,851
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitBasicClass._recv_nack
def _recv_nack(self, method_frame): '''Receive a nack from the broker.''' if self._nack_listener: delivery_tag = method_frame.args.read_longlong() multiple, requeue = method_frame.args.read_bits(2) if multiple: while self._last_ack_id < delivery_tag: self._last_ack_id += 1 self._nack_listener(self._last_ack_id, requeue) else: self._last_ack_id = delivery_tag self._nack_listener(self._last_ack_id, requeue)
python
def _recv_nack(self, method_frame): '''Receive a nack from the broker.''' if self._nack_listener: delivery_tag = method_frame.args.read_longlong() multiple, requeue = method_frame.args.read_bits(2) if multiple: while self._last_ack_id < delivery_tag: self._last_ack_id += 1 self._nack_listener(self._last_ack_id, requeue) else: self._last_ack_id = delivery_tag self._nack_listener(self._last_ack_id, requeue)
[ "def", "_recv_nack", "(", "self", ",", "method_frame", ")", ":", "if", "self", ".", "_nack_listener", ":", "delivery_tag", "=", "method_frame", ".", "args", ".", "read_longlong", "(", ")", "multiple", ",", "requeue", "=", "method_frame", ".", "args", ".", ...
Receive a nack from the broker.
[ "Receive", "a", "nack", "from", "the", "broker", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L227-L238
7,852
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitBasicClass._recv_cancel
def _recv_cancel(self, method_frame): '''Handle Basic.Cancel from broker :param MethodFrame method_frame: Basic.Cancel method frame from broker ''' self.logger.warning("consumer cancelled by broker: %r", method_frame) consumer_tag = method_frame.args.read_shortstr() # NOTE: per RabbitMQ spec, no-wait is always true in Basic.Cancel from # broker # Remove consumer from this basic instance try: cancel_cb = self._broker_cancel_cb_map.pop(consumer_tag) except KeyError: # Must be a race condition between user's cancel and broker's cancel self.logger.warning( '_recv_cancel: no broker-cancel-cb entry for consumer tag %r', consumer_tag) else: if callable(cancel_cb): # Purge from base class only when user supplies cancel_cb self._purge_consumer_by_tag(consumer_tag) # Notify user cancel_cb(consumer_tag)
python
def _recv_cancel(self, method_frame): '''Handle Basic.Cancel from broker :param MethodFrame method_frame: Basic.Cancel method frame from broker ''' self.logger.warning("consumer cancelled by broker: %r", method_frame) consumer_tag = method_frame.args.read_shortstr() # NOTE: per RabbitMQ spec, no-wait is always true in Basic.Cancel from # broker # Remove consumer from this basic instance try: cancel_cb = self._broker_cancel_cb_map.pop(consumer_tag) except KeyError: # Must be a race condition between user's cancel and broker's cancel self.logger.warning( '_recv_cancel: no broker-cancel-cb entry for consumer tag %r', consumer_tag) else: if callable(cancel_cb): # Purge from base class only when user supplies cancel_cb self._purge_consumer_by_tag(consumer_tag) # Notify user cancel_cb(consumer_tag)
[ "def", "_recv_cancel", "(", "self", ",", "method_frame", ")", ":", "self", ".", "logger", ".", "warning", "(", "\"consumer cancelled by broker: %r\"", ",", "method_frame", ")", "consumer_tag", "=", "method_frame", ".", "args", ".", "read_shortstr", "(", ")", "# ...
Handle Basic.Cancel from broker :param MethodFrame method_frame: Basic.Cancel method frame from broker
[ "Handle", "Basic", ".", "Cancel", "from", "broker" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L290-L316
7,853
agoragames/haigha
haigha/connections/rabbit_connection.py
RabbitConfirmClass.select
def select(self, nowait=True, cb=None): ''' Set this channel to use publisher confirmations. ''' nowait = nowait and self.allow_nowait() and not cb if not self._enabled: self._enabled = True self.channel.basic._msg_id = 0 self.channel.basic._last_ack_id = 0 args = Writer() args.write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 85, 10, args)) if not nowait: self._select_cb.append(cb) self.channel.add_synchronous_cb(self._recv_select_ok)
python
def select(self, nowait=True, cb=None): ''' Set this channel to use publisher confirmations. ''' nowait = nowait and self.allow_nowait() and not cb if not self._enabled: self._enabled = True self.channel.basic._msg_id = 0 self.channel.basic._last_ack_id = 0 args = Writer() args.write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 85, 10, args)) if not nowait: self._select_cb.append(cb) self.channel.add_synchronous_cb(self._recv_select_ok)
[ "def", "select", "(", "self", ",", "nowait", "=", "True", ",", "cb", "=", "None", ")", ":", "nowait", "=", "nowait", "and", "self", ".", "allow_nowait", "(", ")", "and", "not", "cb", "if", "not", "self", ".", "_enabled", ":", "self", ".", "_enabled...
Set this channel to use publisher confirmations.
[ "Set", "this", "channel", "to", "use", "publisher", "confirmations", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/connections/rabbit_connection.py#L338-L355
7,854
agoragames/haigha
haigha/channel.py
Channel.close
def close(self, reply_code=0, reply_text='', class_id=0, method_id=0): ''' Close this channel. Routes to channel.close. ''' # In the off chance that we call this twice. A good example is if # there's an error in close listeners and so we're still inside a # single call to process_frames, which will try to close this channel # if there's an exception. if hasattr(self, 'channel'): self.channel.close(reply_code, reply_text, class_id, method_id)
python
def close(self, reply_code=0, reply_text='', class_id=0, method_id=0): ''' Close this channel. Routes to channel.close. ''' # In the off chance that we call this twice. A good example is if # there's an error in close listeners and so we're still inside a # single call to process_frames, which will try to close this channel # if there's an exception. if hasattr(self, 'channel'): self.channel.close(reply_code, reply_text, class_id, method_id)
[ "def", "close", "(", "self", ",", "reply_code", "=", "0", ",", "reply_text", "=", "''", ",", "class_id", "=", "0", ",", "method_id", "=", "0", ")", ":", "# In the off chance that we call this twice. A good example is if", "# there's an error in close listeners and so we...
Close this channel. Routes to channel.close.
[ "Close", "this", "channel", ".", "Routes", "to", "channel", ".", "close", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L187-L196
7,855
agoragames/haigha
haigha/channel.py
Channel.publish_synchronous
def publish_synchronous(self, *args, **kwargs): ''' Helper for publishing a message using transactions. If 'cb' keyword arg is supplied, will be called when the transaction is committed. ''' cb = kwargs.pop('cb', None) self.tx.select() self.basic.publish(*args, **kwargs) self.tx.commit(cb=cb)
python
def publish_synchronous(self, *args, **kwargs): ''' Helper for publishing a message using transactions. If 'cb' keyword arg is supplied, will be called when the transaction is committed. ''' cb = kwargs.pop('cb', None) self.tx.select() self.basic.publish(*args, **kwargs) self.tx.commit(cb=cb)
[ "def", "publish_synchronous", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "cb", "=", "kwargs", ".", "pop", "(", "'cb'", ",", "None", ")", "self", ".", "tx", ".", "select", "(", ")", "self", ".", "basic", ".", "publish", "(",...
Helper for publishing a message using transactions. If 'cb' keyword arg is supplied, will be called when the transaction is committed.
[ "Helper", "for", "publishing", "a", "message", "using", "transactions", ".", "If", "cb", "keyword", "arg", "is", "supplied", "will", "be", "called", "when", "the", "transaction", "is", "committed", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L204-L212
7,856
agoragames/haigha
haigha/channel.py
Channel.dispatch
def dispatch(self, method_frame): ''' Dispatch a method. ''' klass = self._class_map.get(method_frame.class_id) if klass: klass.dispatch(method_frame) else: raise Channel.InvalidClass( "class %d is not supported on channel %d", method_frame.class_id, self.channel_id)
python
def dispatch(self, method_frame): ''' Dispatch a method. ''' klass = self._class_map.get(method_frame.class_id) if klass: klass.dispatch(method_frame) else: raise Channel.InvalidClass( "class %d is not supported on channel %d", method_frame.class_id, self.channel_id)
[ "def", "dispatch", "(", "self", ",", "method_frame", ")", ":", "klass", "=", "self", ".", "_class_map", ".", "get", "(", "method_frame", ".", "class_id", ")", "if", "klass", ":", "klass", ".", "dispatch", "(", "method_frame", ")", "else", ":", "raise", ...
Dispatch a method.
[ "Dispatch", "a", "method", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L214-L224
7,857
agoragames/haigha
haigha/channel.py
Channel.process_frames
def process_frames(self): ''' Process the input buffer. ''' while len(self._frame_buffer): # It would make sense to call next_frame, but it's # technically faster to repeat the code here. frame = self._frame_buffer.popleft() if self._emergency_close_pending: # Implement stability rule from AMQP 0.9.1 section 1.5.2.5. # Method channel.close: "After sending this method, any # received methods except Close and Close-OK MUST be discarded." # # NOTE: presently, we limit our implementation of the rule to # the "emergency close" scenario to avoid potential adverse # side-effect during normal user-initiated close if (not isinstance(frame, MethodFrame) or frame.class_id != self.channel.CLASS_ID or frame.method_id not in (self.channel.CLOSE_METHOD_ID, self.channel.CLOSE_OK_METHOD_ID)): self.logger.warn("Emergency channel close: dropping input " "frame %.255s", frame) continue try: self.dispatch(frame) except ProtocolClass.FrameUnderflow: return except (ConnectionClosed, ChannelClosed): # Immediately raise if connection or channel is closed raise except Exception: self.logger.exception( "Closing on failed dispatch of frame %.255s", frame) # Spec says that channel should be closed if there's a framing # error. Unsure if we can send close if the current exception # is transport level (e.g. gevent.GreenletExit) self._emergency_close_pending = True # Preserve the original exception and traceback during cleanup, # only allowing system-exiting exceptions (e.g., SystemExit, # KeyboardInterrupt) to override it try: raise finally: try: self.close(500, "Failed to dispatch %s" % (str(frame))) except Exception: # Suppress secondary non-system-exiting exception in # favor of the original exception self.logger.exception("Channel close failed") pass
python
def process_frames(self): ''' Process the input buffer. ''' while len(self._frame_buffer): # It would make sense to call next_frame, but it's # technically faster to repeat the code here. frame = self._frame_buffer.popleft() if self._emergency_close_pending: # Implement stability rule from AMQP 0.9.1 section 1.5.2.5. # Method channel.close: "After sending this method, any # received methods except Close and Close-OK MUST be discarded." # # NOTE: presently, we limit our implementation of the rule to # the "emergency close" scenario to avoid potential adverse # side-effect during normal user-initiated close if (not isinstance(frame, MethodFrame) or frame.class_id != self.channel.CLASS_ID or frame.method_id not in (self.channel.CLOSE_METHOD_ID, self.channel.CLOSE_OK_METHOD_ID)): self.logger.warn("Emergency channel close: dropping input " "frame %.255s", frame) continue try: self.dispatch(frame) except ProtocolClass.FrameUnderflow: return except (ConnectionClosed, ChannelClosed): # Immediately raise if connection or channel is closed raise except Exception: self.logger.exception( "Closing on failed dispatch of frame %.255s", frame) # Spec says that channel should be closed if there's a framing # error. Unsure if we can send close if the current exception # is transport level (e.g. gevent.GreenletExit) self._emergency_close_pending = True # Preserve the original exception and traceback during cleanup, # only allowing system-exiting exceptions (e.g., SystemExit, # KeyboardInterrupt) to override it try: raise finally: try: self.close(500, "Failed to dispatch %s" % (str(frame))) except Exception: # Suppress secondary non-system-exiting exception in # favor of the original exception self.logger.exception("Channel close failed") pass
[ "def", "process_frames", "(", "self", ")", ":", "while", "len", "(", "self", ".", "_frame_buffer", ")", ":", "# It would make sense to call next_frame, but it's", "# technically faster to repeat the code here.", "frame", "=", "self", ".", "_frame_buffer", ".", "popleft", ...
Process the input buffer.
[ "Process", "the", "input", "buffer", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L233-L285
7,858
agoragames/haigha
haigha/channel.py
Channel.send_frame
def send_frame(self, frame): ''' Queue a frame for sending. Will send immediately if there are no pending synchronous transactions on this connection. ''' if self.closed: if self.close_info and len(self.close_info['reply_text']) > 0: raise ChannelClosed( "channel %d is closed: %s : %s", self.channel_id, self.close_info['reply_code'], self.close_info['reply_text']) raise ChannelClosed() # If there's any pending event at all, then it means that when the # current dispatch loop started, all possible frames were flushed # and the remaining item(s) starts with a sync callback. After careful # consideration, it seems that it's safe to assume the len>0 means to # buffer the frame. The other advantage here is if not len(self._pending_events): if not self._active and \ isinstance(frame, (ContentFrame, HeaderFrame)): raise Channel.Inactive( "Channel %d flow control activated", self.channel_id) self._connection.send_frame(frame) else: self._pending_events.append(frame)
python
def send_frame(self, frame): ''' Queue a frame for sending. Will send immediately if there are no pending synchronous transactions on this connection. ''' if self.closed: if self.close_info and len(self.close_info['reply_text']) > 0: raise ChannelClosed( "channel %d is closed: %s : %s", self.channel_id, self.close_info['reply_code'], self.close_info['reply_text']) raise ChannelClosed() # If there's any pending event at all, then it means that when the # current dispatch loop started, all possible frames were flushed # and the remaining item(s) starts with a sync callback. After careful # consideration, it seems that it's safe to assume the len>0 means to # buffer the frame. The other advantage here is if not len(self._pending_events): if not self._active and \ isinstance(frame, (ContentFrame, HeaderFrame)): raise Channel.Inactive( "Channel %d flow control activated", self.channel_id) self._connection.send_frame(frame) else: self._pending_events.append(frame)
[ "def", "send_frame", "(", "self", ",", "frame", ")", ":", "if", "self", ".", "closed", ":", "if", "self", ".", "close_info", "and", "len", "(", "self", ".", "close_info", "[", "'reply_text'", "]", ")", ">", "0", ":", "raise", "ChannelClosed", "(", "\...
Queue a frame for sending. Will send immediately if there are no pending synchronous transactions on this connection.
[ "Queue", "a", "frame", "for", "sending", ".", "Will", "send", "immediately", "if", "there", "are", "no", "pending", "synchronous", "transactions", "on", "this", "connection", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L304-L330
7,859
agoragames/haigha
haigha/channel.py
Channel.add_synchronous_cb
def add_synchronous_cb(self, cb): ''' Add an expectation of a callback to release a synchronous transaction. ''' if self.connection.synchronous or self._synchronous: wrapper = SyncWrapper(cb) self._pending_events.append(wrapper) while wrapper._read: # Don't check that the channel has been closed until after # reading frames, in the case that this is processing a clean # channel closed. If there's a protocol error during # read_frames, this will loop back around and result in a # channel closed exception. if self.closed: if self.close_info and \ len(self.close_info['reply_text']) > 0: raise ChannelClosed( "channel %d is closed: %s : %s", self.channel_id, self.close_info['reply_code'], self.close_info['reply_text']) raise ChannelClosed() self.connection.read_frames() return wrapper._result else: self._pending_events.append(cb)
python
def add_synchronous_cb(self, cb): ''' Add an expectation of a callback to release a synchronous transaction. ''' if self.connection.synchronous or self._synchronous: wrapper = SyncWrapper(cb) self._pending_events.append(wrapper) while wrapper._read: # Don't check that the channel has been closed until after # reading frames, in the case that this is processing a clean # channel closed. If there's a protocol error during # read_frames, this will loop back around and result in a # channel closed exception. if self.closed: if self.close_info and \ len(self.close_info['reply_text']) > 0: raise ChannelClosed( "channel %d is closed: %s : %s", self.channel_id, self.close_info['reply_code'], self.close_info['reply_text']) raise ChannelClosed() self.connection.read_frames() return wrapper._result else: self._pending_events.append(cb)
[ "def", "add_synchronous_cb", "(", "self", ",", "cb", ")", ":", "if", "self", ".", "connection", ".", "synchronous", "or", "self", ".", "_synchronous", ":", "wrapper", "=", "SyncWrapper", "(", "cb", ")", "self", ".", "_pending_events", ".", "append", "(", ...
Add an expectation of a callback to release a synchronous transaction.
[ "Add", "an", "expectation", "of", "a", "callback", "to", "release", "a", "synchronous", "transaction", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L332-L358
7,860
agoragames/haigha
haigha/channel.py
Channel.clear_synchronous_cb
def clear_synchronous_cb(self, cb): ''' If the callback is the current expected callback, will clear it off the stack. Else will raise in exception if there's an expectation but this doesn't satisfy it. ''' if len(self._pending_events): ev = self._pending_events[0] # We can't have a strict check using this simple mechanism, # because we could be waiting for a synch response while messages # are being published. So for now, if it's not in the list, do a # check to see if the callback is in the pending list, and if so, # then raise, because it means we received stuff out of order. # Else just pass it through. Note that this situation could happen # on any broker-initiated message. if ev == cb: self._pending_events.popleft() self._flush_pending_events() return ev elif cb in self._pending_events: raise ChannelError( "Expected synchronous callback %s, got %s", ev, cb) # Return the passed-in callback by default return cb
python
def clear_synchronous_cb(self, cb): ''' If the callback is the current expected callback, will clear it off the stack. Else will raise in exception if there's an expectation but this doesn't satisfy it. ''' if len(self._pending_events): ev = self._pending_events[0] # We can't have a strict check using this simple mechanism, # because we could be waiting for a synch response while messages # are being published. So for now, if it's not in the list, do a # check to see if the callback is in the pending list, and if so, # then raise, because it means we received stuff out of order. # Else just pass it through. Note that this situation could happen # on any broker-initiated message. if ev == cb: self._pending_events.popleft() self._flush_pending_events() return ev elif cb in self._pending_events: raise ChannelError( "Expected synchronous callback %s, got %s", ev, cb) # Return the passed-in callback by default return cb
[ "def", "clear_synchronous_cb", "(", "self", ",", "cb", ")", ":", "if", "len", "(", "self", ".", "_pending_events", ")", ":", "ev", "=", "self", ".", "_pending_events", "[", "0", "]", "# We can't have a strict check using this simple mechanism,", "# because we could ...
If the callback is the current expected callback, will clear it off the stack. Else will raise in exception if there's an expectation but this doesn't satisfy it.
[ "If", "the", "callback", "is", "the", "current", "expected", "callback", "will", "clear", "it", "off", "the", "stack", ".", "Else", "will", "raise", "in", "exception", "if", "there", "s", "an", "expectation", "but", "this", "doesn", "t", "satisfy", "it", ...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L360-L385
7,861
agoragames/haigha
haigha/channel.py
Channel._flush_pending_events
def _flush_pending_events(self): ''' Send pending frames that are in the event queue. ''' while len(self._pending_events) and \ isinstance(self._pending_events[0], Frame): self._connection.send_frame(self._pending_events.popleft())
python
def _flush_pending_events(self): ''' Send pending frames that are in the event queue. ''' while len(self._pending_events) and \ isinstance(self._pending_events[0], Frame): self._connection.send_frame(self._pending_events.popleft())
[ "def", "_flush_pending_events", "(", "self", ")", ":", "while", "len", "(", "self", ".", "_pending_events", ")", "and", "isinstance", "(", "self", ".", "_pending_events", "[", "0", "]", ",", "Frame", ")", ":", "self", ".", "_connection", ".", "send_frame",...
Send pending frames that are in the event queue.
[ "Send", "pending", "frames", "that", "are", "in", "the", "event", "queue", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L387-L393
7,862
agoragames/haigha
haigha/channel.py
Channel._closed_cb
def _closed_cb(self, final_frame=None): ''' "Private" callback from the ChannelClass when a channel is closed. Only called after broker initiated close, or we receive a close_ok. Caller has the option to send a final frame, to be used to bypass any synchronous or otherwise-pending frames so that the channel can be cleanly closed. ''' # delete all pending data and send final frame if thre is one. note # that it bypasses send_frame so that even if the closed state is set, # the frame is published. if final_frame: self._connection.send_frame(final_frame) try: self._notify_close_listeners() finally: self._pending_events = deque() self._frame_buffer = deque() # clear out other references for faster cleanup for protocol_class in self._class_map.values(): protocol_class._cleanup() delattr(self, protocol_class.name) self._connection = None self._class_map = None self._close_listeners = set()
python
def _closed_cb(self, final_frame=None): ''' "Private" callback from the ChannelClass when a channel is closed. Only called after broker initiated close, or we receive a close_ok. Caller has the option to send a final frame, to be used to bypass any synchronous or otherwise-pending frames so that the channel can be cleanly closed. ''' # delete all pending data and send final frame if thre is one. note # that it bypasses send_frame so that even if the closed state is set, # the frame is published. if final_frame: self._connection.send_frame(final_frame) try: self._notify_close_listeners() finally: self._pending_events = deque() self._frame_buffer = deque() # clear out other references for faster cleanup for protocol_class in self._class_map.values(): protocol_class._cleanup() delattr(self, protocol_class.name) self._connection = None self._class_map = None self._close_listeners = set()
[ "def", "_closed_cb", "(", "self", ",", "final_frame", "=", "None", ")", ":", "# delete all pending data and send final frame if thre is one. note", "# that it bypasses send_frame so that even if the closed state is set,", "# the frame is published.", "if", "final_frame", ":", "self",...
"Private" callback from the ChannelClass when a channel is closed. Only called after broker initiated close, or we receive a close_ok. Caller has the option to send a final frame, to be used to bypass any synchronous or otherwise-pending frames so that the channel can be cleanly closed.
[ "Private", "callback", "from", "the", "ChannelClass", "when", "a", "channel", "is", "closed", ".", "Only", "called", "after", "broker", "initiated", "close", "or", "we", "receive", "a", "close_ok", ".", "Caller", "has", "the", "option", "to", "send", "a", ...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/channel.py#L395-L421
7,863
agoragames/haigha
haigha/transports/gevent_transport.py
GeventTransport.connect
def connect(self, (host, port)): ''' Connect using a host,port tuple ''' super(GeventTransport, self).connect((host, port), klass=socket.socket)
python
def connect(self, (host, port)): ''' Connect using a host,port tuple ''' super(GeventTransport, self).connect((host, port), klass=socket.socket)
[ "def", "connect", "(", "self", ",", "(", "host", ",", "port", ")", ")", ":", "super", "(", "GeventTransport", ",", "self", ")", ".", "connect", "(", "(", "host", ",", "port", ")", ",", "klass", "=", "socket", ".", "socket", ")" ]
Connect using a host,port tuple
[ "Connect", "using", "a", "host", "port", "tuple" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/transports/gevent_transport.py#L49-L53
7,864
agoragames/haigha
haigha/transports/gevent_transport.py
GeventTransport.read
def read(self, timeout=None): ''' Read from the transport. If no data is available, should return None. If timeout>0, will only block for `timeout` seconds. ''' # If currently locked, another greenlet is trying to read, so yield # control and then return none. Required if a Connection is configured # to be synchronous, a sync callback is trying to read, and there's # another read loop running read_frames. Without it, the run loop will # release the lock but then immediately acquire it again. Yielding # control in the reading thread after bytes are read won't fix # anything, because it's quite possible the bytes read resulted in a # frame that satisfied the synchronous callback, and so this needs to # return immediately to first check the current status of synchronous # callbacks before attempting to read again. if self._read_lock.locked(): self._read_wait.wait(timeout) return None self._read_lock.acquire() try: return super(GeventTransport, self).read(timeout=timeout) finally: self._read_lock.release() self._read_wait.set() self._read_wait.clear()
python
def read(self, timeout=None): ''' Read from the transport. If no data is available, should return None. If timeout>0, will only block for `timeout` seconds. ''' # If currently locked, another greenlet is trying to read, so yield # control and then return none. Required if a Connection is configured # to be synchronous, a sync callback is trying to read, and there's # another read loop running read_frames. Without it, the run loop will # release the lock but then immediately acquire it again. Yielding # control in the reading thread after bytes are read won't fix # anything, because it's quite possible the bytes read resulted in a # frame that satisfied the synchronous callback, and so this needs to # return immediately to first check the current status of synchronous # callbacks before attempting to read again. if self._read_lock.locked(): self._read_wait.wait(timeout) return None self._read_lock.acquire() try: return super(GeventTransport, self).read(timeout=timeout) finally: self._read_lock.release() self._read_wait.set() self._read_wait.clear()
[ "def", "read", "(", "self", ",", "timeout", "=", "None", ")", ":", "# If currently locked, another greenlet is trying to read, so yield", "# control and then return none. Required if a Connection is configured", "# to be synchronous, a sync callback is trying to read, and there's", "# anot...
Read from the transport. If no data is available, should return None. If timeout>0, will only block for `timeout` seconds.
[ "Read", "from", "the", "transport", ".", "If", "no", "data", "is", "available", "should", "return", "None", ".", "If", "timeout", ">", "0", "will", "only", "block", "for", "timeout", "seconds", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/transports/gevent_transport.py#L55-L80
7,865
agoragames/haigha
haigha/classes/protocol_class.py
ProtocolClass.dispatch
def dispatch(self, method_frame): ''' Dispatch a method for this protocol. ''' method = self.dispatch_map.get(method_frame.method_id) if method: callback = self.channel.clear_synchronous_cb(method) callback(method_frame) else: raise self.InvalidMethod( "no method is registered with id: %d" % method_frame.method_id)
python
def dispatch(self, method_frame): ''' Dispatch a method for this protocol. ''' method = self.dispatch_map.get(method_frame.method_id) if method: callback = self.channel.clear_synchronous_cb(method) callback(method_frame) else: raise self.InvalidMethod( "no method is registered with id: %d" % method_frame.method_id)
[ "def", "dispatch", "(", "self", ",", "method_frame", ")", ":", "method", "=", "self", ".", "dispatch_map", ".", "get", "(", "method_frame", ".", "method_id", ")", "if", "method", ":", "callback", "=", "self", ".", "channel", ".", "clear_synchronous_cb", "(...
Dispatch a method for this protocol.
[ "Dispatch", "a", "method", "for", "this", "protocol", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/protocol_class.py#L73-L83
7,866
agoragames/haigha
haigha/reader.py
Reader.seek
def seek(self, offset, whence=0): ''' Simple seek. Follows standard interface. ''' if whence == 0: self._pos = self._start_pos + offset elif whence == 1: self._pos += offset else: self._pos = (self._end_pos - 1) + offset
python
def seek(self, offset, whence=0): ''' Simple seek. Follows standard interface. ''' if whence == 0: self._pos = self._start_pos + offset elif whence == 1: self._pos += offset else: self._pos = (self._end_pos - 1) + offset
[ "def", "seek", "(", "self", ",", "offset", ",", "whence", "=", "0", ")", ":", "if", "whence", "==", "0", ":", "self", ".", "_pos", "=", "self", ".", "_start_pos", "+", "offset", "elif", "whence", "==", "1", ":", "self", ".", "_pos", "+=", "offset...
Simple seek. Follows standard interface.
[ "Simple", "seek", ".", "Follows", "standard", "interface", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/reader.py#L65-L74
7,867
agoragames/haigha
haigha/reader.py
Reader._check_underflow
def _check_underflow(self, n): ''' Raise BufferUnderflow if there's not enough bytes to satisfy the request. ''' if self._pos + n > self._end_pos: raise self.BufferUnderflow()
python
def _check_underflow(self, n): ''' Raise BufferUnderflow if there's not enough bytes to satisfy the request. ''' if self._pos + n > self._end_pos: raise self.BufferUnderflow()
[ "def", "_check_underflow", "(", "self", ",", "n", ")", ":", "if", "self", ".", "_pos", "+", "n", ">", "self", ".", "_end_pos", ":", "raise", "self", ".", "BufferUnderflow", "(", ")" ]
Raise BufferUnderflow if there's not enough bytes to satisfy the request.
[ "Raise", "BufferUnderflow", "if", "there", "s", "not", "enough", "bytes", "to", "satisfy", "the", "request", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/reader.py#L76-L82
7,868
agoragames/haigha
haigha/reader.py
Reader.buffer
def buffer(self): ''' Get a copy of the buffer that this is reading from. Returns a buffer object ''' return buffer(self._input, self._start_pos, (self._end_pos - self._start_pos))
python
def buffer(self): ''' Get a copy of the buffer that this is reading from. Returns a buffer object ''' return buffer(self._input, self._start_pos, (self._end_pos - self._start_pos))
[ "def", "buffer", "(", "self", ")", ":", "return", "buffer", "(", "self", ".", "_input", ",", "self", ".", "_start_pos", ",", "(", "self", ".", "_end_pos", "-", "self", ".", "_start_pos", ")", ")" ]
Get a copy of the buffer that this is reading from. Returns a buffer object
[ "Get", "a", "copy", "of", "the", "buffer", "that", "this", "is", "reading", "from", ".", "Returns", "a", "buffer", "object" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/reader.py#L90-L96
7,869
agoragames/haigha
haigha/reader.py
Reader.read_bit
def read_bit(self): """ Read a single boolean value, returns 0 or 1. Convience for single bit fields. Will raise BufferUnderflow if there's not enough bytes in the buffer. """ # Perform a faster check on underflow if self._pos >= self._end_pos: raise self.BufferUnderflow() result = ord(self._input[self._pos]) & 1 self._pos += 1 return result
python
def read_bit(self): # Perform a faster check on underflow if self._pos >= self._end_pos: raise self.BufferUnderflow() result = ord(self._input[self._pos]) & 1 self._pos += 1 return result
[ "def", "read_bit", "(", "self", ")", ":", "# Perform a faster check on underflow", "if", "self", ".", "_pos", ">=", "self", ".", "_end_pos", ":", "raise", "self", ".", "BufferUnderflow", "(", ")", "result", "=", "ord", "(", "self", ".", "_input", "[", "sel...
Read a single boolean value, returns 0 or 1. Convience for single bit fields. Will raise BufferUnderflow if there's not enough bytes in the buffer.
[ "Read", "a", "single", "boolean", "value", "returns", "0", "or", "1", ".", "Convience", "for", "single", "bit", "fields", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/reader.py#L109-L121
7,870
agoragames/haigha
haigha/reader.py
Reader.read_bits
def read_bits(self, num): ''' Read several bits packed into the same field. Will return as a list. The bit field itself is little-endian, though the order of the returned array looks big-endian for ease of decomposition. Reader('\x02').read_bits(2) -> [False,True] Reader('\x08').read_bits(2) -> [False,True,False,False,False,False,False,False] first_field, second_field = Reader('\x02').read_bits(2) Will raise BufferUnderflow if there's not enough bytes in the buffer. Will raise ValueError if num < 0 or num > 9 ''' # Perform a faster check on underflow if self._pos >= self._end_pos: raise self.BufferUnderflow() if num < 0 or num >= 9: raise ValueError("8 bits per field") field = ord(self._input[self._pos]) result = map(lambda x: field >> x & 1, xrange(num)) self._pos += 1 return result
python
def read_bits(self, num): ''' Read several bits packed into the same field. Will return as a list. The bit field itself is little-endian, though the order of the returned array looks big-endian for ease of decomposition. Reader('\x02').read_bits(2) -> [False,True] Reader('\x08').read_bits(2) -> [False,True,False,False,False,False,False,False] first_field, second_field = Reader('\x02').read_bits(2) Will raise BufferUnderflow if there's not enough bytes in the buffer. Will raise ValueError if num < 0 or num > 9 ''' # Perform a faster check on underflow if self._pos >= self._end_pos: raise self.BufferUnderflow() if num < 0 or num >= 9: raise ValueError("8 bits per field") field = ord(self._input[self._pos]) result = map(lambda x: field >> x & 1, xrange(num)) self._pos += 1 return result
[ "def", "read_bits", "(", "self", ",", "num", ")", ":", "# Perform a faster check on underflow", "if", "self", ".", "_pos", ">=", "self", ".", "_end_pos", ":", "raise", "self", ".", "BufferUnderflow", "(", ")", "if", "num", "<", "0", "or", "num", ">=", "9...
Read several bits packed into the same field. Will return as a list. The bit field itself is little-endian, though the order of the returned array looks big-endian for ease of decomposition. Reader('\x02').read_bits(2) -> [False,True] Reader('\x08').read_bits(2) -> [False,True,False,False,False,False,False,False] first_field, second_field = Reader('\x02').read_bits(2) Will raise BufferUnderflow if there's not enough bytes in the buffer. Will raise ValueError if num < 0 or num > 9
[ "Read", "several", "bits", "packed", "into", "the", "same", "field", ".", "Will", "return", "as", "a", "list", ".", "The", "bit", "field", "itself", "is", "little", "-", "endian", "though", "the", "order", "of", "the", "returned", "array", "looks", "big"...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/reader.py#L123-L145
7,871
agoragames/haigha
haigha/reader.py
Reader._read_field
def _read_field(self): ''' Read a single byte for field type, then read the value. ''' ftype = self._input[self._pos] self._pos += 1 reader = self.field_type_map.get(ftype) if reader: return reader(self) raise Reader.FieldError('Unknown field type %s', ftype)
python
def _read_field(self): ''' Read a single byte for field type, then read the value. ''' ftype = self._input[self._pos] self._pos += 1 reader = self.field_type_map.get(ftype) if reader: return reader(self) raise Reader.FieldError('Unknown field type %s', ftype)
[ "def", "_read_field", "(", "self", ")", ":", "ftype", "=", "self", ".", "_input", "[", "self", ".", "_pos", "]", "self", ".", "_pos", "+=", "1", "reader", "=", "self", ".", "field_type_map", ".", "get", "(", "ftype", ")", "if", "reader", ":", "retu...
Read a single byte for field type, then read the value.
[ "Read", "a", "single", "byte", "for", "field", "type", "then", "read", "the", "value", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/reader.py#L255-L266
7,872
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass.open
def open(self): ''' Open the channel for communication. ''' args = Writer() args.write_shortstr('') self.send_frame(MethodFrame(self.channel_id, 20, 10, args)) self.channel.add_synchronous_cb(self._recv_open_ok)
python
def open(self): ''' Open the channel for communication. ''' args = Writer() args.write_shortstr('') self.send_frame(MethodFrame(self.channel_id, 20, 10, args)) self.channel.add_synchronous_cb(self._recv_open_ok)
[ "def", "open", "(", "self", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_shortstr", "(", "''", ")", "self", ".", "send_frame", "(", "MethodFrame", "(", "self", ".", "channel_id", ",", "20", ",", "10", ",", "args", ")", ")", "self"...
Open the channel for communication.
[ "Open", "the", "channel", "for", "communication", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L47-L54
7,873
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass._send_flow
def _send_flow(self, active): ''' Send a flow control command. ''' args = Writer() args.write_bit(active) self.send_frame(MethodFrame(self.channel_id, 20, 20, args)) self.channel.add_synchronous_cb(self._recv_flow_ok)
python
def _send_flow(self, active): ''' Send a flow control command. ''' args = Writer() args.write_bit(active) self.send_frame(MethodFrame(self.channel_id, 20, 20, args)) self.channel.add_synchronous_cb(self._recv_flow_ok)
[ "def", "_send_flow", "(", "self", ",", "active", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_bit", "(", "active", ")", "self", ".", "send_frame", "(", "MethodFrame", "(", "self", ".", "channel_id", ",", "20", ",", "20", ",", "args"...
Send a flow control command.
[ "Send", "a", "flow", "control", "command", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L76-L83
7,874
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass._recv_flow
def _recv_flow(self, method_frame): ''' Receive a flow control command from the broker ''' self.channel._active = method_frame.args.read_bit() args = Writer() args.write_bit(self.channel.active) self.send_frame(MethodFrame(self.channel_id, 20, 21, args)) if self._flow_control_cb is not None: self._flow_control_cb()
python
def _recv_flow(self, method_frame): ''' Receive a flow control command from the broker ''' self.channel._active = method_frame.args.read_bit() args = Writer() args.write_bit(self.channel.active) self.send_frame(MethodFrame(self.channel_id, 20, 21, args)) if self._flow_control_cb is not None: self._flow_control_cb()
[ "def", "_recv_flow", "(", "self", ",", "method_frame", ")", ":", "self", ".", "channel", ".", "_active", "=", "method_frame", ".", "args", ".", "read_bit", "(", ")", "args", "=", "Writer", "(", ")", "args", ".", "write_bit", "(", "self", ".", "channel"...
Receive a flow control command from the broker
[ "Receive", "a", "flow", "control", "command", "from", "the", "broker" ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L85-L96
7,875
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass._recv_flow_ok
def _recv_flow_ok(self, method_frame): ''' Receive a flow control ack from the broker. ''' self.channel._active = method_frame.args.read_bit() if self._flow_control_cb is not None: self._flow_control_cb()
python
def _recv_flow_ok(self, method_frame): ''' Receive a flow control ack from the broker. ''' self.channel._active = method_frame.args.read_bit() if self._flow_control_cb is not None: self._flow_control_cb()
[ "def", "_recv_flow_ok", "(", "self", ",", "method_frame", ")", ":", "self", ".", "channel", ".", "_active", "=", "method_frame", ".", "args", ".", "read_bit", "(", ")", "if", "self", ".", "_flow_control_cb", "is", "not", "None", ":", "self", ".", "_flow_...
Receive a flow control ack from the broker.
[ "Receive", "a", "flow", "control", "ack", "from", "the", "broker", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L98-L104
7,876
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass.close
def close(self, reply_code=0, reply_text='', class_id=0, method_id=0): ''' Close this channel. Caller has the option of specifying the reason for closure and the class and method ids of the current frame in which an error occurred. If in the event of an exception, the channel will be marked as immediately closed. If channel is already closed, call is ignored. ''' if not getattr(self, 'channel', None) or self.channel._closed: return self.channel._close_info = { 'reply_code': reply_code, 'reply_text': reply_text, 'class_id': class_id, 'method_id': method_id } # exceptions here likely due to race condition as connection is closing # cap the reply_text we send because it may be arbitrarily long try: args = Writer() args.write_short(reply_code) args.write_shortstr(reply_text[:255]) args.write_short(class_id) args.write_short(method_id) self.send_frame(MethodFrame(self.channel_id, 20, 40, args)) self.channel.add_synchronous_cb(self._recv_close_ok) finally: # Immediately set the closed flag so no more frames can be sent # NOTE: in synchronous mode, by the time this is called we will # have already run self.channel._closed_cb and so the channel # reference is gone. if self.channel: self.channel._closed = True
python
def close(self, reply_code=0, reply_text='', class_id=0, method_id=0): ''' Close this channel. Caller has the option of specifying the reason for closure and the class and method ids of the current frame in which an error occurred. If in the event of an exception, the channel will be marked as immediately closed. If channel is already closed, call is ignored. ''' if not getattr(self, 'channel', None) or self.channel._closed: return self.channel._close_info = { 'reply_code': reply_code, 'reply_text': reply_text, 'class_id': class_id, 'method_id': method_id } # exceptions here likely due to race condition as connection is closing # cap the reply_text we send because it may be arbitrarily long try: args = Writer() args.write_short(reply_code) args.write_shortstr(reply_text[:255]) args.write_short(class_id) args.write_short(method_id) self.send_frame(MethodFrame(self.channel_id, 20, 40, args)) self.channel.add_synchronous_cb(self._recv_close_ok) finally: # Immediately set the closed flag so no more frames can be sent # NOTE: in synchronous mode, by the time this is called we will # have already run self.channel._closed_cb and so the channel # reference is gone. if self.channel: self.channel._closed = True
[ "def", "close", "(", "self", ",", "reply_code", "=", "0", ",", "reply_text", "=", "''", ",", "class_id", "=", "0", ",", "method_id", "=", "0", ")", ":", "if", "not", "getattr", "(", "self", ",", "'channel'", ",", "None", ")", "or", "self", ".", "...
Close this channel. Caller has the option of specifying the reason for closure and the class and method ids of the current frame in which an error occurred. If in the event of an exception, the channel will be marked as immediately closed. If channel is already closed, call is ignored.
[ "Close", "this", "channel", ".", "Caller", "has", "the", "option", "of", "specifying", "the", "reason", "for", "closure", "and", "the", "class", "and", "method", "ids", "of", "the", "current", "frame", "in", "which", "an", "error", "occurred", ".", "If", ...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L106-L141
7,877
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass._recv_close
def _recv_close(self, method_frame): ''' Receive a close command from the broker. ''' self.channel._close_info = { 'reply_code': method_frame.args.read_short(), 'reply_text': method_frame.args.read_shortstr(), 'class_id': method_frame.args.read_short(), 'method_id': method_frame.args.read_short() } self.channel._closed = True self.channel._closed_cb( final_frame=MethodFrame(self.channel_id, 20, 41))
python
def _recv_close(self, method_frame): ''' Receive a close command from the broker. ''' self.channel._close_info = { 'reply_code': method_frame.args.read_short(), 'reply_text': method_frame.args.read_shortstr(), 'class_id': method_frame.args.read_short(), 'method_id': method_frame.args.read_short() } self.channel._closed = True self.channel._closed_cb( final_frame=MethodFrame(self.channel_id, 20, 41))
[ "def", "_recv_close", "(", "self", ",", "method_frame", ")", ":", "self", ".", "channel", ".", "_close_info", "=", "{", "'reply_code'", ":", "method_frame", ".", "args", ".", "read_short", "(", ")", ",", "'reply_text'", ":", "method_frame", ".", "args", "....
Receive a close command from the broker.
[ "Receive", "a", "close", "command", "from", "the", "broker", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L143-L156
7,878
agoragames/haigha
haigha/classes/channel_class.py
ChannelClass._recv_close_ok
def _recv_close_ok(self, method_frame): ''' Receive a close ack from the broker. ''' self.channel._closed = True self.channel._closed_cb()
python
def _recv_close_ok(self, method_frame): ''' Receive a close ack from the broker. ''' self.channel._closed = True self.channel._closed_cb()
[ "def", "_recv_close_ok", "(", "self", ",", "method_frame", ")", ":", "self", ".", "channel", ".", "_closed", "=", "True", "self", ".", "channel", ".", "_closed_cb", "(", ")" ]
Receive a close ack from the broker.
[ "Receive", "a", "close", "ack", "from", "the", "broker", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/channel_class.py#L158-L163
7,879
agoragames/haigha
haigha/classes/queue_class.py
QueueClass.bind
def bind(self, queue, exchange, routing_key='', nowait=True, arguments={}, ticket=None, cb=None): ''' bind to a queue. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_shortstr(exchange).\ write_shortstr(routing_key).\ write_bit(nowait).\ write_table(arguments) self.send_frame(MethodFrame(self.channel_id, 50, 20, args)) if not nowait: self._bind_cb.append(cb) self.channel.add_synchronous_cb(self._recv_bind_ok)
python
def bind(self, queue, exchange, routing_key='', nowait=True, arguments={}, ticket=None, cb=None): ''' bind to a queue. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_shortstr(exchange).\ write_shortstr(routing_key).\ write_bit(nowait).\ write_table(arguments) self.send_frame(MethodFrame(self.channel_id, 50, 20, args)) if not nowait: self._bind_cb.append(cb) self.channel.add_synchronous_cb(self._recv_bind_ok)
[ "def", "bind", "(", "self", ",", "queue", ",", "exchange", ",", "routing_key", "=", "''", ",", "nowait", "=", "True", ",", "arguments", "=", "{", "}", ",", "ticket", "=", "None", ",", "cb", "=", "None", ")", ":", "nowait", "=", "nowait", "and", "...
bind to a queue.
[ "bind", "to", "a", "queue", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/queue_class.py#L86-L104
7,880
agoragames/haigha
haigha/classes/queue_class.py
QueueClass.unbind
def unbind(self, queue, exchange, routing_key='', arguments={}, ticket=None, cb=None): ''' Unbind a queue from an exchange. This is always synchronous. ''' args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_shortstr(exchange).\ write_shortstr(routing_key).\ write_table(arguments) self.send_frame(MethodFrame(self.channel_id, 50, 50, args)) self._unbind_cb.append(cb) self.channel.add_synchronous_cb(self._recv_unbind_ok)
python
def unbind(self, queue, exchange, routing_key='', arguments={}, ticket=None, cb=None): ''' Unbind a queue from an exchange. This is always synchronous. ''' args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_shortstr(exchange).\ write_shortstr(routing_key).\ write_table(arguments) self.send_frame(MethodFrame(self.channel_id, 50, 50, args)) self._unbind_cb.append(cb) self.channel.add_synchronous_cb(self._recv_unbind_ok)
[ "def", "unbind", "(", "self", ",", "queue", ",", "exchange", ",", "routing_key", "=", "''", ",", "arguments", "=", "{", "}", ",", "ticket", "=", "None", ",", "cb", "=", "None", ")", ":", "args", "=", "Writer", "(", ")", "args", ".", "write_short", ...
Unbind a queue from an exchange. This is always synchronous.
[ "Unbind", "a", "queue", "from", "an", "exchange", ".", "This", "is", "always", "synchronous", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/queue_class.py#L112-L126
7,881
agoragames/haigha
haigha/classes/queue_class.py
QueueClass.purge
def purge(self, queue, nowait=True, ticket=None, cb=None): ''' Purge all messages in a queue. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 50, 30, args)) if not nowait: self._purge_cb.append(cb) return self.channel.add_synchronous_cb(self._recv_purge_ok)
python
def purge(self, queue, nowait=True, ticket=None, cb=None): ''' Purge all messages in a queue. ''' nowait = nowait and self.allow_nowait() and not cb args = Writer() args.write_short(ticket or self.default_ticket).\ write_shortstr(queue).\ write_bit(nowait) self.send_frame(MethodFrame(self.channel_id, 50, 30, args)) if not nowait: self._purge_cb.append(cb) return self.channel.add_synchronous_cb(self._recv_purge_ok)
[ "def", "purge", "(", "self", ",", "queue", ",", "nowait", "=", "True", ",", "ticket", "=", "None", ",", "cb", "=", "None", ")", ":", "nowait", "=", "nowait", "and", "self", ".", "allow_nowait", "(", ")", "and", "not", "cb", "args", "=", "Writer", ...
Purge all messages in a queue.
[ "Purge", "all", "messages", "in", "a", "queue", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/classes/queue_class.py#L134-L148
7,882
agoragames/haigha
haigha/writer.py
Writer.write_bits
def write_bits(self, *args): ''' Write multiple bits in a single byte field. The bits will be written in little-endian order, but should be supplied in big endian order. Will raise ValueError when more than 8 arguments are supplied. write_bits(True, False) => 0x02 ''' # Would be nice to make this a bit smarter if len(args) > 8: raise ValueError("Can only write 8 bits at a time") self._output_buffer.append(chr( reduce(lambda x, y: xor(x, args[y] << y), xrange(len(args)), 0))) return self
python
def write_bits(self, *args): ''' Write multiple bits in a single byte field. The bits will be written in little-endian order, but should be supplied in big endian order. Will raise ValueError when more than 8 arguments are supplied. write_bits(True, False) => 0x02 ''' # Would be nice to make this a bit smarter if len(args) > 8: raise ValueError("Can only write 8 bits at a time") self._output_buffer.append(chr( reduce(lambda x, y: xor(x, args[y] << y), xrange(len(args)), 0))) return self
[ "def", "write_bits", "(", "self", ",", "*", "args", ")", ":", "# Would be nice to make this a bit smarter", "if", "len", "(", "args", ")", ">", "8", ":", "raise", "ValueError", "(", "\"Can only write 8 bits at a time\"", ")", "self", ".", "_output_buffer", ".", ...
Write multiple bits in a single byte field. The bits will be written in little-endian order, but should be supplied in big endian order. Will raise ValueError when more than 8 arguments are supplied. write_bits(True, False) => 0x02
[ "Write", "multiple", "bits", "in", "a", "single", "byte", "field", ".", "The", "bits", "will", "be", "written", "in", "little", "-", "endian", "order", "but", "should", "be", "supplied", "in", "big", "endian", "order", ".", "Will", "raise", "ValueError", ...
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L52-L67
7,883
agoragames/haigha
haigha/writer.py
Writer.write_bit
def write_bit(self, b, pack=Struct('B').pack): ''' Write a single bit. Convenience method for single bit args. ''' self._output_buffer.append(pack(True if b else False)) return self
python
def write_bit(self, b, pack=Struct('B').pack): ''' Write a single bit. Convenience method for single bit args. ''' self._output_buffer.append(pack(True if b else False)) return self
[ "def", "write_bit", "(", "self", ",", "b", ",", "pack", "=", "Struct", "(", "'B'", ")", ".", "pack", ")", ":", "self", ".", "_output_buffer", ".", "append", "(", "pack", "(", "True", "if", "b", "else", "False", ")", ")", "return", "self" ]
Write a single bit. Convenience method for single bit args.
[ "Write", "a", "single", "bit", ".", "Convenience", "method", "for", "single", "bit", "args", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L69-L74
7,884
agoragames/haigha
haigha/writer.py
Writer.write_short_at
def write_short_at(self, n, pos, pack_into=Struct('>H').pack_into): ''' Write an unsigned 16bit value at a specific position in the buffer. Used for writing tables and frames. ''' if 0 <= n <= 0xFFFF: pack_into(self._output_buffer, pos, n) else: raise ValueError('Short %d out of range 0..0xFFFF', n) return self
python
def write_short_at(self, n, pos, pack_into=Struct('>H').pack_into): ''' Write an unsigned 16bit value at a specific position in the buffer. Used for writing tables and frames. ''' if 0 <= n <= 0xFFFF: pack_into(self._output_buffer, pos, n) else: raise ValueError('Short %d out of range 0..0xFFFF', n) return self
[ "def", "write_short_at", "(", "self", ",", "n", ",", "pos", ",", "pack_into", "=", "Struct", "(", "'>H'", ")", ".", "pack_into", ")", ":", "if", "0", "<=", "n", "<=", "0xFFFF", ":", "pack_into", "(", "self", ".", "_output_buffer", ",", "pos", ",", ...
Write an unsigned 16bit value at a specific position in the buffer. Used for writing tables and frames.
[ "Write", "an", "unsigned", "16bit", "value", "at", "a", "specific", "position", "in", "the", "buffer", ".", "Used", "for", "writing", "tables", "and", "frames", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L96-L105
7,885
agoragames/haigha
haigha/writer.py
Writer.write_long
def write_long(self, n, pack=Struct('>I').pack): """ Write an integer as an unsigned 32-bit value. """ if 0 <= n <= 0xFFFFFFFF: self._output_buffer.extend(pack(n)) else: raise ValueError('Long %d out of range 0..0xFFFFFFFF', n) return self
python
def write_long(self, n, pack=Struct('>I').pack): if 0 <= n <= 0xFFFFFFFF: self._output_buffer.extend(pack(n)) else: raise ValueError('Long %d out of range 0..0xFFFFFFFF', n) return self
[ "def", "write_long", "(", "self", ",", "n", ",", "pack", "=", "Struct", "(", "'>I'", ")", ".", "pack", ")", ":", "if", "0", "<=", "n", "<=", "0xFFFFFFFF", ":", "self", ".", "_output_buffer", ".", "extend", "(", "pack", "(", "n", ")", ")", "else",...
Write an integer as an unsigned 32-bit value.
[ "Write", "an", "integer", "as", "an", "unsigned", "32", "-", "bit", "value", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L107-L115
7,886
agoragames/haigha
haigha/writer.py
Writer.write_long_at
def write_long_at(self, n, pos, pack_into=Struct('>I').pack_into): ''' Write an unsigned 32bit value at a specific position in the buffer. Used for writing tables and frames. ''' if 0 <= n <= 0xFFFFFFFF: pack_into(self._output_buffer, pos, n) else: raise ValueError('Long %d out of range 0..0xFFFFFFFF', n) return self
python
def write_long_at(self, n, pos, pack_into=Struct('>I').pack_into): ''' Write an unsigned 32bit value at a specific position in the buffer. Used for writing tables and frames. ''' if 0 <= n <= 0xFFFFFFFF: pack_into(self._output_buffer, pos, n) else: raise ValueError('Long %d out of range 0..0xFFFFFFFF', n) return self
[ "def", "write_long_at", "(", "self", ",", "n", ",", "pos", ",", "pack_into", "=", "Struct", "(", "'>I'", ")", ".", "pack_into", ")", ":", "if", "0", "<=", "n", "<=", "0xFFFFFFFF", ":", "pack_into", "(", "self", ".", "_output_buffer", ",", "pos", ",",...
Write an unsigned 32bit value at a specific position in the buffer. Used for writing tables and frames.
[ "Write", "an", "unsigned", "32bit", "value", "at", "a", "specific", "position", "in", "the", "buffer", ".", "Used", "for", "writing", "tables", "and", "frames", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L117-L126
7,887
agoragames/haigha
haigha/writer.py
Writer.write_shortstr
def write_shortstr(self, s): """ Write a string up to 255 bytes long after encoding. If passed a unicode string, encode as UTF-8. """ if isinstance(s, unicode): s = s.encode('utf-8') self.write_octet(len(s)) self.write(s) return self
python
def write_shortstr(self, s): if isinstance(s, unicode): s = s.encode('utf-8') self.write_octet(len(s)) self.write(s) return self
[ "def", "write_shortstr", "(", "self", ",", "s", ")", ":", "if", "isinstance", "(", "s", ",", "unicode", ")", ":", "s", "=", "s", ".", "encode", "(", "'utf-8'", ")", "self", ".", "write_octet", "(", "len", "(", "s", ")", ")", "self", ".", "write",...
Write a string up to 255 bytes long after encoding. If passed a unicode string, encode as UTF-8.
[ "Write", "a", "string", "up", "to", "255", "bytes", "long", "after", "encoding", ".", "If", "passed", "a", "unicode", "string", "encode", "as", "UTF", "-", "8", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L139-L148
7,888
agoragames/haigha
haigha/writer.py
Writer.write_timestamp
def write_timestamp(self, t, pack=Struct('>Q').pack): """ Write out a Python datetime.datetime object as a 64-bit integer representing seconds since the Unix UTC epoch. """ # Double check timestamp, can't imagine why it would be signed self._output_buffer.extend(pack(long(timegm(t.timetuple())))) return self
python
def write_timestamp(self, t, pack=Struct('>Q').pack): # Double check timestamp, can't imagine why it would be signed self._output_buffer.extend(pack(long(timegm(t.timetuple())))) return self
[ "def", "write_timestamp", "(", "self", ",", "t", ",", "pack", "=", "Struct", "(", "'>Q'", ")", ".", "pack", ")", ":", "# Double check timestamp, can't imagine why it would be signed", "self", ".", "_output_buffer", ".", "extend", "(", "pack", "(", "long", "(", ...
Write out a Python datetime.datetime object as a 64-bit integer representing seconds since the Unix UTC epoch.
[ "Write", "out", "a", "Python", "datetime", ".", "datetime", "object", "as", "a", "64", "-", "bit", "integer", "representing", "seconds", "since", "the", "Unix", "UTC", "epoch", "." ]
7b004e1c0316ec14b94fec1c54554654c38b1a25
https://github.com/agoragames/haigha/blob/7b004e1c0316ec14b94fec1c54554654c38b1a25/haigha/writer.py#L161-L168
7,889
chrippa/python-librtmp
librtmp/packet.py
RTMPPacket.body
def body(self): """The body of the packet.""" view = ffi.buffer(self.packet.m_body, self.packet.m_nBodySize) return view[:]
python
def body(self): view = ffi.buffer(self.packet.m_body, self.packet.m_nBodySize) return view[:]
[ "def", "body", "(", "self", ")", ":", "view", "=", "ffi", ".", "buffer", "(", "self", ".", "packet", ".", "m_body", ",", "self", ".", "packet", ".", "m_nBodySize", ")", "return", "view", "[", ":", "]" ]
The body of the packet.
[ "The", "body", "of", "the", "packet", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/packet.py#L103-L107
7,890
chrippa/python-librtmp
librtmp/logging.py
add_log_callback
def add_log_callback(callback): """Adds a log callback.""" global _log_callbacks if not callable(callback): raise ValueError("Callback must be callable") _log_callbacks.add(callback) return callback
python
def add_log_callback(callback): global _log_callbacks if not callable(callback): raise ValueError("Callback must be callable") _log_callbacks.add(callback) return callback
[ "def", "add_log_callback", "(", "callback", ")", ":", "global", "_log_callbacks", "if", "not", "callable", "(", "callback", ")", ":", "raise", "ValueError", "(", "\"Callback must be callable\"", ")", "_log_callbacks", ".", "add", "(", "callback", ")", "return", ...
Adds a log callback.
[ "Adds", "a", "log", "callback", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/logging.py#L36-L44
7,891
chrippa/python-librtmp
librtmp/stream.py
RTMPStream.read
def read(self, size): """Attempts to read data from the stream. :param size: int, The maximum amount of bytes to read. Raises :exc:`IOError` on error. """ # If enabled tell the server that our buffer can fit the whole # stream, this often increases throughput alot. if self._update_buffer and not self._updated_buffer and self.duration: self.update_buffer((self.duration * 1000) + 5000) self._updated_buffer = True if not self._buf or len(self._buf) != size: self._buf = ffi.new("char[]", size) self._view = ffi.buffer(self._buf, size) res = librtmp.RTMP_Read(self.client.rtmp, self._buf, size) if res < 0: raise IOError("Failed to read data") return self._view[:res]
python
def read(self, size): # If enabled tell the server that our buffer can fit the whole # stream, this often increases throughput alot. if self._update_buffer and not self._updated_buffer and self.duration: self.update_buffer((self.duration * 1000) + 5000) self._updated_buffer = True if not self._buf or len(self._buf) != size: self._buf = ffi.new("char[]", size) self._view = ffi.buffer(self._buf, size) res = librtmp.RTMP_Read(self.client.rtmp, self._buf, size) if res < 0: raise IOError("Failed to read data") return self._view[:res]
[ "def", "read", "(", "self", ",", "size", ")", ":", "# If enabled tell the server that our buffer can fit the whole", "# stream, this often increases throughput alot.", "if", "self", ".", "_update_buffer", "and", "not", "self", ".", "_updated_buffer", "and", "self", ".", "...
Attempts to read data from the stream. :param size: int, The maximum amount of bytes to read. Raises :exc:`IOError` on error.
[ "Attempts", "to", "read", "data", "from", "the", "stream", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/stream.py#L21-L43
7,892
chrippa/python-librtmp
librtmp/stream.py
RTMPStream.write
def write(self, data): """Writes data to the stream. :param data: bytes, FLV data to write to the stream The data passed can contain multiple FLV tags, but it MUST always contain complete tags or undefined behaviour might occur. Raises :exc:`IOError` on error. """ if isinstance(data, bytearray): data = bytes(data) if not isinstance(data, byte_types): raise ValueError("A bytes argument is required") res = librtmp.RTMP_Write(self.client.rtmp, data, len(data)) if res < 0: raise IOError("Failed to write data") return res
python
def write(self, data): if isinstance(data, bytearray): data = bytes(data) if not isinstance(data, byte_types): raise ValueError("A bytes argument is required") res = librtmp.RTMP_Write(self.client.rtmp, data, len(data)) if res < 0: raise IOError("Failed to write data") return res
[ "def", "write", "(", "self", ",", "data", ")", ":", "if", "isinstance", "(", "data", ",", "bytearray", ")", ":", "data", "=", "bytes", "(", "data", ")", "if", "not", "isinstance", "(", "data", ",", "byte_types", ")", ":", "raise", "ValueError", "(", ...
Writes data to the stream. :param data: bytes, FLV data to write to the stream The data passed can contain multiple FLV tags, but it MUST always contain complete tags or undefined behaviour might occur. Raises :exc:`IOError` on error.
[ "Writes", "data", "to", "the", "stream", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/stream.py#L45-L67
7,893
chrippa/python-librtmp
librtmp/stream.py
RTMPStream.pause
def pause(self): """Pauses the stream.""" res = librtmp.RTMP_Pause(self.client.rtmp, 1) if res < 1: raise RTMPError("Failed to pause")
python
def pause(self): res = librtmp.RTMP_Pause(self.client.rtmp, 1) if res < 1: raise RTMPError("Failed to pause")
[ "def", "pause", "(", "self", ")", ":", "res", "=", "librtmp", ".", "RTMP_Pause", "(", "self", ".", "client", ".", "rtmp", ",", "1", ")", "if", "res", "<", "1", ":", "raise", "RTMPError", "(", "\"Failed to pause\"", ")" ]
Pauses the stream.
[ "Pauses", "the", "stream", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/stream.py#L69-L74
7,894
chrippa/python-librtmp
librtmp/stream.py
RTMPStream.unpause
def unpause(self): """Unpauses the stream.""" res = librtmp.RTMP_Pause(self.client.rtmp, 0) if res < 1: raise RTMPError("Failed to unpause")
python
def unpause(self): res = librtmp.RTMP_Pause(self.client.rtmp, 0) if res < 1: raise RTMPError("Failed to unpause")
[ "def", "unpause", "(", "self", ")", ":", "res", "=", "librtmp", ".", "RTMP_Pause", "(", "self", ".", "client", ".", "rtmp", ",", "0", ")", "if", "res", "<", "1", ":", "raise", "RTMPError", "(", "\"Failed to unpause\"", ")" ]
Unpauses the stream.
[ "Unpauses", "the", "stream", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/stream.py#L76-L81
7,895
chrippa/python-librtmp
librtmp/stream.py
RTMPStream.seek
def seek(self, time): """Attempts to seek in the stream. :param time: int, Time to seek to in seconds """ res = librtmp.RTMP_SendSeek(self.client.rtmp, time) if res < 1: raise RTMPError("Failed to seek")
python
def seek(self, time): res = librtmp.RTMP_SendSeek(self.client.rtmp, time) if res < 1: raise RTMPError("Failed to seek")
[ "def", "seek", "(", "self", ",", "time", ")", ":", "res", "=", "librtmp", ".", "RTMP_SendSeek", "(", "self", ".", "client", ".", "rtmp", ",", "time", ")", "if", "res", "<", "1", ":", "raise", "RTMPError", "(", "\"Failed to seek\"", ")" ]
Attempts to seek in the stream. :param time: int, Time to seek to in seconds
[ "Attempts", "to", "seek", "in", "the", "stream", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/stream.py#L83-L92
7,896
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.set_option
def set_option(self, key, value): """Sets a option for this session. For a detailed list of available options see the librtmp(3) man page. :param key: str, A valid option key. :param value: A value, anything that can be converted to str is valid. Raises :exc:`ValueError` if a invalid option is specified. """ akey = AVal(key) aval = AVal(value) res = librtmp.RTMP_SetOpt(self.rtmp, akey.aval, aval.aval) if res < 1: raise ValueError("Unable to set option {0}".format(key)) self._options[akey] = aval
python
def set_option(self, key, value): akey = AVal(key) aval = AVal(value) res = librtmp.RTMP_SetOpt(self.rtmp, akey.aval, aval.aval) if res < 1: raise ValueError("Unable to set option {0}".format(key)) self._options[akey] = aval
[ "def", "set_option", "(", "self", ",", "key", ",", "value", ")", ":", "akey", "=", "AVal", "(", "key", ")", "aval", "=", "AVal", "(", "value", ")", "res", "=", "librtmp", ".", "RTMP_SetOpt", "(", "self", ".", "rtmp", ",", "akey", ".", "aval", ","...
Sets a option for this session. For a detailed list of available options see the librtmp(3) man page. :param key: str, A valid option key. :param value: A value, anything that can be converted to str is valid. Raises :exc:`ValueError` if a invalid option is specified.
[ "Sets", "a", "option", "for", "this", "session", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L128-L148
7,897
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.setup_url
def setup_url(self, url): r"""Attempt to parse a RTMP URL. Additional options may be specified by appending space-separated key=value pairs to the URL. Special characters in values may need to be escaped to prevent misinterpretation by the option parser. The escape encoding uses a backslash followed by two hexadecimal digits representing the ASCII value of the character. E.g., spaces must be escaped as `\\20` and backslashes must be escaped as `\\5c`. :param url: str, A RTMP URL in the format `rtmp[t][e|s]://hostname[:port][/app[/playpath]]` Raises :exc:`RTMPError` if URL parsing fails. """ self.url = bytes(url, "utf8") res = librtmp.RTMP_SetupURL(self.rtmp, self.url) if res < 1: raise RTMPError("Unable to parse URL")
python
def setup_url(self, url): r"""Attempt to parse a RTMP URL. Additional options may be specified by appending space-separated key=value pairs to the URL. Special characters in values may need to be escaped to prevent misinterpretation by the option parser. The escape encoding uses a backslash followed by two hexadecimal digits representing the ASCII value of the character. E.g., spaces must be escaped as `\\20` and backslashes must be escaped as `\\5c`. :param url: str, A RTMP URL in the format `rtmp[t][e|s]://hostname[:port][/app[/playpath]]` Raises :exc:`RTMPError` if URL parsing fails. """ self.url = bytes(url, "utf8") res = librtmp.RTMP_SetupURL(self.rtmp, self.url) if res < 1: raise RTMPError("Unable to parse URL")
[ "def", "setup_url", "(", "self", ",", "url", ")", ":", "self", ".", "url", "=", "bytes", "(", "url", ",", "\"utf8\"", ")", "res", "=", "librtmp", ".", "RTMP_SetupURL", "(", "self", ".", "rtmp", ",", "self", ".", "url", ")", "if", "res", "<", "1",...
r"""Attempt to parse a RTMP URL. Additional options may be specified by appending space-separated key=value pairs to the URL. Special characters in values may need to be escaped to prevent misinterpretation by the option parser. The escape encoding uses a backslash followed by two hexadecimal digits representing the ASCII value of the character. E.g., spaces must be escaped as `\\20` and backslashes must be escaped as `\\5c`. :param url: str, A RTMP URL in the format `rtmp[t][e|s]://hostname[:port][/app[/playpath]]` Raises :exc:`RTMPError` if URL parsing fails.
[ "r", "Attempt", "to", "parse", "a", "RTMP", "URL", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L150-L170
7,898
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.read_packet
def read_packet(self): """Reads a RTMP packet from the server. Returns a :class:`RTMPPacket`. Raises :exc:`RTMPError` on error. Raises :exc:`RTMPTimeoutError` on timeout. Usage:: >>> packet = conn.read_packet() >>> packet.body b'packet body ...' """ packet = ffi.new("RTMPPacket*") packet_complete = False while not packet_complete: res = librtmp.RTMP_ReadPacket(self.rtmp, packet) if res < 1: if librtmp.RTMP_IsTimedout(self.rtmp): raise RTMPTimeoutError("Timed out while reading packet") else: raise RTMPError("Failed to read packet") packet_complete = packet.m_nBytesRead == packet.m_nBodySize return RTMPPacket._from_pointer(packet)
python
def read_packet(self): packet = ffi.new("RTMPPacket*") packet_complete = False while not packet_complete: res = librtmp.RTMP_ReadPacket(self.rtmp, packet) if res < 1: if librtmp.RTMP_IsTimedout(self.rtmp): raise RTMPTimeoutError("Timed out while reading packet") else: raise RTMPError("Failed to read packet") packet_complete = packet.m_nBytesRead == packet.m_nBodySize return RTMPPacket._from_pointer(packet)
[ "def", "read_packet", "(", "self", ")", ":", "packet", "=", "ffi", ".", "new", "(", "\"RTMPPacket*\"", ")", "packet_complete", "=", "False", "while", "not", "packet_complete", ":", "res", "=", "librtmp", ".", "RTMP_ReadPacket", "(", "self", ".", "rtmp", ",...
Reads a RTMP packet from the server. Returns a :class:`RTMPPacket`. Raises :exc:`RTMPError` on error. Raises :exc:`RTMPTimeoutError` on timeout. Usage:: >>> packet = conn.read_packet() >>> packet.body b'packet body ...'
[ "Reads", "a", "RTMP", "packet", "from", "the", "server", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L242-L271
7,899
chrippa/python-librtmp
librtmp/rtmp.py
RTMP.send_packet
def send_packet(self, packet, queue=True): """Sends a RTMP packet to the server. :param packet: RTMPPacket, the packet to send to the server. :param queue: bool, If True, queue up the packet in a internal queue rather than sending it right away. """ if not isinstance(packet, RTMPPacket): raise ValueError("A RTMPPacket argument is required") return librtmp.RTMP_SendPacket(self.rtmp, packet.packet, int(queue))
python
def send_packet(self, packet, queue=True): if not isinstance(packet, RTMPPacket): raise ValueError("A RTMPPacket argument is required") return librtmp.RTMP_SendPacket(self.rtmp, packet.packet, int(queue))
[ "def", "send_packet", "(", "self", ",", "packet", ",", "queue", "=", "True", ")", ":", "if", "not", "isinstance", "(", "packet", ",", "RTMPPacket", ")", ":", "raise", "ValueError", "(", "\"A RTMPPacket argument is required\"", ")", "return", "librtmp", ".", ...
Sends a RTMP packet to the server. :param packet: RTMPPacket, the packet to send to the server. :param queue: bool, If True, queue up the packet in a internal queue rather than sending it right away.
[ "Sends", "a", "RTMP", "packet", "to", "the", "server", "." ]
6efefd5edd76cad7a3b53f7c87c1c7350448224d
https://github.com/chrippa/python-librtmp/blob/6efefd5edd76cad7a3b53f7c87c1c7350448224d/librtmp/rtmp.py#L273-L286