PageRenderTime 66ms CodeModel.GetById 28ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://mobicents.googlecode.com/
Java | 232 lines | 108 code | 32 blank | 92 comment | 5 complexity | f61e2891165739c5e860069fc18a8385 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     : EndpointConfigurationHandle.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.EndpointConfiguration;
 44import jain.protocol.ip.mgcp.message.EndpointConfigurationResponse;
 45import jain.protocol.ip.mgcp.message.parms.BearerInformation;
 46import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 47import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 48
 49import java.net.InetAddress;
 50import java.text.ParseException;
 51
 52import org.apache.log4j.Logger;
 53import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
 54import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
 55import org.mobicents.protocols.mgcp.parser.Utils;
 56
 57/**
 58 * 
 59 * @author Oleg Kulikov
 60 * @author Pavel Mitrenko
 61 */
 62public class EndpointConfigurationHandler extends TransactionHandler {
 63
 64	private EndpointConfiguration command;
 65	private EndpointConfigurationResponse response;
 66
 67	private static final Logger logger = Logger.getLogger(EndpointConfigurationHandler.class);
 68
 69	/** Creates a new instance of EndpointConfigurationHandle */
 70	public EndpointConfigurationHandler(JainMgcpStackImpl stack) {
 71		super(stack);
 72	}
 73
 74	public EndpointConfigurationHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
 75		super(stack, address, port);
 76	}
 77
 78	public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
 79		Utils utils = utilsFactory.allocate();
 80		MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
 81		try {
 82			parser.parse(message);
 83		} catch (Exception e) {
 84			// should never happen
 85			logger.error("Parsing of EndpointConfiguration command failed", e);
 86		} finally {
 87			utilsFactory.deallocate(utils);
 88		}
 89
 90		return command;
 91	}
 92
 93	public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
 94		Utils utils = utilsFactory.allocate();
 95		MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
 96		try {
 97			parser.parse(message);
 98		} catch (Exception e) {
 99			// should never happen
100			logger.error("Parsing of EndpointConfiguration Response failed", e);
101		} finally {
102			utilsFactory.deallocate(utils);
103		}
104		return response;
105	}
106
107	public String encode(JainMgcpCommandEvent event) {
108		// encode message header
109		EndpointConfiguration evt = (EndpointConfiguration) event;
110		StringBuffer s = new StringBuffer();
111		s.append("EPCF ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
112				evt.getEndpointIdentifier()).append(MGCP_VERSION).append(NEW_LINE);
113
114		// encode mandatory parameters
115		s.append("B:e:").append(evt.getBearerInformation()).append(NEW_LINE);
116
117		return s.toString();
118	}
119
120	public String encode(JainMgcpResponseEvent event) {
121		EndpointConfigurationResponse response = (EndpointConfigurationResponse) event;
122		ReturnCode returnCode = response.getReturnCode();
123		StringBuffer s = new StringBuffer();
124		s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
125				SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
126
127		return s.toString();
128	}
129
130	private class CommandContentHandle implements MgcpContentHandler {
131		private Utils utils = null;
132
133		public CommandContentHandle(Utils utils) {
134			this.utils = utils;
135		}
136
137		/**
138		 * Receive notification of the header of a message. Parser will call
139		 * this method to report about header reading.
140		 * 
141		 * @param header
142		 *            the header from the message.
143		 */
144		public void header(String header) throws ParseException {
145
146			command = new EndpointConfiguration(source != null ? source : stack, endpoint, BearerInformation.EncMethod_A_Law);
147			command.setTransactionHandle(remoteTID);
148		}
149
150		/**
151		 * Receive notification of the parameter of a message. Parser will call
152		 * this method to report about parameter reading.
153		 * 
154		 * @param name
155		 *            the name of the paremeter
156		 * @param value
157		 *            the value of the parameter.
158		 */
159		public void param(String name, String value) throws ParseException {
160			if (name.equalsIgnoreCase("B")) {
161				command.setBearerInformation(utils.decodeBearerInformation(value));
162			}
163		}
164
165		/**
166		 * Receive notification of the session description. Parser will call
167		 * this method to report about session descriptor reading.
168		 * 
169		 * @param sd
170		 *            the session description from message.
171		 */
172		public void sessionDescription(String sd) throws ParseException {
173		}
174	}
175
176	private class ResponseContentHandle implements MgcpContentHandler {
177		private Utils utils = null;
178
179		public ResponseContentHandle(Utils utils) {
180			this.utils = utils;
181		}
182
183		/**
184		 * Receive notification of the header of a message. Parser will call
185		 * this method to report about header reading.
186		 * 
187		 * @param header
188		 *            the header from the message.
189		 */
190		public void header(String header) throws ParseException {
191			String[] tokens = utils.splitStringBySpace(header);
192
193			int tid = Integer.parseInt(tokens[1]);
194			response = new EndpointConfigurationResponse(source != null ? source : stack, utils
195					.decodeReturnCode(Integer.parseInt(tokens[0])));
196			response.setTransactionHandle(tid);
197		}
198
199		/**
200		 * Receive notification of the parameter of a message. Parser will call
201		 * this method to report about parameter reading.
202		 * 
203		 * @param name
204		 *            the name of the paremeter
205		 * @param value
206		 *            the value of the parameter.
207		 */
208		public void param(String name, String value) throws ParseException {
209		}
210
211		/**
212		 * Receive notification of the session description. Parser will call
213		 * this method to report about session descriptor reading.
214		 * 
215		 * @param sd
216		 *            the session description from message.
217		 */
218		public void sessionDescription(String sd) throws ParseException {
219		}
220	}
221
222	@Override
223	public JainMgcpResponseEvent getProvisionalResponse() {
224		EndpointConfigurationResponse provisionalresponse = null;
225		if (!sent) {
226			provisionalresponse = new EndpointConfigurationResponse(source != null ? source : stack,
227					ReturnCode.Transaction_Being_Executed);
228		}
229		return provisionalresponse;
230	}
231
232}