PageRenderTime 36ms CodeModel.GetById 19ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 297 lines | 163 code | 41 blank | 93 comment | 39 complexity | c51bf74d068d85257cb177f77df22f91 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.ModifyConnection;
 44import jain.protocol.ip.mgcp.message.ModifyConnectionResponse;
 45import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 46import jain.protocol.ip.mgcp.message.parms.ConnectionDescriptor;
 47import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 48import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 49import jain.protocol.ip.mgcp.message.parms.NotificationRequestParms;
 50import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
 51import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
 52import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 53
 54import java.io.IOException;
 55import java.net.InetAddress;
 56import java.text.ParseException;
 57
 58import org.apache.log4j.Logger;
 59import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
 60import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
 61import org.mobicents.protocols.mgcp.parser.Utils;
 62
 63/**
 64 * 
 65 * @author Oleg Kulikov
 66 * @author Pavel Mitrenko
 67 * @author amit bhayani
 68 */
 69public class ModifyConnectionHandler extends TransactionHandler {
 70	private ModifyConnection command = null;
 71	private ModifyConnectionResponse response = null;
 72
 73	private static final Logger logger = Logger.getLogger(ModifyConnectionHandler.class);
 74
 75	/** Creates a new instance of ModifyConnectionHandler */
 76	public ModifyConnectionHandler(JainMgcpStackImpl stack) {
 77		super(stack);
 78	}
 79
 80	public ModifyConnectionHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
 81		super(stack, address, port);
 82	}
 83
 84	public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
 85		Utils utils = utilsFactory.allocate();
 86		MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
 87		try {
 88			parser.parse(message);
 89		} catch (IOException e) {
 90			logger.error("Decoding of MDCX command failed", e);
 91		} finally {
 92			utilsFactory.deallocate(utils);
 93		}
 94
 95		NotifiedEntity notifiedEntity = command.getNotifiedEntity();
 96		if (command.getNotifiedEntity() != null) {
 97			this.stack.provider.setNotifiedEntity(notifiedEntity);
 98		}
 99		return command;
