?? invitehandler.java
字號:
/*
This file is part of Peers.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright 2007, 2008 Yohann Martineau
*/
package net.sourceforge.peers.sip.core.useragent.handlers;
import gov.nist.jrtp.RtpException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import net.sourceforge.peers.Logger;
import net.sourceforge.peers.media.CaptureRtpSender;
import net.sourceforge.peers.media.IncomingRtpReader;
import net.sourceforge.peers.sdp.NoCodecException;
import net.sourceforge.peers.sdp.SDPManager;
import net.sourceforge.peers.sdp.SessionDescription;
import net.sourceforge.peers.sip.RFC3261;
import net.sourceforge.peers.sip.Utils;
import net.sourceforge.peers.sip.core.useragent.MidDialogRequestManager;
import net.sourceforge.peers.sip.core.useragent.SipEvent;
import net.sourceforge.peers.sip.core.useragent.UserAgent;
import net.sourceforge.peers.sip.core.useragent.SipEvent.EventType;
import net.sourceforge.peers.sip.syntaxencoding.NameAddress;
import net.sourceforge.peers.sip.syntaxencoding.SipHeaderFieldName;
import net.sourceforge.peers.sip.syntaxencoding.SipHeaderFieldValue;
import net.sourceforge.peers.sip.syntaxencoding.SipHeaderParamName;
import net.sourceforge.peers.sip.syntaxencoding.SipHeaders;
import net.sourceforge.peers.sip.syntaxencoding.SipURI;
import net.sourceforge.peers.sip.transaction.ClientTransaction;
import net.sourceforge.peers.sip.transaction.ClientTransactionUser;
import net.sourceforge.peers.sip.transaction.InviteServerTransaction;
import net.sourceforge.peers.sip.transaction.ServerTransaction;
import net.sourceforge.peers.sip.transaction.ServerTransactionUser;
import net.sourceforge.peers.sip.transaction.Transaction;
import net.sourceforge.peers.sip.transaction.TransactionManager;
import net.sourceforge.peers.sip.transactionuser.Dialog;
import net.sourceforge.peers.sip.transactionuser.DialogManager;
import net.sourceforge.peers.sip.transport.MessageSender;
import net.sourceforge.peers.sip.transport.SipRequest;
import net.sourceforge.peers.sip.transport.SipResponse;
import net.sourceforge.peers.sip.transport.TransportManager;
public class InviteHandler extends DialogMethodHandler
implements ServerTransactionUser, ClientTransactionUser {
//TODO move sdp manager, this should probably be in UserAgent
private SDPManager sdpManager;
public InviteHandler() {
sdpManager = new SDPManager();
}
//////////////////////////////////////////////////////////
// UAS methods
//////////////////////////////////////////////////////////
public void handleInitialInvite(SipRequest sipRequest) {
// setChanged();
// ArrayList args = new ArrayList();
// args.add(Event.incomingCall);
// args.add(sipRequest);
//
// notifyObservers(args);
//generate 180 Ringing
SipResponse sipResponse = buildGenericResponse(sipRequest,
RFC3261.CODE_180_RINGING, RFC3261.REASON_180_RINGING);
Dialog dialog = buildDialogForUas(sipResponse, sipRequest);
//here dialog is already stored in dialogs in DialogManager
InviteServerTransaction inviteServerTransaction = (InviteServerTransaction)
TransactionManager.getInstance().createServerTransaction(sipResponse,
Utils.getInstance().getSipPort(), RFC3261.TRANSPORT_UDP, this,
sipRequest);
inviteServerTransaction.start();
inviteServerTransaction.receivedRequest(sipRequest);
//TODO send 180 more than once
inviteServerTransaction.sendReponse(sipResponse);
setChanged();
notifyObservers(new SipEvent(EventType.INCOMING_CALL, sipResponse));
dialog.receivedOrSent1xx();
List<String> peers = UserAgent.getInstance().getPeers();
String responseTo = sipRequest.getSipHeaders().get(
new SipHeaderFieldName(RFC3261.HDR_FROM)).getValue();
if (!peers.contains(responseTo)) {
peers.add(responseTo);
}
}
public void handleReInvite(SipRequest sipRequest) {
}
public void acceptCall(SipRequest sipRequest, Dialog dialog) {
SipHeaders reqHeaders = sipRequest.getSipHeaders();
SipHeaderFieldValue contentType =
reqHeaders.get(new SipHeaderFieldName(RFC3261.HDR_CONTENT_TYPE));
if (RFC3261.CONTENT_TYPE_SDP.equals(contentType)) {
//TODO
// String sdpResponse;
// try {
// sdpResponse = sdpManager.handleOffer(
// new String(sipRequest.getBody()));
// } catch (NoCodecException e) {
// sdpResponse = sdpManager.generateErrorResponse();
// }
} else {
// TODO manage empty bodies and non-application/sdp content type
}
//TODO if mode autoanswer just send 200 without asking any question
SipResponse sipResponse =
MidDialogRequestManager.generateMidDialogResponse(
sipRequest,
dialog,
RFC3261.CODE_200_OK,
RFC3261.REASON_200_OK);
// TODO 13.3 dialog invite-specific processing
// TODO timer if there is an Expires header in INVITE
// TODO 3xx
// TODO 486 or 600
String body;
if (sipRequest.getBody() != null
&& RFC3261.CONTENT_TYPE_SDP.equals(contentType.getValue())) {
// create response in 200
try {
body = sdpManager.handleOffer(sipRequest.getBody());
} catch (NoCodecException e) {
body = sdpManager.generateErrorResponse();
}
} else {
// create offer in 200
body = sdpManager.generateOffer();
}
sipResponse.setBody(body.getBytes());
SipHeaders respHeaders = sipResponse.getSipHeaders();
respHeaders.add(new SipHeaderFieldName(RFC3261.HDR_CONTENT_TYPE),
new SipHeaderFieldValue(RFC3261.CONTENT_TYPE_SDP));
// TODO determine port and transport for server transaction>transport
// from initial invite
// FIXME determine port and transport for server transaction>transport
ServerTransaction serverTransaction =
TransactionManager.getInstance().getServerTransaction(sipRequest);
serverTransaction.start();
serverTransaction.receivedRequest(sipRequest);
serverTransaction.sendReponse(sipResponse);
// TODO manage retransmission of the response (send to the transport)
// until ACK arrives, if no ACK is received within 64*T1, confirm dialog
// and terminate it with a BYE
// Logger.getInstance().debug("before dialog.receivedOrSent2xx();");
// Logger.getInstance().debug("dialog state: " + dialog.getState());
dialog.receivedOrSent2xx();
// Logger.getInstance().debug("dialog state: " + dialog.getState());
// Logger.getInstance().debug("after dialog.receivedOrSent2xx();");
// setChanged();
// notifyObservers(sipRequest);
}
public void rejectCall(SipRequest sipRequest) {
//TODO generate 486, etc.
SipHeaders reqHeaders = sipRequest.getSipHeaders();
SipHeaderFieldValue to = reqHeaders.get(
new SipHeaderFieldName(RFC3261.HDR_FROM));
String remoteUri = to.getValue();
if (remoteUri.indexOf(RFC3261.LEFT_ANGLE_BRACKET) > -1) {
remoteUri = NameAddress.nameAddressToUri(remoteUri);
}
Dialog dialog = DialogManager.getInstance().getDialog(remoteUri);
//TODO manage auto reject Do not disturb (DND)
SipResponse sipResponse =
MidDialogRequestManager.generateMidDialogResponse(
sipRequest,
dialog,
RFC3261.CODE_486_BUSYHERE,
RFC3261.REASON_486_BUSYHERE);
// TODO determine port and transport for server transaction>transport
// from initial invite
// FIXME determine port and transport for server transaction>transport
ServerTransaction serverTransaction =
TransactionManager.getInstance().getServerTransaction(sipRequest);
serverTransaction.start();
serverTransaction.receivedRequest(sipRequest);
serverTransaction.sendReponse(sipResponse);
dialog.receivedOrSent300To699();
// setChanged();
// notifyObservers(sipRequest);
}
//////////////////////////////////////////////////////////
// UAC methods
//////////////////////////////////////////////////////////
public ClientTransaction preProcessInvite(SipRequest sipRequest) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -