#!/usr/bin/env python
# encoding: utf-8
"""
__init__.py

Created by <NAME> on 2010-01-15.
Copyright (c) 2010-2011 Exa Networks. All rights reserved.
"""

from struct import pack,unpack

# We do not implement the RFC State Machine so .. we do not care :D
class State (object):
	IDLE        = 0x01
	CONNECT     = 0x02
	ACTIVE      = 0x03
	OPENSENT    = 0x04
	OPENCONFIRM = 0x05
	ESTABLISHED = 0x06


# README: the name is HORRIBLE, fix this !!
def defix (data):
	l = unpack('!H',data[0:2])[0]
	return l,data[2:l+2],data[l+2:]

# README: the name is HORRIBLE, fix this !!
def prefix (data):
	return '%s%s' % (pack('!H',len(data)),data)

class Message (Exception):
	TYPE = None

	MARKER = chr(0xff)*16

	class Type:
		OPEN          = 0x01  #   1
		UPDATE        = 0x02  #   2
		NOTIFICATION  = 0x04  #   4
		KEEPALIVE     = 0x08  #   8
		ROUTE_REFRESH = 0x10  #  16
		LIST          = 0x20  #  32
		HEADER        = 0x40  #  64
		GENERAL       = 0x80  # 128
		#LOCALRIB    = 0x100  # 256

	def _message (self,message = ""):
		message_len = pack('!H',19+len(message))
		return "%s%s%s%s" % (self.MARKER,message_len,self.TYPE,message)

	def __str__ (self,code=None):
		if code is None:
			if self.TYPE is None:
				return 'UNKNOWN (invalid code : %s)' % str(code)
			code = ord(self.TYPE)

		result = []
		if code & self.Type.OPEN:
			result.append('OPEN')
		if code & self.Type.UPDATE:
			result.append('UPDATE')
		if code & self.Type.NOTIFICATION:
			result.append('NOTIFICATION')
		if code & self.Type.KEEPALIVE:
			result.append('KEEPALIVE')
		if code & self.Type.ROUTE_REFRESH:
			result.append('ROUTE_REFRESH')
		if code & self.Type.LIST:
			result.append('LIST')
		if code & self.Type.HEADER:
			result.append('HEADER')
		if code & self.Type.GENERAL:
			result.append('GENERAL')
		if result:
			return ' '.join(result)
		return 'UNKNOWN (%d)' % code

class Failure (Exception):
	pass

#!/usr/bin/env python
# encoding: utf-8
"""
__init__.py

Created by <NAME> on 2010-01-15.
Copyright (c) 2010-2011 Exa Networks. All rights reserved.
"""

from struct import pack,unpack

# We do not implement the RFC State Machine so .. we do not care :D
class State (object):
	IDLE        = 0x01
	CONNECT     = 0x02
	ACTIVE      = 0x03
	OPENSENT    = 0x04
	OPENCONFIRM = 0x05
	ESTABLISHED = 0x06


# README: the name is HORRIBLE, fix this !!
def defix (data):
	l = unpack('!H',data[0:2])[0]
	return l,data[2:l+2],data[l+2:]

# README: the name is HORRIBLE, fix this !!
def prefix (data):
	return '%s%s' % (pack('!H',len(data)),data)

class Message (Exception):
	TYPE = None

	MARKER = chr(0xff)*16

	class Type:
		OPEN          = 0x01  #   1
		UPDATE        = 0x02  #   2
		NOTIFICATION  = 0x04  #   4
		KEEPALIVE     = 0x08  #   8
		ROUTE_REFRESH = 0x10  #  16
		LIST          = 0x20  #  32
		HEADER        = 0x40  #  64
		GENERAL       = 0x80  # 128
		#LOCALRIB    = 0x100  # 256

	def _message (self,message = ""):
		message_len = pack('!H',19+len(message))
		return "%s%s%s%s" % (self.MARKER,message_len,self.TYPE,message)

	def __str__ (self,code=None):
		if code is None:
			if self.TYPE is None:
				return 'UNKNOWN (invalid code : %s)' % str(code)
			code = ord(self.TYPE)

		result = []
		if code & self.Type.OPEN:
			result.append('OPEN')
		if code & self.Type.UPDATE:
			result.append('UPDATE')
		if code & self.Type.NOTIFICATION:
			result.append('NOTIFICATION')
		if code & self.Type.KEEPALIVE:
			result.append('KEEPALIVE')
		if code & self.Type.ROUTE_REFRESH:
			result.append('ROUTE_REFRESH')
		if code & self.Type.LIST:
			result.append('LIST')
		if code & self.Type.HEADER:
			result.append('HEADER')
		if code & self.Type.GENERAL:
			result.append('GENERAL')
		if result:
			return ' '.join(result)
		return 'UNKNOWN (%d)' % code

class Failure (Exception):
	pass

#!/usr/bin/env python # encoding: utf-8 __init__.py  Created by <NAME> on 2010-01-15. Copyright (c) 2010-2011 Exa Networks. All rights reserved. # We do not implement the RFC State Machine so .. we do not care :D # README: the name is HORRIBLE, fix this !! # README: the name is HORRIBLE, fix this !! #   1 #   2 #   4 #   8 #  16 #  32 #  64 # 128 #LOCALRIB    = 0x100  # 256