/** * Constructor - takes a datagram packet and a stack structure Extracts the address of the other * from the datagram packet and stashes away the pointer to the passed stack structure. * * @param stack is the shared SIPStack structure * @param messageProcessor is the creating message processor. */ protected UDPMessageChannel(SIPTransactionStack stack, UDPMessageProcessor messageProcessor) { super.messageProcessor = messageProcessor; this.sipStack = stack; Thread mythread = new Thread(this); this.myAddress = messageProcessor.getIpAddress().getHostAddress(); this.myPort = messageProcessor.getPort(); mythread.setName("UDPMessageChannelThread"); mythread.setDaemon(true); mythread.start(); }
/** * Constructor. We create one of these in order to process an incoming message. * * @param stack is the SIP sipStack. * @param messageProcessor is the creating message processor. * @param packet is the incoming datagram packet. */ protected UDPMessageChannel( SIPTransactionStack stack, UDPMessageProcessor messageProcessor, DatagramPacket packet) { this.incomingPacket = packet; super.messageProcessor = messageProcessor; this.sipStack = stack; this.myAddress = messageProcessor.getIpAddress().getHostAddress(); this.myPort = messageProcessor.getPort(); Thread mythread = new Thread(this); mythread.setDaemon(true); mythread.start(); }
/** * Constructor. We create one of these when we send out a message. * * @param targetAddr INET address of the place where we want to send messages. * @param port target port (where we want to send the message). * @param sipStack our SIP Stack. */ protected UDPMessageChannel( InetAddress targetAddr, int port, SIPTransactionStack sipStack, UDPMessageProcessor messageProcessor) { peerAddress = targetAddr; peerPort = port; peerProtocol = "UDP"; super.messageProcessor = messageProcessor; this.myAddress = messageProcessor.getIpAddress().getHostAddress(); this.myPort = messageProcessor.getPort(); this.sipStack = sipStack; if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug( "Creating message channel " + targetAddr.getHostAddress() + "/" + port); } }
/** Run method specified by runnnable. */ public void run() { // Assume no thread pooling (bug fix by spierhj) ThreadAuditor.ThreadHandle threadHandle = null; while (true) { // Create a new string message parser to parse the list of messages. if (myParser == null) { myParser = new StringMsgParser(); myParser.setParseExceptionListener(this); } // messages that we write out to him. DatagramPacket packet; if (sipStack.threadPoolSize != -1) { synchronized (((UDPMessageProcessor) messageProcessor).messageQueue) { while (((UDPMessageProcessor) messageProcessor).messageQueue.isEmpty()) { // Check to see if we need to exit. if (!((UDPMessageProcessor) messageProcessor).isRunning) return; try { // We're part of a thread pool. Ask the auditor to // monitor this thread. if (threadHandle == null) { threadHandle = sipStack.getThreadAuditor().addCurrentThread(); } // Send a heartbeat to the thread auditor threadHandle.ping(); // Wait for packets // Note: getPingInterval returns 0 (infinite) if the // thread auditor is disabled. ((UDPMessageProcessor) messageProcessor) .messageQueue.wait(threadHandle.getPingIntervalInMillisecs()); } catch (InterruptedException ex) { if (!((UDPMessageProcessor) messageProcessor).isRunning) return; } } packet = (DatagramPacket) ((UDPMessageProcessor) messageProcessor).messageQueue.removeFirst(); } this.incomingPacket = packet; } else { packet = this.incomingPacket; } // Process the packet. Catch and log any exception we may throw. try { processIncomingDataPacket(packet); } catch (Exception e) { sipStack.logWriter.logError("Error while processing incoming UDP packet", e); } if (sipStack.threadPoolSize == -1) { return; } } }