PageRenderTime 38ms CodeModel.GetById 19ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/jain-mgcp/stack/src/main/java/org/mobicents/protocols/mgcp/stack/DeleteConnectionHandler.java

http://mobicents.googlecode.com/
Java | 294 lines | 154 code | 50 blank | 90 comment | 36 complexity | 90f582f393af6b4eb640db111452034c MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23/*
 24 * File Name     : CreateConnectionHandle.java
 25 *
 26 * The JAIN MGCP API implementaion.
 27 *
 28 * The source code contained in this file is in in the public domain.
 29 * It can be used in any project or product without prior permission,
 30 * license or royalty payments. There is  NO WARRANTY OF ANY KIND,
 31 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
 32 * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 33 * AND DATA ACCURACY.  We do not warrant or make any representations
 34 * regarding the use of the software or the  results thereof, including
 35 * but not limited to the correctness, accuracy, reliability or
 36 * usefulness of the software.
 37 */
 38
 39package org.mobicents.protocols.mgcp.stack;
 40
 41import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
 42import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
 43import jain.protocol.ip.mgcp.message.DeleteConnection;
 44import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
 45import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 46import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 47import jain.protocol.ip.mgcp.message.parms.NotificationRequestParms;
 48import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
 49import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 50
 51import java.io.IOException;
 52import java.net.InetAddress;
 53import java.text.ParseException;
 54
 55import org.apache.log4j.Logger;
 56import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
 57import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
 58import org.mobicents.protocols.mgcp.parser.Utils;
 59
 60/**
 61 * 
 62 * @author Oleg Kulikov
 63 * @author Pavel Mitrenko
 64 */
 65public class DeleteConnectionHandler extends TransactionHandler {
 66
 67	private DeleteConnection command;
 68	private DeleteConnectionResponse response;
 69
 70	private static final Logger logger = Logger.getLogger(DeleteConnectionHandler.class);
 71
 72	/** Creates a new instance of CreateConnectionHandle */
 73	public DeleteConnectionHandler(JainMgcpStackImpl stack) {
 74		super(stack);
 75	}
 76
 77	public DeleteConnectionHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
 78		super(stack, address, port);
 79	}
 80
 81	public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
 82		Utils utils = utilsFactory.allocate();
 83		MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
 84		try {
 85			parser.parse(message);
 86		} catch (IOException e) {
 87			logger.error("Decode of DLCX command failed", e);
 88		} finally {
 89			utilsFactory.deallocate(utils);
 90		}
 91
 92		return command;
 93	}
 94
 95	public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
 96		Utils utils = utilsFactory.allocate();
 97		MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
 98		try {
 99			parser.parse(message);
100		} catch (Exception e) {
101			logger.error("Decode of DLCX Response failed", e);
102		} finally {
103			utilsFactory.deallocate(utils);
104		}
105
106		return response;
107	}
108
109	public String encode(JainMgcpCommandEvent event) {
110
111		// encode message header
112		Utils utils = utilsFactory.allocate();
113		DeleteConnection evt = (DeleteConnection) event;
114		StringBuffer s = new StringBuffer();
115		s.append("DLCX ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
116				evt.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
117
118		// encode optional parameters
119
120		if (evt.getBearerInformation() != null) {
121			s.append("B:e:").append(evt.getBearerInformation()).append(NEW_LINE);
122
123		}
124
125		if (evt.getCallIdentifier() != null) {
126			s.append("C:").append(evt.getCallIdentifier()).append(NEW_LINE);
127
128		}
129
130		if (evt.getConnectionIdentifier() != null) {
131			s.append("I:").append(evt.getConnectionIdentifier()).append(NEW_LINE);
132
133		}
134
135		if (evt.getConnectionParms() != null) {
136			s.append("P:").append(utils.encodeConnectionParms(evt.getConnectionParms())).append(NEW_LINE);
137
138		}
139
140		if (evt.getNotificationRequestParms() != null) {
141			s.append(utils.encodeNotificationRequestParms(evt.getNotificationRequestParms())).append(NEW_LINE);
142
143		}
144
145		if (evt.getReasonCode() != null) {
146			s.append("E:").append(evt.getReasonCode());
147
148		}
149		utilsFactory.deallocate(utils);
150		return s.toString();
151
152	}
153
154	public String encode(JainMgcpResponseEvent event) {
155		DeleteConnectionResponse response = (DeleteConnectionResponse) event;
156		Utils utils = utilsFactory.allocate();
157		ReturnCode returnCode = response.getReturnCode();
158		StringBuffer s = new StringBuffer();
159		s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
160				SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
161
162		if (response.getConnectionParms() != null) {
163			s.append("P:").append(utils.encodeConnectionParms(response.getConnectionParms())).append(NEW_LINE);
164
165		}
166
167		return s.toString();
168
169	}
170
171	public class CommandContentHandle implements MgcpContentHandler {
172		private Utils utils = null;
173
174		public CommandContentHandle(Utils utils) {
175			this.utils = utils;
176		}
177
178		/**
179		 * Receive notification of the header of a message. Parser will call this method to report about header reading.
180		 * 
181		 * @param header
182		 *            the header from the message.
183		 */
184		public void header(String header) throws ParseException {
185
186			command = new DeleteConnection(source != null ? source : stack, endpoint);
187			command.setTransactionHandle(remoteTID);
188		}
189
190		/**
191		 * Receive notification of the parameter of a message. Parser will call this method to report about parameter
192		 * reading.
193		 * 
194		 * @param name
195		 *            the name of the paremeter
196		 * @param value
197		 *            the value of the parameter.
198		 */
199		public void param(String name, String value) throws ParseException {
200			if (name.equalsIgnoreCase("B")) {
201				command.setBearerInformation(utils.decodeBearerInformation(value));
202			} else if (name.equalsIgnoreCase("c")) {
203				command.setCallIdentifier(new CallIdentifier(value));
204			} else if (name.equalsIgnoreCase("I")) {
205				command.setConnectionIdentifier(new ConnectionIdentifier(value));
206			} else if (name.equalsIgnoreCase("X")) {
207				command.setNotificationRequestParms(new NotificationRequestParms(new RequestIdentifier(value)));
208			} else if (name.equalsIgnoreCase("R")) {
209				command.getNotificationRequestParms().setRequestedEvents(utils.decodeRequestedEventList(value));
210			} else if (name.equalsIgnoreCase("S")) {
211				command.getNotificationRequestParms().setSignalRequests(utils.decodeEventNames(value));
212			} else if (name.equalsIgnoreCase("T")) {
213				command.getNotificationRequestParms().setDetectEvents(utils.decodeEventNames(value));
214			} else if (name.equalsIgnoreCase("P")) {
215				command.setConnectionParms(utils.decodeConnectionParms(value));
216			} else if (name.equalsIgnoreCase("E")) {
217				command.setReasonCode(utils.decodeReasonCode(value));
218			}
219		}
220
221		/**
222		 * Receive notification of the session description. Parser will call this method to report about session
223		 * descriptor reading.
224		 * 
225		 * @param sd
226		 *            the session description from message.
227		 */
228		public void sessionDescription(String sd) throws ParseException {
229			// do nothing
230		}
231	}
232
233	private class ResponseContentHandle implements MgcpContentHandler {
234		private Utils utils = null;
235
236		public ResponseContentHandle(Utils utils) {
237			this.utils = utils;
238		}
239
240		/**
241		 * Receive notification of the header of a message. Parser will call this method to report about header reading.
242		 * 
243		 * @param header
244		 *            the header from the message.
245		 */
246		public void header(String header) throws ParseException {
247			String[] tokens = utils.splitStringBySpace(header);
248
249			int tid = Integer.parseInt(tokens[1]);
250			response = new DeleteConnectionResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
251					.parseInt(tokens[0])));
252			response.setTransactionHandle(tid);
253		}
254
255		/**
256		 * Receive notification of the parameter of a message. Parser will call this method to report about parameter
257		 * reading.
258		 * 
259		 * @param name
260		 *            the name of the paremeter
261		 * @param value
262		 *            the value of the parameter.
263		 */
264		public void param(String name, String value) throws ParseException {
265			if (name.equalsIgnoreCase("P")) {
266				response.setConnectionParms(utils.decodeConnectionParms(value));
267			}
268		}
269
270		/**
271		 * Receive notification of the session description. Parser will call this method to report about session
272		 * descriptor reading.
273		 * 
274		 * @param sd
275		 *            the session description from message.
276		 */
277		public void sessionDescription(String sd) throws ParseException {
278			// not used
279		}
280	}
281
282	@Override
283	public JainMgcpResponseEvent getProvisionalResponse() {
284		DeleteConnectionResponse provisionalResponse = null;
285
286		if (!sent) {
287			provisionalResponse = new DeleteConnectionResponse(source != null ? source : stack,
288					ReturnCode.Transaction_Being_Executed);
289			provisionalResponse.setTransactionHandle(commandEvent.getTransactionHandle());
290		}
291		return provisionalResponse;
292	}
293
294}