kotlin JAINSIP—搭建sip服务器(第一篇)

时间:2025-01-31 12:42:39
import java.util.* import javax.sip.* import javax.sip.address.AddressFactory import javax.sip.header.HeaderFactory import javax.sip.message.MessageFactory import javax.sip.message.Request import kotlin.collections.HashMap abstract class Sip : SipListener { var sipstack: SipStack//监听端口对象 var hf: HeaderFactory//头部对象 var af: AddressFactory//地址对象 var mf: MessageFactory//消息体对象 var sipProvider: SipProvider//sip信息监听对象 var userInfo=HashMap<String,String>()//用户信息 key:账号value:密码 val address="192.168.3.5"//服务器地址 val prot=5060//服务器端口 init { //添加账号信息 userInfo.put("1","123456") userInfo.put("2","123456") userInfo.put("3","123456") userInfo.put("4","123456") //配置一下日志信息 val prop = Properties() prop.setProperty(".STACK_NAME", "teststackname") prop.setProperty(".TRACE_LEVEL", "32") prop.setProperty(".DEBUG_LOG", "") prop.setProperty(".SERVER_LOG", "") val sf = SipFactory.getInstance() sf.pathName = "" sipstack = sf.createSipStack(prop) sf.createHeaderFactory() hf = sf.createHeaderFactory() af = sf.createAddressFactory() mf = sf.createMessageFactory() val listeningPoint = sipstack.createListeningPoint(address, prot, "udp")//启动sip的udp服务端 sipProvider = sipstack.createSipProvider(listeningPoint) sipProvider.addSipListener(this)//设置sip的消息监听 println("sipf服务已启动") } /** *@Description 注册函数 *@Param *@Return *@Author *@Date 2019/11/9 *@Time 14:20 */ abstract fun doRegister(requestEvent:RequestEvent) /** *@Description 处理invite消息 *@Param *@Return *@Author *@Date 2019/11/9 *@Time 14:35 */ abstract fun doInvite(requestEvent: RequestEvent) /** *@Description 处理通话过程中的信息 *@Param *@Return *@Author *@Date 2019/11/14 *@Time 14:33 */ abstract fun doProcessResponse(responseEvent: ResponseEvent) /** * 当出现异步消息传输错误时,SIP堆栈调用此方法。 */ override fun processIOException(p0: IOExceptionEvent?) { System.err.println(p0?.transport) // println("processIOException$p0") } /** *@Description 此方法在对话(会话)结束时由SIP堆栈调用。 *@Param [dialogTerminatedEvent] *@Return void *@Author *@Date 2019/11/9 *@Time 0:38 */ override fun processDialogTerminated(p0: DialogTerminatedEvent?) { println("processDialogTerminated$p0") } /** * SIP服务端接收消息的方法 * Content 里面是GBK编码 * This method is called by the SIP stack when a new request arrives. */ override fun processRequest(requestEvent: RequestEvent) { System.err.println("processRequest:${requestEvent.request.method.toUpperCase()}") if (requestEvent.request==null) { println("无效的processRequest请求") return } when(requestEvent.request.method.toUpperCase()){ Request.INVITE->{ doInvite(requestEvent) } Request.REGISTER->{ doRegister(requestEvent) } Request.SUBSCRIBE->{ } Request.ACK->{ } Request.BYE->{ } Request.CANCEL->{ } else->{ println("未知的:" + requestEvent.request.method.toUpperCase()) } } } /** * sip客户端回复消息 */ override fun processResponse(p0: ResponseEvent) { doProcessResponse(p0) } /** * 当消息没有应答时,SIP堆栈调用此方法。请注意,这与错误消息的处理方式不同。 */ override fun processTimeout(p0: TimeoutEvent?) { println("processTimeout$p0") } /** *@Description 此方法在事务结束时由SIP堆栈调用。 *@Param [transactionTerminatedEvent] *@Return void *@Author *@Date 2019/11/9 *@Time 0:38 */ override fun processTransactionTerminated(p0: TransactionTerminatedEvent?) { println("processTransactionTerminated$p0") } }