B
     9-eÏ  ã               @   sx   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
Z
ddlmZ e e¡ZG dd„ dƒZdS )	zÊ
Copyright (C) 2019 Interactive Brokers LLC. All rights reserved. This code is subject to the terms
 and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable.
é    N)ÚFAIL_CREATE_SOCK)ÚCONNECT_FAIL)ÚNO_VALID_ID)Úexitc               @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )Ú
Connectionc             C   sN   || _ || _d | _d | _t ¡ | _| j s6tt› dƒ | jsJtt› dƒ d S )Nz::connection host=Nonez::connection port=None)	ÚhostÚportÚsocketÚwrapperÚ	threadingÚLockÚlockr   Ú__name__)Úselfr   r   © r   ú²G:\My Drive\STUDY\EPAT\09 TBP - Trading & Back-testing Platforms\TBP04 - Backtesting & Live Trading\IB IBridgePy API\02 Python 3.7 IBridgePy_Win_Anaconda37_64\ibapi\connection.pyÚ__init__   s    
zConnection.__init__c          
   C   sÄ   yt   ¡ | _ W nT t jk
rb } z4t d|› ¡ | jrR| j tt ¡ t ¡ d¡ W d d }~X Y nX y| j  	| j
| jf¡ W n6 t jk
r²   | jr®| j tt ¡ t ¡ d¡ Y nX | j  d¡ d S )Nzconnect failed. err=Ú é   )r	   ÚerrorÚloggerÚdebugr
   r   r   ÚcodeÚmsgÚconnectr   r   r   Ú
settimeout)r   Úerrr   r   r   r   (   s    , zConnection.connectc             C   s^   | j  ¡  zB| jd k	rJt d¡ | j ¡  d | _t d¡ | jrJ| j ¡  W d | j  ¡  X d S )NÚdisconnectingÚdisconnected)	r   Úacquirer	   r   r   Úcloser
   ÚconnectionClosedÚrelease)r   r   r   r   Ú
disconnect9   s    




zConnection.disconnectc             C   s
   | j d k	S )N)r	   )r   r   r   r   ÚisConnectedF   s    zConnection.isConnectedc          	   C   s°   t  d¡ | j ¡  t  d¡ |  ¡ s>t  d¡ | j ¡  dS z@y| j |¡}W n* tjk
rz   t  dt	 
¡ ¡ d}Y nX W d t  d¡ | j ¡  t  d¡ X t  d|¡ |S )	Nzacquiring lockzacquired lockz5sendMsg attempted while not connected, releasing lockr   zexception from sendMsg %szreleasing lockzrelease lockzsendMsg: sent: %d)r   r   r   r   r$   r"   r	   Úsendr   Ú	tracebackÚ
format_exc)r   r   ZnSentr   r   r   ÚsendMsgI   s"    






zConnection.sendMsgc          
   C   s°   |   ¡ st d¡ dS y|  ¡ }t|ƒdkr,W n| tjk
rX   t dt ¡ ¡ d}Y nT tj	k
rz   t 
d¡ d}Y n2 tk
rª } zt 	d|› ¡ W d d }~X Y nX |S )Nz5recvMsg attempted while not connected, releasing lockó    r   zsocket timeout from recvMsg %szsocket broken, reconnectedz Socket is broken or closed. err=)r$   r   r   Ú_recvAllMsgÚlenr	   Útimeoutr&   r'   r   ÚinfoÚOSError)r   Úbufr   r   r   r   ÚrecvMsg_   s     

"zConnection.recvMsgc             C   s    t  d¡ t d¡ |  ¡  d S )Né   zTry to reconnect socket)ÚtimeÚsleepr   r-   r   )r   r   r   r   Ú
_reconnect{   s    

zConnection._reconnectc             C   sT   d}d}xF|rN|   ¡ rN| j d¡}||7 }t dt|ƒ|¡ t|ƒdk r
d}q
W |S )NTr)   i   zlen %d raw:%s|F)r$   r	   Úrecvr   r   r+   )r   ÚcontZallbufr/   r   r   r   r*   €   s    zConnection._recvAllMsgN)r   Ú
__module__Ú__qualname__r   r   r#   r$   r(   r0   r4   r*   r   r   r   r   r      s   r   )Ú__doc__r&   r	   r   ÚloggingÚibapi.errorsr   r   Úibapi.commonr   r2   Úsysr   Ú	getLoggerr   r   r   r   r   r   r   Ú<module>   s   
