2828position = socket .gethostbyname (socket .gethostname ())
2929mrn_domain = 'NewsTextAnalytics'
3030mrn_item = 'MRN_STORY'
31- mrn_item = 'MRN_TRNA'
31+ # mrn_item = 'MRN_TRNA'
3232
3333# Global Variables
3434web_socket_app = None
@@ -65,16 +65,17 @@ def processRefresh(ws, message_json):
6565 decodeFieldList (message_json ["Fields" ])
6666
6767
68- def processUpdate (ws , message_json ):
68+ def processMRNUpdate (ws , message_json ): # process incoming News Update messages
6969 print ("RECEIVED: Update Message" )
7070 # print(message_json)
7171
7272 fields_data = message_json ["Fields" ]
7373 # Dump the FieldList first (for informational purposes)
74- decodeFieldList (message_json ["Fields" ])
74+ # decodeFieldList(message_json["Fields"])
7575
7676 # declare variables
7777 tot_size = 0
78+ guid = None
7879
7980 try :
8081 # Get data for all requried fields
@@ -96,29 +97,30 @@ def processUpdate(ws, message_json):
9697 if envelop and envelop ["data" ]["mrn_src" ] == mrn_src and frag_num == envelop ["data" ]["frag_num" ] + 1 :
9798 print ("process multiple fragments for guid %s" %
9899 envelop ["guid" ])
99- # print(envelop)
100+
100101 #print("fragment before merge = %d" % len(envelop["data"]["fragment"]))
101102
102- # Merge incoming data to existing envelop
103+ # Merge incoming data to existing envelop and getting FRAGMENT and TOT_SIZE data to local variables
103104 fragment = envelop ["data" ]["fragment" ] = envelop ["data" ]["fragment" ] + fragment
104105 envelop ["data" ]["frag_num" ] = frag_num
105106 tot_size = envelop ["data" ]["tot_size" ]
106107 print ("TOT_SIZE = %d" % tot_size )
107- print ("fragment length = %d" % len (fragment ))
108- #print("TOT_SIZE from envelop = %d" % envelop["data"]["tot_size"])
109- #print("fragment after merge = %d" % len(envelop["data"]["fragment"]))
108+ print ("Current FRAGMENT length = %d" % len (fragment ))
109+
110+ # The multiple fragments news are not completed, waiting.
110111 if tot_size != len (fragment ):
111112 return None
113+ # The multiple fragments news are completed, delete assoiclate GUID dictionary
114+ elif tot_size == len (fragment ):
115+ del _news_envelopes [guid_index ]
112116 else :
113117 print ("Error: Cannot find fragment for GUID %s with matching FRAG_NUM or MRN_SRC %s" % (
114118 guid , mrn_src ))
115119 return None
116- else : # FRAG_NUM: 1 The first fragment
120+ else : # FRAG_NUM = 1 The first fragment
117121 tot_size = int (fields_data ["TOT_SIZE" ])
118- print ("fragment length = %d" % len (fragment ))
119- print ("TOT_SIZE = %d" % tot_size )
122+ print ("FRAGMENT length = %d" % len (fragment ))
120123 if tot_size != len (fragment ): # Completed News
121- #print("Receiving Multiple Fragments!!")
122124 print ("Add new fragments to news envelop for guid %s" % guid )
123125 _news_envelopes .append ({
124126 "guid" : guid ,
@@ -131,20 +133,26 @@ def processUpdate(ws, message_json):
131133 })
132134 return None
133135
134- # News Fragment completed, decompress and print data as JSON
136+ # News Fragment(s) completed, decompress and print data as JSON to console
135137 if tot_size == len (fragment ):
136138 decompressed_data = zlib .decompress (fragment , zlib .MAX_WBITS | 32 )
137139 print ("News = %s" % json .loads (decompressed_data ))
138140
139141 except KeyError as keyerror :
140142 print ('KeyError exception: ' , keyerror )
143+ except IndexError as indexerror :
144+ print ('IndexError exception: ' , indexerror )
141145 except zlib .error as error :
142146 print ('zlib exception: ' , error )
147+ # Some console environments like Windows may encounter this unicode display as a limitation of OS
148+ except UnicodeEncodeError as encodeerror :
149+ print ("UnicodeEncodeError exception. Cannot decode unicode character for %s in this enviroment: " %
150+ guid , encodeerror )
143151 except Exception as e :
144152 print ('exception: ' , sys .exc_info ()[0 ])
145153
146154
147- def processStatus (ws , message_json ):
155+ def processStatus (ws , message_json ): # process incoming status message
148156 print ("RECEIVED: Status Message" )
149157 print (json .dumps (message_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
150158
@@ -165,7 +173,7 @@ def process_message(ws, message_json):
165173 processRefresh (ws , message_json )
166174 elif message_type == "Update" :
167175 if "Domain" in message_json and message_json ["Domain" ] == mrn_domain :
168- processUpdate (ws , message_json )
176+ processMRNUpdate (ws , message_json )
169177 elif message_type == "Status" :
170178 processStatus (ws , message_json )
171179 elif message_type == "Ping" :
0 commit comments