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

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

http://mobicents.googlecode.com/
Java | 198 lines | 137 code | 31 blank | 30 comment | 17 complexity | 9876fe677952f8e21a8ec22debe2546b 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.RestartInProgress;
 28import jain.protocol.ip.mgcp.message.RestartInProgressResponse;
 29import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 30import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
 31import jain.protocol.ip.mgcp.message.parms.RestartMethod;
 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
 43/**
 44 * Parse/encode RSIP commands.
 45 * 
 46 * @author Tom Uijldert
 47 * 
 48 */
 49public class RestartInProgressHandler extends TransactionHandler {
 50
 51	private static final Logger logger = Logger.getLogger(RestartInProgressHandler.class);
 52
 53	private RestartInProgress command;
 54	private RestartInProgressResponse response;
 55
 56	public RestartInProgressHandler(JainMgcpStackImpl stack) {
 57		super(stack);
 58	}
 59
 60	public RestartInProgressHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
 61		super(stack, address, port);
 62	}
 63
 64	@Override
 65	public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
 66		Utils utils = utilsFactory.allocate();
 67		MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
 68		try {
 69			parser.parse(message);
 70		} catch (Exception e) {
 71			throw new ParseException(e.getMessage(), -1);
 72		} finally {
 73			utilsFactory.deallocate(utils);
 74		}
 75		return command;
 76	}
 77
 78	@Override
 79	public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
 80		Utils utils = utilsFactory.allocate();
 81		MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
 82		try {
 83			parser.parse(message);
 84		} catch (IOException e) {
 85			// should never happen
 86		} finally {
 87			utilsFactory.deallocate(utils);
 88		}
 89		return response;
 90	}
 91
 92	@Override
 93	public String encode(JainMgcpCommandEvent event) {
 94		RestartInProgress rsip = (RestartInProgress) event;
 95		StringBuffer message = new StringBuffer();
 96		message.append("RSIP ").append(event.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
 97				rsip.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
 98
 99		message.append("RM:").append(rsip.getRestartMethod()).append(NEW_LINE);
100		if (rsip.getRestartDelay() != 0) {
101			message.append("RD:").append(rsip.getRestartDelay()).append(NEW_LINE);
102		}
103		if (rsip.getReasonCode() != null) {
104			message.append("E:").append(rsip.getReasonCode()).append(NEW_LINE);
105		}
106		return message.toString();
107	}
108
109	@Override
110	public String encode(JainMgcpResponseEvent event) {
111
112		RestartInProgressResponse response = (RestartInProgressResponse) event;
113		ReturnCode returnCode = response.getReturnCode();
114		StringBuffer s = new StringBuffer();
115		s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
116				SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
117
118		// TODO should utils.encodeNotifiedEntity decide on port?
119		if (response.getNotifiedEntity() != null) {
120			Utils utils = utilsFactory.allocate();
121			s.append("N:").append(utils.encodeNotifiedEntity(response.getNotifiedEntity())).append(NEW_LINE);
122			utilsFactory.deallocate(utils);
123
124		}
125		return s.toString();
126		// return msg;
127	}
128
129	private class CommandContentHandle implements MgcpContentHandler {
130		private Utils utils = null;
131
132		public CommandContentHandle(Utils utils) {
133			this.utils = utils;
134		}
135
136		public void header(String header) throws ParseException {
137
138
139			command = new RestartInProgress(source != null ? source : stack, endpoint, RestartMethod.Restart);
140			command.setTransactionHandle(remoteTID);
141		}
142
143		public void param(String name, String value) throws ParseException {
144			if (name.equalsIgnoreCase("RM")) {
145				command.setRestartMethod(utils.decodeRestartMethod(value));
146			} else if (name.equalsIgnoreCase("RD")) {
147				command.setRestartDelay(Integer.parseInt(value));
148			} else if (name.equalsIgnoreCase("E")) {
149				command.setReasonCode(utils.decodeReasonCode(value));
150			}
151		}
152
153		public void sessionDescription(String sd) throws ParseException {
154			throw new UnsupportedOperationException("Not supported yet.");
155		}
156	}
157
158	private class ResponseContentHandle implements MgcpContentHandler {
159		private Utils utils;
160
161		public ResponseContentHandle(Utils utils) {
162			this.utils = utils;
163		}
164
165		public void header(String header) throws ParseException {
166			String[] tokens = utils.splitStringBySpace(header);
167
168			int tid = Integer.parseInt(tokens[1]);
169			response = new RestartInProgressResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
170					.parseInt(tokens[0])));
171			response.setTransactionHandle(tid);
172		}
173
174		public void param(String name, String value) throws ParseException {
175			if (name.equalsIgnoreCase("N")) {
176				NotifiedEntity n = utils.decodeNotifiedEntity(value, true);
177				response.setNotifiedEntity(n);
178			} else {
179				logger.warn("Unidentified AUCX Response parameter " + name + " with value = " + value);
180			}
181		}
182
183		public void sessionDescription(String sd) throws ParseException {
184			throw new UnsupportedOperationException("Not supported yet.");
185		}
186	}
187
188	@Override
189	public JainMgcpResponseEvent getProvisionalResponse() {
190		RestartInProgressResponse provisionalResponse = null;
191		if (!sent) {
192			provisionalResponse = new RestartInProgressResponse(source != null ? source : stack,
193					ReturnCode.Transaction_Being_Executed);
194			provisionalResponse.setTransactionHandle(remoteTID);
195		}
196		return provisionalResponse;
197	}
198}