⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractgateway.java

📁 华为编程开发规范与案例, 华为编程开发规范与案例,华为编程开发规范与案例
💻 JAVA
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib.gateway;

import java.util.*;
import java.util.concurrent.*;
import org.apache.log4j.*;
import org.smslib.*;

abstract public class AbstractGateway
{
	public static class Attributes
	{
		public static final int SEND = 0x0001;

		public static final int RECEIVE = 0x0002;

		public static final int CUSTOMFROM = 0x0004;

		public static final int BIGMESSAGES = 0x0008;

		public static final int WAPSI = 0x0010;

		public static final int PORTADDRESSING = 0x0020;

		public static final int FLASHSMS = 0x0040;

		public static final int DELIVERYREPORTS = 0x0080;
	}

	public enum Protocol
	{
		TEXT, PDU
	};

	public enum Priority
	{
		HIGH, NORMAL, LOW
	};

	protected boolean started;

	protected String gatewayId;

	protected int attributes;

	protected boolean inbound;

	protected boolean outbound;

	protected Logger logger;

	protected Protocol protocol;

	protected IInboundMessageNotification inboundNotification;

	protected IOutboundMessageNotification outboundNotification;

	protected ICallNotification callNotification;

	protected BlockingQueue<OutboundMessage> lowQueue, normalQueue, highQueue;

	protected QueueManager lowQMan, normalQMan, highQMan;

	protected Statistics statistics;

	protected String from;

	public AbstractGateway(String id, Logger logger)
	{
		this.gatewayId = id;
		this.logger = logger;
		started = false;
		inbound = false;
		outbound = false;
		attributes = 0;
		protocol = Protocol.PDU;
		inboundNotification = null;
		outboundNotification = null;
		callNotification = null;
		lowQueue = new LinkedBlockingQueue<OutboundMessage>();
		normalQueue = new LinkedBlockingQueue<OutboundMessage>();
		highQueue = new LinkedBlockingQueue<OutboundMessage>();
		statistics = new Statistics();
	}

	public boolean isStarted()
	{
		return started;
	}

	public int getAttributes()
	{
		return attributes;
	}

	/**
	 * Returns true if the the gateway is set for inbound messaging.
	 * 
	 * @return True if this gateway is set for inbound messaging.
	 */
	public boolean isInbound()
	{
		return inbound;
	}

	/**
	 * Enables or disables the gateway for inbound messaging. The command is
	 * accepted only if the gateway supports inbound messaging.
	 * 
	 * @param value
	 *           True to enable the gateway for inbound messaging.
	 * @throws UnsupportedOperationException
	 *            The gateway does not support inbound messaging.
	 */
	public void setInbound(boolean value) throws UnsupportedOperationException
	{
		if ((attributes & Attributes.RECEIVE) == 0) throw new UnsupportedOperationException("Unsupported Gateway operation!");
		inbound = value;
	}

	/**
	 * Returns true if the the gateway is set for outbound messaging.
	 * 
	 * @return True if this gateway is set for outbound messaging.
	 */
	public boolean isOutbound()
	{
		return outbound;
	}

	/**
	 * Enables or disables the gateway for outbound messaging. The command is
	 * accepted only if the gateway supports outbound messaging.
	 * 
	 * @param value
	 *           True to enable the gateway for outbound messaging.
	 * @throws UnsupportedOperationException
	 *            The gateway does not support outbound messaging.
	 */
	public void setOutbound(boolean value) throws UnsupportedOperationException
	{
		if ((attributes & Attributes.SEND) == 0) throw new UnsupportedOperationException("Unsupported Gateway operation!");
		outbound = value;
	}

	public Logger getLogger()
	{
		return logger;
	}

	/**
	 * Sets the communication protocol of the gateway. The call is applicable
	 * only for modem gateways, in other cases it is ignored.
	 * 
	 * @param protocol
	 * @see Protocol
	 * @see #getProtocol
	 */
	public void setProtocol(Protocol protocol)
	{
		this.protocol = protocol;
	}

	/**
	 * Returns the communication protocol current in use by the gateway.
	 * 
	 * @return The communication protocol.
	 * @see Protocol
	 * @see #setProtocol(Protocol)
	 */
	public Protocol getProtocol()
	{
		return protocol;
	}

	/**
	 * Returns the gateway id assigned to this gateway during initialization.
	 * 
	 * @return The gateway id.
	 */
	public String getGatewayId()
	{
		return gatewayId;
	}

	/**
	 * Returns the notification method set for inbound messages. Returns null if
	 * no such method is set.
	 * 
	 * @return The notification method.
	 * @see #setInboundNotification(IInboundMessageNotification)
	 */
	public IInboundMessageNotification getInboundNotification()
	{
		return inboundNotification;
	}

	/**
	 * Sets the inbound message notification method. The method must adhere to
	 * the IInboundMessageNotification interface. If set, SMSLib will call this
	 * method upon arrival of a new inbound message.
	 * 
	 * @param inboundNotification
	 *           The method to be called.
	 * @see #getInboundNotification()
	 * @see IInboundMessageNotification
	 */
	public void setInboundNotification(IInboundMessageNotification inboundNotification)
	{
		this.inboundNotification = inboundNotification;
	}

	/**
	 * Returns the notification method set for outbound messages. Returns null if
	 * no such method is set.
	 * 
	 * @return The notification method.
	 * @see #setOutboundNotification(IOutboundMessageNotification)
	 */
	public IOutboundMessageNotification getOutboundNotification()
	{
		return outboundNotification;
	}

