Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package gov.nist.javax.sip.stack.sctp;
  
 
 
 import  com.sun.nio.sctp.MessageInfo;
 import  com.sun.nio.sctp.SctpChannel;

SCTP message channel

Author(s):
Jeroen van Bemmel
 
 final class SCTPMessageChannel extends MessageChannel 
 
 	private final SCTPMessageProcessor processor;
 	private InetSocketAddress peerAddress;			// destination address
 	
 	private final SctpChannel channel;
 	private final SelectionKey key;
 	
 	private final MessageInfo messageInfo;	// outgoing SCTP options, cached
 	private long rxTime;	//< Time first byte of message was received
 
 	// XXX Hardcoded, enough? TODO make stack property
 	private final ByteBuffer rxBuffer = ByteBuffer.allocateDirect( 10000 );
 	
 	private final StringMsgParser parser = new StringMsgParserthis );	// Parser instance
 	
 	// for outgoing connections
 		this. = p;
 		this. = p;	// super class
 		this. = dest;		
 		this. = dest;		// assume the same, override upon packet
 		
 		this. = MessageInfo.createOutgoing( dest, 0 );
 		.unordered( true );
 		
 		this. = SctpChannel.open( dest, 1, 1 );
 		.configureBlocking( false );
 		this. = .registerChannelthis );
 	}
 	
 	// For incoming connections
 	SCTPMessageChannelSCTPMessageProcessor p, SctpChannel c ) throws IOException {
 		this. = p;
 		this. = p;	// super class
 		SocketAddress a = c.getRemoteAddresses().iterator().next();
 		this. = MessageInfo.createOutgoing( a, 0 );
 		.unordered( true );
 		
 		this. = c;
 		.configureBlocking( false );
 		this. = .registerChannelthis );
 	}
 			
 	public void close() {		
 		try {
 			this..cancel();
 			.close();
 		} catch (IOException e) {
 		} finally {
 		}
 	}
 
	void closeNoRemove() {		
		try {
			this..cancel();
			.close();
catch (IOException e) {
		}
	}
	public String getKey() {
		// Note: could put this in super class
		return getKeythis.getPeerInetAddress(), this.getPeerPort(), this.getTransport() );
	}
	public String getPeerAddress() {
	}
	}
	}
	public int getPeerPacketSourcePort() {
	}
	public int getPeerPort() {
	}
	protected String getPeerProtocol() {
		return "sctp";	// else something really is weird ;)
	}
	}
	public String getTransport() {
		return "sctp";
	}
	public String getViaHost() {
	}
	public int getViaPort() {
		return .getPort();
	}
	public boolean isReliable() {
		return true;
	}
	public boolean isSecure() {
		return false;
	}
	public void sendMessage(SIPMessage sipMessagethrows IOException {
		byte[] msg = sipMessage.encodeAsBytesthis.getTransport() );
		this.sendMessagemsgthis.getPeerInetAddress(), this.getPeerPort(), false );
	}
	protected void sendMessage(byte[] messageInetAddress receiverAddress,
			int receiverPortboolean reconnectFlagthrows IOException {
		assertreceiverAddress.equals.getAddress() ) );
		assertreceiverPort == .getPort() );
		// XX ignoring 'reconnect' for now
		int nBytes = .send( ByteBuffer.wrap(message),  );
			getSIPStack().getStackLogger().logDebug"SCTP bytes sent:" + nBytes );
		}
	}

Called by SCTPMessageProcessor when one or more bytes are available for reading

Throws:
IOException
	void readMessages() throws IOException {
		if (==0) {
		}
		MessageInfo info = .receive( nullnull );
		if (info==null) {
			// happens a lot, some sort of keep-alive?
				getSIPStack().getStackLogger().logDebug"SCTP read-event but no message" );
			}
			return;
else if (info.bytes()==-1) {
			getSIPStack().getStackLogger().logWarning"SCTP peer closed, closing too..." );
			this.close();
			return;
else if ( !info.isComplete() ) {
				getSIPStack().getStackLogger().logDebug"SCTP incomplete message; bytes=" + info.bytes() );
			}
			return;
else {
				getSIPStack().getStackLogger().logDebug"SCTP message now complete; bytes=" + info.bytes() );
			}			
		}
		// Assume it is 1 full message, not multiple messages
		byte[] msg = new byte.position() ];
		.getmsg );
		try {
			this.processMessagem );
			 = 0;	// reset for next message
