diff --git a/__init__.py b/__init__.py index e08a4ba..7fd1779 100644 --- a/__init__.py +++ b/__init__.py @@ -266,6 +266,18 @@ class pure_json_protocol(object): .. note:: The :const:`channel_name`-exchange will be initiated by the client directly after the the connection is established. * If a channel_name is given at both communication sides and they are different, the client name is taken over and the server will log a warning message. + + **Example:** + + .. literalinclude:: socket_protocol/_examples_/socket_protocol_client.py + + and + + .. literalinclude:: socket_protocol/_examples_/socket_protocol_server.py + + Will result to the following output: + + .. literalinclude:: socket_protocol/_examples_/socket_protocol_client.log """ DEFAULT_CHANNEL_NAME = 'all_others' diff --git a/_docs_/_downloads/37503cb17b21b2c78bb8b07730976f24/unittest.pdf b/_docs_/_downloads/37503cb17b21b2c78bb8b07730976f24/unittest.pdf index 6d98ce9..5adb608 100644 Binary files a/_docs_/_downloads/37503cb17b21b2c78bb8b07730976f24/unittest.pdf and b/_docs_/_downloads/37503cb17b21b2c78bb8b07730976f24/unittest.pdf differ diff --git a/_docs_/index.html b/_docs_/index.html index cc28a21..d9a7b1a 100644 --- a/_docs_/index.html +++ b/_docs_/index.html @@ -471,6 +471,73 @@
  • If a channel_name is given at both communication sides and they are different, the client name is taken over and the server will log a warning message.
  • +

    Example:

    +
    import sys
    +sys.path.append('../..')
    +
    +import report
    +import socket_protocol
    +import tcp_socket
    +import time
    +
    +
    +report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
    +c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
    +sp = socket_protocol.pure_json_protocol(c, channel_name='example_client')
    +sp.send(socket_protocol.SID_READ_REQUEST, 0, None)
    +print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data()))
    +
    +
    +

    and

    +
    import sys
    +sys.path.append('../..')
    +
    +import report
    +import socket_protocol
    +import tcp_socket
    +import time
    +
    +
    +def time_callback(msg):
    +    if msg.get_status() == socket_protocol.STATUS_OKAY:
    +        return socket_protocol.STATUS_OKAY, time.asctime()
    +    else:
    +        return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
    +
    +
    +report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
    +s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
    +sp = socket_protocol.pure_json_protocol(s, channel_name='example_server')
    +sp.register_callback(socket_protocol.SID_READ_REQUEST, 0, time_callback)
    +
    +i = 0
    +while not s.is_connected() and i <= 20:
    +    i += 1
    +    time.sleep(.1)  # wait for a connection
    +
    +i = 0
    +while s.is_connected() and i <= 20:
    +    i += 1
    +    time.sleep(.1)  # wait for disconnect
    +
    +
    +

    Will result to the following output:

    +
    2021-01-11 17:40:12,803: root.socket_protocol.example_client - INFO - prot-client: Resetting authentification state to AUTH_STATE_UNTRUSTED_CONNECTION
    +2021-01-11 17:40:12,803: root.socket_protocol - INFO - prot-client: Initialisation finished.
    +2021-01-11 17:40:12,803: root.socket_protocol.example_client - INFO - prot-client: TX -> service: read data request, data_id: 0, status: okay, data: "None"
    +2021-01-11 17:40:12,954: root.tcp_socket.example_client - INFO - comm-client: Connection established... (to 127.0.0.1:17017)
    +2021-01-11 17:40:12,955: root.socket_protocol.example_client - INFO - prot-client: TX -> service: channel name request, data_id: name, status: okay, data: "'example_client'"
    +2021-01-11 17:40:12,956: root.tcp_socket.example_client - INFO - comm-client: TX -> "(74): 7b 22 64 61 74 61 5f 69 64 22 3a 20 30 2c 20 22 73 65 72 76 69 63 65 5f 69 64 22 3a 20 38 2c 20 22 73 74 61 74 75 73 22 3a 20 30 2c 20 22 64 61 74 61 22 3a 20 22 65 78 61 6d 70 6c 65 5f 63 6c 69 65 6e 74 22 7d f5 cd dd e7"
    +2021-01-11 17:40:13,004: root.tcp_socket.example_client - INFO - comm-client: TX -> "(63): 7b 22 64 61 74 61 5f 69 64 22 3a 20 30 2c 20 22 73 65 72 76 69 63 65 5f 69 64 22 3a 20 31 30 2c 20 22 73 74 61 74 75 73 22 3a 20 30 2c 20 22 64 61 74 61 22 3a 20 6e 75 6c 6c 7d 45 05 7b b4"
    +2021-01-11 17:40:13,057: root.stringtools.stp - INFO - STP: message identified - (62): 7b 22 64 61 74 61 5f 69 64 22 3a 20 30 2c 20 22 73 65 72 76 69 63 65 5f 69 64 22 3a 20 39 2c 20 22 73 74 61 74 75 73 22 3a 20 30 2c 20 22 64 61 74 61 22 3a 20 6e 75 6c 6c 7d 30 59 be 2f
    +2021-01-11 17:40:13,058: root.tcp_socket.example_client - INFO - comm-client: RX  <- "(62): 7b 22 64 61 74 61 5f 69 64 22 3a 20 30 2c 20 22 73 65 72 76 69 63 65 5f 69 64 22 3a 20 39 2c 20 22 73 74 61 74 75 73 22 3a 20 30 2c 20 22 64 61 74 61 22 3a 20 6e 75 6c 6c 7d 30 59 be 2f"
    +2021-01-11 17:40:13,058: root.socket_protocol.example_client - INFO - prot-client: RX <- service: channel name response, data_id: name, status: okay, data: "None"
    +2021-01-11 17:40:13,109: root.stringtools.stp - INFO - STP: message identified - (85): 7b 22 64 61 74 61 5f 69 64 22 3a 20 30 2c 20 22 73 65 72 76 69 63 65 5f 69 64 22 3a 20 31 31 2c 20 22 73 74 61 74 75 73 22 3a 20 30 2c 20 22 64 61 74 61 22 3a 20 22 4d 6f 6e 20 4a 61 6e 20 31 31 20 31 37 3a 34 30 3a 31 33 20 32 30 32 31 22 7d dd 78 8e ae
    +2021-01-11 17:40:13,109: root.tcp_socket.example_client - INFO - comm-client: RX  <- "(85): 7b 22 64 61 74 61 5f 69 64 22 3a 20 30 2c 20 22 73 65 72 76 69 63 65 5f 69 64 22 3a 20 31 31 2c 20 22 73 74 61 74 75 73 22 3a 20 30 2c 20 22 64 61 74 61 22 3a 20 22 4d 6f 6e 20 4a 61 6e 20 31 31 20 31 37 3a 34 30 3a 31 33 20 32 30 32 31 22 7d dd 78 8e ae"
    +2021-01-11 17:40:13,110: root.socket_protocol.example_client - INFO - prot-client: RX <- service: read data response, data_id: 0, status: okay, data: "'Mon Jan 11 17:40:13 2021'"
    +The Client received: 'Mon Jan 11 17:40:13 2021'
    +
    +
    add_data(service_id, data_id, name)ΒΆ
    diff --git a/_docs_/searchindex.js b/_docs_/searchindex.js index 96bffba..a6b2ef1 100644 --- a/_docs_/searchindex.js +++ b/_docs_/searchindex.js @@ -1 +1 @@ -Search.setIndex({docnames:["index"],envversion:{"sphinx.domains.c":1,"sphinx.domains.changeset":1,"sphinx.domains.cpp":1,"sphinx.domains.javascript":1,"sphinx.domains.math":2,"sphinx.domains.python":1,"sphinx.domains.rst":1,"sphinx.domains.std":1,sphinx:55},filenames:["index.rst"],objects:{"":{socket_protocol:[0,0,0,"-"]},"socket_protocol.data_storage":{get_data:[0,4,1,""],get_data_id:[0,4,1,""],get_service_id:[0,4,1,""],get_status:[0,4,1,""]},"socket_protocol.pure_json_protocol":{add_data:[0,4,1,""],add_msg_to_auth_whitelist_:[0,4,1,""],add_service:[0,4,1,""],add_status:[0,4,1,""],authentificate:[0,4,1,""],check_authentification_state:[0,4,1,""],connection_established:[0,4,1,""],is_connected:[0,4,1,""],receive:[0,4,1,""],reconnect:[0,4,1,""],register_callback:[0,4,1,""],send:[0,4,1,""]},socket_protocol:{AUTH_STATE_KEY_TRANSFERRED:[0,1,1,""],AUTH_STATE_SEED_REQUESTED:[0,1,1,""],AUTH_STATE_SEED_TRANSFERRED:[0,1,1,""],AUTH_STATE_TRUSTED_CONNECTION:[0,1,1,""],AUTH_STATE_UNTRUSTED_CONNECTION:[0,1,1,""],AUTH_STATE__NAMES:[0,1,1,""],DID_AUTH_KEY:[0,1,1,""],DID_AUTH_SEED:[0,1,1,""],DID_CHANNEL_NAME:[0,1,1,""],RequestSidExistsError:[0,2,1,""],ResponseSidExistsError:[0,2,1,""],SID_AUTH_REQUEST:[0,1,1,""],SID_AUTH_RESPONSE:[0,1,1,""],SID_CHANNEL_NAME_REQUEST:[0,1,1,""],SID_CHANNEL_NAME_RESPONSE:[0,1,1,""],SID_EXECUTE_REQUEST:[0,1,1,""],SID_EXECUTE_RESPONSE:[0,1,1,""],SID_READ_REQUEST:[0,1,1,""],SID_READ_RESPONSE:[0,1,1,""],SID_WRITE_REQUEST:[0,1,1,""],SID_WRITE_RESPONSE:[0,1,1,""],STATUS_AUTH_REQUIRED:[0,1,1,""],STATUS_BUFFERING_UNHANDLED_REQUEST:[0,1,1,""],STATUS_CALLBACK_ERROR:[0,1,1,""],STATUS_CHECKSUM_ERROR:[0,1,1,""],STATUS_LOG_LVL:[0,1,1,""],STATUS_OKAY:[0,1,1,""],STATUS_OPERATION_NOT_PERMITTED:[0,1,1,""],STATUS_SERVICE_OR_DATA_UNKNOWN:[0,1,1,""],data_storage:[0,3,1,""],pure_json_protocol:[0,3,1,""],struct_json_protocol:[0,3,1,""]}},objnames:{"0":["py","module","Python module"],"1":["py","data","Python data"],"2":["py","exception","Python exception"],"3":["py","class","Python class"],"4":["py","method","Python method"]},objtypes:{"0":"py:module","1":"py:data","2":"py:exception","3":"py:class","4":"py:method"},terms:{"boolean":0,"class":0,"default":0,"float":0,"function":0,"int":0,"new":0,"return":0,"true":0,IDs:0,The:0,add:0,add_data:0,add_msg_to_auth_whitelist_:0,add_servic:0,add_statu:0,after:0,alder:0,all:0,also:0,ani:0,arg:0,argument:0,auth_state__nam:0,auth_state_key_transf:0,auth_state_seed_request:0,auth_state_seed_transf:0,auth_state_trusted_connect:0,auth_state_untrusted_connect:0,authentif:0,authentification_feedback:0,authetif:0,authitif:0,author:0,auto_auth:0,automat:0,avail:0,been:0,binascii:0,bool:0,both:0,call:0,callback:0,channel:0,channel_nam:0,check_authentification_st:0,checksum:0,client:0,comm_inst:0,commun:0,compat:0,connect:0,connection_establish:0,constant:0,content:0,creat:0,data:0,data_id:0,data_storag:0,defin:0,definit:0,depend:0,deprec:0,descript:0,design:0,did:0,did_auth_kei:0,did_auth_se:0,did_channel_nam:0,differ:0,directli:0,dirk:0,disconnect:0,enabl:0,error:0,establish:0,except:0,exchang:0,execut:0,fals:0,first:0,follow:0,further:0,get:0,get_data:0,get_data_id:0,get_service_id:0,get_statu:0,give:0,given:0,had:0,has:0,have:0,here:0,hexlifi:0,hit:0,identifi:0,incl:0,includ:0,index:0,init_channel_nam:0,initi:0,initialis:0,instanc:0,interfac:0,is_client:0,is_connect:0,issu:0,iter:0,json:0,kei:0,keyword:0,kwarg:0,least:0,length:0,less:0,level:0,like:0,list:0,log:0,manual:0,mean:0,messag:0,method:0,mockeri:0,modul:0,mount:0,name:0,need:0,none:0,object:0,okai:0,old:0,onli:0,oper:0,optin:0,option:0,order:0,otherwis:0,out:0,over:0,page:0,paramet:0,parent:0,permit:0,point:0,previou:0,prioris:0,process:0,pure_json_protocol:0,read:0,read_request:0,read_respons:0,reason:0,receiv:0,recommend:0,reconnect:0,reduc:0,regist:0,register_callback:0,register_connect_callback:0,register_disconnect_callback:0,registr:0,relev:0,req_nam:0,req_sid:0,request:0,requestsidexistserror:0,requir:0,resp_nam:0,resp_sid:0,respond:0,respons:0,response_data:0,response_statu:0,responsesidexistserror:0,same:0,search:0,secret:0,see:0,seed:0,send:0,sent:0,serivc:0,server:0,servic:0,service_:0,service_id:0,set:0,should:0,sid:0,sid_:0,sid_auth_request:0,sid_auth_respons:0,sid_channel_name_request:0,sid_channel_name_respons:0,sid_execute_request:0,sid_execute_respons:0,sid_read_request:0,sid_read_respons:0,sid_write_request:0,sid_write_respons:0,side:0,specif:0,sta_:0,start:0,state:0,statu:0,status_:0,status_auth_requir:0,status_buffering_unhandled_request:0,status_callback_error:0,status_checksum_error:0,status_log_lvl:0,status_okai:0,status_operation_not_permit:0,status_service_or_data_unknown:0,storag:0,str:0,struct_json_protocol:0,submodul:0,successful:0,sudo:0,support:0,taken:0,than:0,thei:0,thi:0,time:0,timeout:0,timout:0,transfer:0,trust:0,tupl:0,type:0,unhandl:0,unittest:0,unknown:0,unspecif:0,untrust:0,urandom:0,usag:0,use:0,used:0,using:0,valu:0,via:0,warn:0,where:0,write:0,write_request:0,write_respons:0,you:0},titles:["Welcome to socket_protocol\u2019s documentation!"],titleterms:{document:0,indic:0,protocol:0,socket:0,socket_protocol:0,tabl:0,welcom:0}}) \ No newline at end of file +Search.setIndex({docnames:["index"],envversion:{"sphinx.domains.c":1,"sphinx.domains.changeset":1,"sphinx.domains.cpp":1,"sphinx.domains.javascript":1,"sphinx.domains.math":2,"sphinx.domains.python":1,"sphinx.domains.rst":1,"sphinx.domains.std":1,sphinx:55},filenames:["index.rst"],objects:{"":{socket_protocol:[0,0,0,"-"]},"socket_protocol.data_storage":{get_data:[0,4,1,""],get_data_id:[0,4,1,""],get_service_id:[0,4,1,""],get_status:[0,4,1,""]},"socket_protocol.pure_json_protocol":{add_data:[0,4,1,""],add_msg_to_auth_whitelist_:[0,4,1,""],add_service:[0,4,1,""],add_status:[0,4,1,""],authentificate:[0,4,1,""],check_authentification_state:[0,4,1,""],connection_established:[0,4,1,""],is_connected:[0,4,1,""],receive:[0,4,1,""],reconnect:[0,4,1,""],register_callback:[0,4,1,""],send:[0,4,1,""]},socket_protocol:{AUTH_STATE_KEY_TRANSFERRED:[0,1,1,""],AUTH_STATE_SEED_REQUESTED:[0,1,1,""],AUTH_STATE_SEED_TRANSFERRED:[0,1,1,""],AUTH_STATE_TRUSTED_CONNECTION:[0,1,1,""],AUTH_STATE_UNTRUSTED_CONNECTION:[0,1,1,""],AUTH_STATE__NAMES:[0,1,1,""],DID_AUTH_KEY:[0,1,1,""],DID_AUTH_SEED:[0,1,1,""],DID_CHANNEL_NAME:[0,1,1,""],RequestSidExistsError:[0,2,1,""],ResponseSidExistsError:[0,2,1,""],SID_AUTH_REQUEST:[0,1,1,""],SID_AUTH_RESPONSE:[0,1,1,""],SID_CHANNEL_NAME_REQUEST:[0,1,1,""],SID_CHANNEL_NAME_RESPONSE:[0,1,1,""],SID_EXECUTE_REQUEST:[0,1,1,""],SID_EXECUTE_RESPONSE:[0,1,1,""],SID_READ_REQUEST:[0,1,1,""],SID_READ_RESPONSE:[0,1,1,""],SID_WRITE_REQUEST:[0,1,1,""],SID_WRITE_RESPONSE:[0,1,1,""],STATUS_AUTH_REQUIRED:[0,1,1,""],STATUS_BUFFERING_UNHANDLED_REQUEST:[0,1,1,""],STATUS_CALLBACK_ERROR:[0,1,1,""],STATUS_CHECKSUM_ERROR:[0,1,1,""],STATUS_LOG_LVL:[0,1,1,""],STATUS_OKAY:[0,1,1,""],STATUS_OPERATION_NOT_PERMITTED:[0,1,1,""],STATUS_SERVICE_OR_DATA_UNKNOWN:[0,1,1,""],data_storage:[0,3,1,""],pure_json_protocol:[0,3,1,""],struct_json_protocol:[0,3,1,""]}},objnames:{"0":["py","module","Python module"],"1":["py","data","Python data"],"2":["py","exception","Python exception"],"3":["py","class","Python class"],"4":["py","method","Python method"]},objtypes:{"0":"py:module","1":"py:data","2":"py:exception","3":"py:class","4":"py:method"},terms:{"boolean":0,"class":0,"default":0,"float":0,"function":0,"import":0,"int":0,"new":0,"null":[],"return":0,"true":0,"while":0,Adding:[],IDs:0,NOT:[],The:0,Will:0,__authentificate_check_key__:[],__authentificate_create_key__:[],__authentificate_create_seed__:[],__authentificate_process_feedback__:[],__channel_name_request__:[],__channel_name_response__:[],add:0,add_data:0,add_msg_to_auth_whitelist_:0,add_servic:0,add_statu:0,after:0,alder:0,all:0,all_oth:[],also:0,ani:0,append:0,arg:0,argument:0,asctim:0,auth_state__nam:0,auth_state_key_transf:0,auth_state_seed_request:0,auth_state_seed_transf:0,auth_state_trusted_connect:0,auth_state_untrusted_connect:0,authentif:0,authentification_feedback:0,authetif:0,authitif:0,author:0,auto_auth:0,automat:0,avail:0,been:0,binascii:0,bool:0,both:0,buffer:[],call:0,callback:0,chang:[],channel:0,channel_nam:0,check_authentification_st:0,checksum:0,clean:[],client:0,comm:0,comm_inst:0,commun:0,compat:0,connect:0,connection_establish:0,constant:0,content:0,cound:[],creat:0,data:0,data_id:0,data_storag:0,debug:[],def:0,defin:0,definit:0,depend:0,deprec:0,descript:0,design:0,did:0,did_auth_kei:0,did_auth_se:0,did_channel_nam:0,differ:0,directli:0,dirk:0,disconnect:0,els:0,enabl:0,end:[],error:0,establish:0,exampl:0,example_cli:0,example_serv:0,except:0,exchang:0,execut:0,fals:0,finish:0,first:0,follow:0,further:0,get:0,get_data:0,get_data_id:0,get_service_id:0,get_statu:0,give:0,given:0,had:0,has:0,have:0,here:0,hexlifi:0,hit:0,identifi:0,incl:0,includ:0,incom:[],index:0,info:0,init_channel_nam:0,initi:0,initialis:0,instanc:0,interfac:0,is_client:0,is_connect:0,issu:0,iter:0,jan:0,json:0,kei:0,keyword:0,kwarg:0,least:0,length:0,less:0,level:0,like:0,list:0,log:0,log_name_lvl:0,lost:[],manual:0,mean:0,messag:0,method:0,mirror_callback:[],mockeri:0,modul:0,mon:0,mount:0,msg:0,name:0,need:0,neg:[],none:0,object:0,okai:0,old:0,onli:0,oper:0,optin:0,option:0,order:0,otherwis:0,out:0,output:0,over:0,page:0,paramet:0,parent:0,path:0,pattern:[],permit:0,point:0,previou:0,print:0,prioris:0,process:0,prot:0,pure_json_protocol:0,read:0,read_request:0,read_respons:0,reason:0,receiv:0,recommend:0,reconnect:0,reduc:0,regist:0,register_callback:0,register_connect_callback:0,register_disconnect_callback:0,registr:0,relev:0,report:0,repr:0,req_nam:0,req_sid:0,request:0,requestsidexistserror:0,requir:0,reset:0,resp_nam:0,resp_sid:0,respond:0,respons:0,response_data:0,response_statu:0,responsesidexistserror:0,result:0,root:0,same:0,search:0,secret:0,see:0,seed:0,send:0,sent:0,serivc:0,server:0,servic:0,service_:0,service_id:0,set:0,should:0,sid:0,sid_:0,sid_auth_request:0,sid_auth_respons:0,sid_channel_name_request:0,sid_channel_name_respons:0,sid_execute_request:0,sid_execute_respons:0,sid_read_request:0,sid_read_respons:0,sid_write_request:0,sid_write_respons:0,side:0,sleep:0,specif:0,sta_:0,start:0,state:0,statu:0,status_:0,status_auth_requir:0,status_buffering_unhandled_request:0,status_callback_error:0,status_checksum_error:0,status_log_lvl:0,status_okai:0,status_operation_not_permit:0,status_service_or_data_unknown:0,stdoutloggingconfigur:0,storag:0,store:[],stp:0,stp_state_escape_1:[],stp_state_escape_2:[],stp_state_idl:[],stp_state_store_data:[],str:0,stringtool:0,struct_json_protocol:0,submodul:0,successful:0,sudo:0,support:0,sync:[],sys:0,taken:0,tcp:[],tcp_client_stp:0,tcp_server_stp:0,tcp_socket:0,than:0,thei:0,thi:0,time:0,time_callback:0,timeout:0,timout:0,transfer:0,trust:0,tupl:0,type:0,unhandl:0,unittest:0,unknown:0,unspecif:0,untrust:0,urandom:0,usag:0,use:0,used:0,using:0,valu:0,via:0,wait:0,warn:0,where:0,whitelist:[],write:0,write_request:0,write_respons:0,x01:[],x88j3:[],you:0},titles:["Welcome to socket_protocol\u2019s documentation!"],titleterms:{document:0,indic:0,protocol:0,socket:0,socket_protocol:0,tabl:0,welcom:0}}) \ No newline at end of file diff --git a/_examples_/Makefile b/_examples_/Makefile index c41ed7c..949c12a 100644 --- a/_examples_/Makefile +++ b/_examples_/Makefile @@ -4,7 +4,11 @@ LOGFILES = ${EXAMPLES:.py=.log} .PHONY: all all: $(LOGFILES) -%.log: %.py +%server.log: %server.py + @echo -n + +%client.log: %client.py + python3 $(subst client,server,$<) 1> /dev/null& python3 $< > $@ clean: diff --git a/_examples_/socket_protocol_client.py b/_examples_/socket_protocol_client.py new file mode 100644 index 0000000..c6b80f8 --- /dev/null +++ b/_examples_/socket_protocol_client.py @@ -0,0 +1,14 @@ +import sys +sys.path.append('../..') + +import report +import socket_protocol +import tcp_socket +import time + + +report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) +c = tcp_socket.tcp_client_stp('127.0.0.1', 17017) +sp = socket_protocol.pure_json_protocol(c, channel_name='example_client') +sp.send(socket_protocol.SID_READ_REQUEST, 0, None) +print('The Client received: %s' % repr(sp.receive(socket_protocol.SID_READ_RESPONSE, 0).get_data())) diff --git a/_examples_/socket_protocol_server.py b/_examples_/socket_protocol_server.py new file mode 100644 index 0000000..391bf69 --- /dev/null +++ b/_examples_/socket_protocol_server.py @@ -0,0 +1,30 @@ +import sys +sys.path.append('../..') + +import report +import socket_protocol +import tcp_socket +import time + + +def time_callback(msg): + if msg.get_status() == socket_protocol.STATUS_OKAY: + return socket_protocol.STATUS_OKAY, time.asctime() + else: + return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None + + +report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ]) +s = tcp_socket.tcp_server_stp('127.0.0.1', 17017) +sp = socket_protocol.pure_json_protocol(s, channel_name='example_server') +sp.register_callback(socket_protocol.SID_READ_REQUEST, 0, time_callback) + +i = 0 +while not s.is_connected() and i <= 20: + i += 1 + time.sleep(.1) # wait for a connection + +i = 0 +while s.is_connected() and i <= 20: + i += 1 + time.sleep(.1) # wait for disconnect