#!/usr/bin/python

########################################################################################
#
# Filename:   MAPServer 
#  
# Description
# ===========
# 
#  
#
# Author    : Michael Hsiao 
#
# Date      : 2016/11/20 @Taipei
# Desc      : MAP SMS Server

# Update by : Michael Hsiao 
# Date      : 2017/02/19 @Manila
# Desc      : Support MO-FSM ,SRI-SM and MT-FSM (request/response)


import sys
import PCA_GenLib
import PCA_M3UAServerSocket

############################################################################\
#
############################################################################
def Server(XMLCFG):
  try:
	print 'Start Server ...'
	try:
		PCA_GenLib.DBXMLCFGInit(XMLCFG)		
		try:	
			Msg = "Server StartUp ... 2017/03/19 Version 1.1"
			PCA_GenLib.WriteLog(Msg,0)	
			TimeOut = 2.5
			MAPServer = PCA_M3UAServerSocket.Acceptor(XMLCFG)
                        try:
                          MAPServer.dispatcher(TimeOut=1.0)
                        finally:				
                          MAPServer.close()
		finally:
			
			PCA_GenLib.WriteLog(Msg,0)
	finally:
	
		PCA_GenLib.CloseLog()
  except IOError:        
	print "IO error"
	print '< ',sys.exc_type,sys.exc_value,' >'

  except:
   	print 'err uncaught ! < ',sys.exc_type,sys.exc_value,' >'

############################################################################
#
############################################################################
if __name__ == '__main__':
  
  try:	
  	print "Open cfg file"
        try:		
		filename =  sys.argv[1]
        	XMLCFG =  open(filename,"r").read()
	except IOError:
		XMLCFG =  open("MAPServer.cfg","r").read()		

	Server(XMLCFG)
  except:
  	print "MAPServer.cfg configuration file not found"
 	print "Msg = : <%s>,<%s>" % (sys.exc_type,sys.exc_value)
  	sys.exit()
   	
  print "Bye !"
	