catch (ParseException e) {
				getSIPStack().getStackLogger().logDebug"Invalid message bytes=" + msg.length + ":" + new String(msg) );
			}
			this.close();
			throw new IOException"Error parsing incoming SCTP message"e );
		}		
	}

Actually proces the parsed message.

Parameters:
sipMessage JvB: copied from UDPMessageChannel, TODO restructure
    private void processMessageSIPMessage sipMessagelong rxTime ) {
    	SIPTransactionStack sipStack = .getSIPStack();
        if (sipMessage instanceof SIPRequest) {
            SIPRequest sipRequest = (SIPRequestsipMessage;
            // This is a request - process it.
            // So far so good -- we will commit this message if
            // all processing is OK.
            if (sipStack.getStackLogger().isLoggingEnabled(.)) {
                sipStack.getServerLogger().logMessage(sipMessagethis
                        .getPeerHostPort().toString(), this.getHost() + ":"
                        + this.getPort(), falserxTime);
            }
            ServerRequestInterface sipServerRequest = sipStack
                    .newSIPServerRequest(sipRequestthis);
            // Drop it if there is no request returned
            if (sipServerRequest == null) {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger()
                            .logWarning("Null request interface returned -- dropping request");
                }
                return;
            }
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("About to process "
                        + sipRequest.getFirstLine() + "/" + sipServerRequest);
            try {
                sipServerRequest.processRequest(sipRequestthis);
            } finally {
                if (sipServerRequest instanceof SIPTransaction) {
                    SIPServerTransaction sipServerTx = (SIPServerTransactionsipServerRequest;
                    if (!sipServerTx.passToListener()) {
                        ((SIPTransactionsipServerRequest).releaseSem();
                    }
                }
            }
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Done processing "
                        + sipRequest.getFirstLine() + "/" + sipServerRequest);
            // So far so good -- we will commit this message if
            // all processing is OK.
        } else {
            // Handle a SIP Reply message.
            SIPResponse sipResponse = (SIPResponsesipMessage;
            try {
                sipResponse.checkHeaders();
            } catch (ParseException ex) {
                if (sipStack.isLoggingEnabled())
                    sipStack.getStackLogger()
                            .logError("Dropping Badly formatted response message >>> "
                                    + sipResponse);
                return;
            }
            ServerResponseInterface sipServerResponse = sipStack
                    .newSIPServerResponse(sipResponsethis);
            if (sipServerResponse != null) {
                try {
                    if (sipServerResponse instanceof SIPClientTransaction
                            && !((SIPClientTransactionsipServerResponse)
                                    .checkFromTag(sipResponse)) {
                        if (sipStack.isLoggingEnabled())
                            sipStack.getStackLogger()
                                    .logError("Dropping response message with invalid tag >>> "
                                            + sipResponse);
                        return;
                    }
                    sipServerResponse.processResponse(sipResponsethis);
                } finally {
                    if (sipServerResponse instanceof SIPTransaction
                            && !((SIPTransactionsipServerResponse)
                                    .passToListener())
                        ((SIPTransactionsipServerResponse).releaseSem();
                }
                // Normal processing of message.
            } else {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("null sipServerResponse!");
                }
            }
        }
    }
    
    
Implementation of the ParseExceptionListener interface.

Parameters:
ex Exception that is given to us by the parser.
Throws:
ParseException If we choose to reject the header or message. JvB: copied from UDPMessageChannel, TODO restructure!
    public void handleException(ParseException exSIPMessage sipMessage,
            Class hdrClassString headerString message)
            throws ParseException {
        if (getSIPStack().isLoggingEnabled())
            this.getSIPStack().getStackLogger().logException(ex);
        // Log the bad message for later reference.
        if ((hdrClass != null)
                && (hdrClass.equals(From.class) || hdrClass.equals(To.class)
                        || hdrClass.equals(CSeq.class)
                        || hdrClass.equals(Via.class)
                        || hdrClass.equals(CallID.class)
                        || hdrClass.equals(RequestLine.class) || hdrClass
                        .equals(StatusLine.class))) {
        	getSIPStack().getStackLogger().logError("BAD MESSAGE!");
        	getSIPStack().getStackLogger().logError(message);
            throw ex;
        } else {
            sipMessage.addUnparsed(header);
        }
    }
    public int compareTo(SCTPMessageChannel o) {
    	return this.hashCode() - o.hashCode();
    }
    
    @Override
    protected void uncache() {
    	// TODO Auto-generated method stub
    }
New to GrepCode? Check out our FAQ X