PageRenderTime 35ms CodeModel.GetById 7ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 183 lines | 125 code | 34 blank | 24 comment | 11 complexity | a21ec05216e55abf7073fdeb27d60aa2 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
 23package org.mobicents.protocols.mgcp.stack;
 24
 25import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
 26import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
 27import jain.protocol.ip.mgcp.message.Notify;
 28import jain.protocol.ip.mgcp.message.NotifyResponse;
 29import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 30import jain.protocol.ip.mgcp.message.parms.EventName;
 31import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
 32import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 33
 34import java.io.IOException;
 35import java.net.InetAddress;
 36import java.text.ParseException;
 37
 38import org.apache.log4j.Logger;
 39import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
 40import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
 41import org.mobicents.protocols.mgcp.parser.Utils;
 42
 43public class NotifyHandler extends TransactionHandler {
 44
 45	private Notify command;
 46	private NotifyResponse response;
 47
 48	private static final Logger logger = Logger.getLogger(NotifyHandler.class);
 49
 50	public NotifyHandler(JainMgcpStackImpl stack) {
 51		super(stack);
 52	}
 53
 54	public NotifyHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
 55		super(stack, address, port);
 56	}
 57
 58	@Override
 59	public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
 60		Utils utils = utilsFactory.allocate();
 61		MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
 62		try {
 63			parser.parse(message);
 64		} catch (Exception e) {
 65			throw new ParseException(e.getMessage(), -1);
 66		} finally {
 67			utilsFactory.deallocate(utils);
 68		}
 69
 70		return command;
 71	}
 72
 73	@Override
 74	public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
 75		Utils utils = utilsFactory.allocate();
 76		MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
 77		try {
 78			parser.parse(message);
 79		} catch (IOException e) {
 80			logger.error("Something wrong while parsing the NOTIFY Response received", e);
 81		} finally {
 82			utilsFactory.deallocate(utils);
 83		}
 84
 85		return response;
 86	}
 87
 88	@Override
 89	public String encode(JainMgcpCommandEvent event) {
 90		Notify notify = (Notify) event;
 91		StringBuffer message = new StringBuffer();
 92		message.append("NTFY ").append(event.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
 93				notify.getEndpointIdentifier()).append(MGCP_VERSION).append(NEW_LINE);
 94
 95		if (notify.getNotifiedEntity() != null) {
 96			message.append("N: ").append(notify.getNotifiedEntity()).append(NEW_LINE);
 97		}
 98
 99		message.append("X: ").append(notify.getRequestIdentifier()).append(NEW_LINE);
100
101		Utils utils = utilsFactory.allocate();
102		message.append("O: ").append(utils.encodeEventNames(notify.getObservedEvents())).append(NEW_LINE);
103		utilsFactory.deallocate(utils);
104
105		return message.toString();
106	}
107
108	@Override
109	public String encode(JainMgcpResponseEvent event) {
110		StringBuffer s = new StringBuffer();
111		s.append(event.getReturnCode().getValue()).append(SINGLE_CHAR_SPACE).append(event.getTransactionHandle())
112				.append(SINGLE_CHAR_SPACE).append(event.getReturnCode().getComment()).append(NEW_LINE);
113		return s.toString();
114
115		// return event.getReturnCode().getValue() + " " +
116		// event.getTransactionHandle() + " "
117		// + event.getReturnCode().getComment() + "\n";
118	}
119
120	private class CommandContentHandle implements MgcpContentHandler {
121
122		private Utils utils = null;
123
124		public CommandContentHandle(Utils utils) {
125			this.utils = utils;
126		}
127
128		public void header(String header) throws ParseException {
129
130			command = new Notify(source != null ? source : stack, endpoint, new RequestIdentifier("0"), new EventName[] {});
131			command.setTransactionHandle(remoteTID);
132		}
133
134		public void param(String name, String value) throws ParseException {
135			if (name.equalsIgnoreCase("N")) {
136				command.setNotifiedEntity(utils.decodeNotifiedEntity(value, false));
137			} else if (name.equalsIgnoreCase("X")) {
138				command.setRequestIdentifier(new RequestIdentifier(value));
139			} else if (name.equalsIgnoreCase("O")) {
140				command.setObservedEvents(utils.decodeEventNames(value));
141			}
142		}
143
144		public void sessionDescription(String sd) throws ParseException {
145			throw new UnsupportedOperationException("Not supported yet.");
146		}
147	}
148
149	private class ResponseContentHandle implements MgcpContentHandler {
150		private Utils utils;
151
152		public ResponseContentHandle(Utils utils) {
153			this.utils = utils;
154		}
155
156		public void header(String header) throws ParseException {
157			String[] tokens = utils.splitStringBySpace(header);
158
159			int tid = Integer.parseInt(tokens[1]);
160			response = new NotifyResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
161					.parseInt(tokens[0])));
162			response.setTransactionHandle(tid);
163		}
164
165		public void param(String name, String value) throws ParseException {
166			throw new UnsupportedOperationException("Not supported yet.");
167		}
168
169		public void sessionDescription(String sd) throws ParseException {
170			throw new UnsupportedOperationException("Not supported yet.");
171		}
172	}
173
174	@Override
175	public JainMgcpResponseEvent getProvisionalResponse() {
176		NotifyResponse provisionalresponse = null;
177		if (!sent) {
178			provisionalresponse = new NotifyResponse(source != null ? source : stack,
179					ReturnCode.Transaction_Being_Executed);
180		}
181		return provisionalresponse;
182	}
183}