100	}
101
102	public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
103		Utils utils = utilsFactory.allocate();
104		MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
105		try {
106			parser.parse(message);
107		} catch (IOException e) {
108			logger.error("Decoding of MDCX Response failed", e);
109		} finally {
110			utilsFactory.deallocate(utils);
111		}
112
113		return response;
114	}
115
116	public String encode(JainMgcpCommandEvent event) {
117
118		// encode message header
119		Utils utils = utilsFactory.allocate();
120		ModifyConnection evt = (ModifyConnection) event;
121		StringBuffer s = new StringBuffer();
122		s.append("MDCX ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
123				evt.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
124
125		// encode mandatory parameters
126
127		s.append("C:").append(evt.getCallIdentifier()).append(NEW_LINE);
128		s.append("I:").append(evt.getConnectionIdentifier()).append(NEW_LINE);
129
130		// encode optional parameters
131
132		if (evt.getBearerInformation() != null) {
133			s.append("B:e:").append(evt.getBearerInformation()).append(NEW_LINE);
134		}
135
136		if (evt.getLocalConnectionOptions() != null) {
137			s.append("L:").append(utils.encodeLocalOptionValueList(evt.getLocalConnectionOptions())).append(NEW_LINE);
138		}
139
140		if (evt.getMode() != null) {
141			s.append("M:").append(evt.getMode()).append(NEW_LINE);
142		}
143
144		if (evt.getNotificationRequestParms() != null) {
145			s.append(utils.encodeNotificationRequestParms(evt.getNotificationRequestParms())).append(NEW_LINE);
146		}
147
148		if (evt.getNotifiedEntity() != null) {
149			s.append("N:").append(evt.getNotifiedEntity()).append(NEW_LINE);
150		}
151
152		if (evt.getRemoteConnectionDescriptor() != null) {
153			s.append(NEW_LINE).append(evt.getRemoteConnectionDescriptor());
154		}
155		utilsFactory.deallocate(utils);
156		return s.toString();
157	}
158
159	public String encode(JainMgcpResponseEvent event) {
160		ModifyConnectionResponse response = (ModifyConnectionResponse) event;
161		ReturnCode returnCode = response.getReturnCode();
162		StringBuffer s = new StringBuffer();
163		s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
164				SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
165
166		if (response.getLocalConnectionDescriptor() != null) {
167			s.append(NEW_LINE).append(response.getLocalConnectionDescriptor());
168		}
169
170		return s.toString();
171	}
172
173	private class CommandContentHandle implements MgcpContentHandler {
174		private Utils utils = null;
175
176		public CommandContentHandle(Utils utils) {
177			this.utils = utils;
178		}
179
180		/**
181		 * Receive notification of the header of a message. Parser will call
182		 * this method to report about header reading.
183		 * 
184		 * @param header
185		 *            the header from the message.
186		 */
187		public void header(String header) throws ParseException {
188
189			command = new ModifyConnection(source != null ? source : stack, new CallIdentifier("00"), endpoint,
190					new ConnectionIdentifier("00"));
191			command.setTransactionHandle(remoteTID);
192		}
193
194		/**
195		 * Receive notification of the parameter of a message. Parser will call
196		 * this method to report about parameter reading.
197		 * 
198		 * @param name
199		 *            the name of the paremeter
200		 * @param value
201		 *            the value of the parameter.
202		 */
203		public void param(String name, String value) throws ParseException {
204			if (name.equalsIgnoreCase("B")) {
205				command.setBearerInformation(utils.decodeBearerInformation(value));
206			} else if (name.equalsIgnoreCase("c")) {
207				command.setCallIdentifier(new CallIdentifier(value));
208			} else if (name.equalsIgnoreCase("I")) {
209				command.setConnectionIdentifier(new ConnectionIdentifier(value));
210			} else if (name.equalsIgnoreCase("m")) {
211				command.setMode(utils.decodeConnectionMode(value));
212			} else if (name.equalsIgnoreCase("L")) {
213				command.setLocalConnectionOptions(utils.decodeLocalOptionValueList(value));
214			} else if (name.equalsIgnoreCase("N")) {
215				command.setNotifiedEntity(utils.decodeNotifiedEntity(value, true));
216			} else if (name.equalsIgnoreCase("X")) {
217				command.setNotificationRequestParms(new NotificationRequestParms(new RequestIdentifier(value)));
218			} else if (name.equalsIgnoreCase("R")) {
219				command.getNotificationRequestParms().setRequestedEvents(utils.decodeRequestedEventList(value));
220			} else if (name.equalsIgnoreCase("S")) {
221				command.getNotificationRequestParms().setSignalRequests(utils.decodeEventNames(value));
222			} else if (name.equalsIgnoreCase("T")) {
223				command.getNotificationRequestParms().setDetectEvents(utils.decodeEventNames(value));
224			}
225		}
226
227		/**
228		 * Receive notification of the session description. Parser will call
229		 * this method to report about session descriptor reading.
230		 * 
231		 * @param sd
232		 *            the session description from message.
233		 */
234		public void sessionDescription(String sd) throws ParseException {
235			command.setRemoteConnectionDescriptor(new ConnectionDescriptor(sd));
236		}
237	}
238
239	private class ResponseContentHandle implements MgcpContentHandler {
240		private Utils utils;
241
242		public ResponseContentHandle(Utils utils) {
243			this.utils = utils;
244		}
245
246		/**
247		 * Receive notification of the header of a message. Parser will call
248		 * this method to report about header reading.
249		 * 
250		 * @param header
251		 *            the header from the message.
252		 */
253		public void header(String header) throws ParseException {
254			String[] tokens = utils.splitStringBySpace(header);
255			int tid = Integer.parseInt(tokens[1]);
256
257			response = new ModifyConnectionResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
258					.parseInt(tokens[0])));
259			response.setTransactionHandle(tid);
260		}
261
262		/**
263		 * Receive notification of the parameter of a message. Parser will call
264		 * this method to report about parameter reading.
265		 * 
266		 * @param name
267		 *            the name of the paremeter
268		 * @param value
269		 *            the value of the parameter.
270		 */
271		public void param(String name, String value) throws ParseException {
272			// hasn't parameters
273		}
274
275		/**
276		 * Receive notification of the session description. Parser will call
277		 * this method to report about session descriptor reading.
278		 * 
279		 * @param sd
280		 *            the session description from message.
281		 */
282		public void sessionDescription(String sd) throws ParseException {
283			response.setLocalConnectionDescriptor(new ConnectionDescriptor(sd));
284		}
285	}
286
287	@Override
288	public JainMgcpResponseEvent getProvisionalResponse() {
289		ModifyConnectionResponse provisionalresponse = null;
290		if (!sent) {
291			provisionalresponse = new ModifyConnectionResponse(source != null ? source : stack,
292					ReturnCode.Transaction_Being_Executed);
293		}
294		return provisionalresponse;
295	}
296
297}