	/**
	 * Sets the outbound notification method. The method must adhere to the
	 * IOutboundMessageNotification interface. If set, SMSLib will call this
	 * method upon dispatch of a message through the queueing (asyncronous)
	 * calls.
	 * 
	 * @param outboundNotification
	 * @see #getOutboundNotification()
	 * @see IOutboundMessageNotification
	 */
	public void setOutboundNotification(IOutboundMessageNotification outboundNotification)
	{
		this.outboundNotification = outboundNotification;
	}

	public ICallNotification getCallNotification()
	{
		return callNotification;
	}

	public void setCallNotification(ICallNotification callNotification)
	{
		this.callNotification = callNotification;
	}

	/**
	 * Returns the total number of messages received by this gateway.
	 * 
	 * @return The number of received messages.
	 */
	public int getInboundMessageCount()
	{
		return statistics.inbound;
	}

	/**
	 * Returns the total number of messages sent via this gateway.
	 * 
	 * @return The number of sent messages.
	 */
	public int getOutboundMessageCount()
	{
		return statistics.outbound;
	}

	public String getFrom()
	{
		return from;
	}

	public void setFrom(String from)
	{
		this.from = from;
	}

	public void queueMessage(OutboundMessage msg, Priority priority)
	{
		LinkedList<OutboundMessage> msgList = new LinkedList<OutboundMessage>();
		msgList.add(msg);
		queueMessage(msgList, priority);
	}

	public void queueMessage(LinkedList<OutboundMessage> msgList, Priority priority)
	{
		for (int i = 0; i < msgList.size(); i++)
		{
			switch (priority)
			{
				case LOW:
					lowQueue.add(msgList.get(i));
					break;
				case NORMAL:
					normalQueue.add(msgList.get(i));
					break;
				case HIGH:
					highQueue.add(msgList.get(i));
					break;
			}
		}
	}

	public void startGateway() throws Exception
	{
		lowQMan = new QueueManager(lowQueue, Thread.MIN_PRIORITY);
		normalQMan = new QueueManager(normalQueue, Thread.NORM_PRIORITY);
		highQMan = new QueueManager(highQueue, Thread.MAX_PRIORITY);
		started = true;
	}

	public void stopGateway() throws Exception
	{
		started = false;
		lowQMan.interrupt();
		lowQMan.join();
		normalQMan.interrupt();
		normalQMan.join();
		highQMan.interrupt();
		highQMan.join();
	}

	abstract public void readMessages(LinkedList<InboundMessage> msgList, InboundMessage.MessageClass msgClass) throws Exception;

	abstract public InboundMessage readMessage(String memLoc, int memIndex) throws Exception;

	abstract public void sendMessage(OutboundMessage msg) throws Exception;

	public void sendMessage(OutboundMessage msg, Priority priority) throws Exception
	{
		sendMessage(msg);
	}

	public void sendMessages(java.util.LinkedList<org.smslib.OutboundMessage> msgList) throws java.lang.Exception
	{
		for (int i = 0; i < msgList.size(); i++)
		{
			sendMessage(msgList.get(i));
		}
	}

	abstract public void deleteMessage(InboundMessage msg) throws Exception;

	abstract public float queryBalance() throws Exception;

	abstract public boolean queryCoverage(OutboundMessage msg) throws Exception;

	/**
	 * Answers question if gateway offers services defined in attribute bitmask.
	 * 
	 * @param att
	 *           atribute bitmask
	 * @return true if gateway supports this capabilities
	 */
	public boolean isCapableOf(int att)
	{
		return ((att & attributes) == att);
	}

	/**
	 * Answers to a question
	 * 
	 * In fact, logical implication is performed.
	 * 
	 * @param attrib
	 *           Capability that is checked
	 * @param required
	 * @return true if gateway conforms to requested attribute.
	 */
	public boolean conformsTo(int attrib, boolean required)
	{
		if (required && !isCapableOf(attrib)) return false;
		else return true;
	}

	private class QueueManager extends Thread
	{
		private BlockingQueue<OutboundMessage> queue;

		public QueueManager(BlockingQueue<OutboundMessage> queue, int priority)
		{
			this.queue = queue;
			setPriority(priority);
			start();
		}

		public void run()
		{
			OutboundMessage msg = null;

			while (true)
			{
				try
				{
					if (started)
					{
						msg = queue.take();
						logger.debug("Queue unblocked.");
						switch (getPriority())
						{
							case MIN_PRIORITY:
								sendMessage(msg, Priority.LOW);
								break;
							case NORM_PRIORITY:
								sendMessage(msg, Priority.NORMAL);
								break;
							case MAX_PRIORITY:
								sendMessage(msg, Priority.HIGH);
								break;
						}
						if (getOutboundNotification() != null) getOutboundNotification().sent(gatewayId, msg);
					}
					sleep(5000);
				}
				catch (InterruptedException e)
				{
					break;
				}
				catch (Exception e)
				{
					try
					{
						if (msg != null) queue.put(msg);
					}
					catch (Exception e1)
					{
						logger.fatal("Queue failed to re-insert object: " + msg);
					}
				}
			}
		}
	}

	protected class Statistics
	{
		public int inbound;

		public int outbound;

		public Statistics()
		{
			inbound = 0;
			outbound = 0;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -