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

📄 amodemdriver.java

📁 java 操作短消息api,有5个列子工大家参考.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

		public synchronized void put(byte c)
		{
			if (this.buffer == null) this.buffer = new byte[getGateway().getService().S.SERIAL_BUFFER_SIZE];
			this.buffer[this.bufferEnd] = c;
			this.bufferEnd++;
			if (this.bufferEnd == getGateway().getService().S.SERIAL_BUFFER_SIZE) this.bufferEnd = 0;
			if (getGateway().getService().S.DEBUG_QUEUE) getGateway().getService().getLogger().logDebug("IN READER QUEUE : " + (int) c + " / " + (char) c, null, getGateway().getGatewayId());
			notifyAll();
		}

		public synchronized void put(String s)
		{
			for (int i = 0; i < s.length(); i++)
				put((byte) s.charAt(i));
		}

		public synchronized byte get() throws TimeoutException, InterruptedException
		{
			byte c;
			if (this.buffer == null) this.buffer = new byte[getGateway().getService().S.SERIAL_BUFFER_SIZE];
			while (true)
			{
				try
				{
					if (this.bufferStart == this.bufferEnd) wait(getGateway().getService().S.SERIAL_TIMEOUT);
					if (this.bufferStart == this.bufferEnd) throw new TimeoutException("No response from device.");
					c = this.buffer[this.bufferStart];
					this.bufferStart++;
					if (this.bufferStart == getGateway().getService().S.SERIAL_BUFFER_SIZE) this.bufferStart = 0;
					return c;
				}
				catch (InterruptedException e)
				{
					if (getGateway().getGatewayStatus() == GatewayStatuses.RUNNING) getGateway().getService().getLogger().logWarn("Ignoring InterruptedException in Queue.get().", null, getGateway().getGatewayId());
					else
					{
						getGateway().getService().getLogger().logWarn("Re-throwing InterruptedException in Queue.get() - should be during shutdown...", null, getGateway().getGatewayId());
						throw new InterruptedException();
					}
				}
			}
		}

		public synchronized byte peek() throws InterruptedException
		{
			if (this.buffer == null) this.buffer = new byte[getGateway().getService().S.SERIAL_BUFFER_SIZE];
			while (true)
			{
				try
				{
					if (this.bufferStart == this.bufferEnd) wait(getGateway().getService().S.SERIAL_TIMEOUT);
					if (this.bufferStart == this.bufferEnd) return -1;
					return this.buffer[this.bufferStart];
				}
				catch (InterruptedException e)
				{
					if (getGateway().getGatewayStatus() == GatewayStatuses.RUNNING) getGateway().getService().getLogger().logWarn("Ignoring InterruptedException in Queue.peek().", e, getGateway().getGatewayId());
					else
					{
						getGateway().getService().getLogger().logWarn("Re-throwing InterruptedException in Queue.peek() - should be during shutdown...", e, getGateway().getGatewayId());
						throw new InterruptedException();
					}
				}
			}
		}

		public synchronized String peek(int sizeToRead)
		{
			int i, size;
			StringBuffer result;
			if (this.buffer == null) this.buffer = new byte[getGateway().getService().S.SERIAL_BUFFER_SIZE];
			size = sizeToRead;
			if (this.bufferStart == this.bufferEnd) return "";
			result = new StringBuffer(size);
			i = this.bufferStart;
			while (size > 0)
			{
				if ((this.buffer[i] != 0x0a) && (this.buffer[i] != 0x0d))
				{
					result.append((char) this.buffer[i]);
					size--;
				}
				i++;
				if (i == getGateway().getService().S.SERIAL_BUFFER_SIZE) i = 0;
				if (i == this.bufferEnd) break;
			}
			return result.toString();
		}

		public synchronized void clear()
		{
			this.bufferStart = 0;
			this.bufferEnd = 0;
		}

		public void dump()
		{
			int i;
			i = this.bufferStart;
			while (i < this.bufferEnd)
			{
				System.out.println(this.buffer[i] + " -> " + (char) this.buffer[i]);
				i++;
			}
		}
	}

	private class ModemReader extends Thread
	{
		public ModemReader()
		{
			start();
			getGateway().getService().getLogger().logDebug("ModemReader thread started.", null, getGateway().getGatewayId());
		}

		@Override
		public void run()
		{
			int c;
			String data;
			while (AModemDriver.this.connected)
			{
				try
				{
					synchronized (AModemDriver.this.SYNC_Reader)
					{
						if (!AModemDriver.this.dataReceived) AModemDriver.this.SYNC_Reader.wait();
						if (!AModemDriver.this.connected) break;
						c = read();
						while (c != -1)
						{
							AModemDriver.this.queue.put((byte) c);
							if (!portHasData()) break;
							c = read();
						}
						AModemDriver.this.dataReceived = false;
					}
					data = AModemDriver.this.queue.peek(6);
					for (int i = 0; i < getGateway().getATHandler().getUnsolicitedResponses().length; i++)
						if (data.indexOf(getGateway().getATHandler().getUnsolicitedResponse(i)) >= 0)
						{
							AModemDriver.this.keepAlive.interrupt();
							break;
						}
				}
				catch (InterruptedException e)
				{
					if (!AModemDriver.this.connected) break;
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
			getGateway().getService().getLogger().logDebug("ModemReader thread ended.", null, getGateway().getGatewayId());
		}
	}

	private class KeepAlive extends Thread
	{
		public KeepAlive()
		{
			setPriority(MIN_PRIORITY);
			start();
			getGateway().getService().getLogger().logDebug("ModemDriver: KeepAlive thread started.", null, getGateway().getGatewayId());
		}

		@Override
		public void run()
		{
			while (true)
			{
				try
				{
					try
					{
						sleep(getGateway().getService().S.SERIAL_KEEPALIVE_INTERVAL);
					}
					catch (InterruptedException e)
					{
						// Swallow this...
					}
					if (!AModemDriver.this.connected) break;
					if (getGateway().getGatewayStatus() == GatewayStatuses.RUNNING)
					{
						synchronized (AModemDriver.this.SYNC_Commander)
						{
							if (!AModemDriver.this.connected) break;
							getGateway().getService().getLogger().logDebug("** KeepAlive START **", null, getGateway().getGatewayId());
							try
							{
								if (!getGateway().getATHandler().isAlive()) getGateway().setGatewayStatus(GatewayStatuses.RESTART);
							}
							catch (Exception e)
							{
								getGateway().setGatewayStatus(GatewayStatuses.RESTART);
							}
							getGateway().getService().getLogger().logDebug("** KeepAlive END **", null, getGateway().getGatewayId());
						}
					}
				}
				catch (Exception e)
				{
					getGateway().getService().getLogger().logError("ModemDriver: KeepAlive Error.", e, getGateway().getGatewayId());
					getGateway().setGatewayStatus(GatewayStatuses.RESTART);
				}
			}
			getGateway().getService().getLogger().logDebug("ModemDriver: KeepAlive thread ended.", null, getGateway().getGatewayId());
		}
	}

	private class AsyncNotifier extends Thread
	{
		class Event
		{
			AsyncEvents event;

			String response;

			public Event(AsyncEvents myEvent, String myResponse)
			{
				this.event = myEvent;
				this.response = myResponse;
			}

			@Override
			public String toString()
			{
				return "Event: " + this.event + " / Response: " + this.response;
			}
		}

		private BlockingQueue<Event> eventQueue;

		private Object SYNC;

		public AsyncNotifier()
		{
			this.SYNC = new Object();
			this.eventQueue = new LinkedBlockingQueue<Event>();
			setPriority(MIN_PRIORITY);
			start();
			getGateway().getService().getLogger().logDebug("AsyncNotifier thread started.", null, getGateway().getGatewayId());
		}

		protected void setEvent(AsyncEvents event, String response)
		{
			synchronized (this.SYNC)
			{
				Event ev = new Event(event, response);
				getGateway().getService().getLogger().logDebug("Storing AsyncEvent: " + ev, null, getGateway().getGatewayId());
				this.eventQueue.add(ev);
				this.SYNC.notify();
			}
		}

		protected String getMemLoc(String indication)
		{
			Pattern p = Pattern.compile("\\+?\"\\S+\"");
			Matcher m = p.matcher(indication);
			if (m.find()) return indication.substring(m.start(), m.end()).replaceAll("\"", "");
			return "";
		}

		protected int getMemIndex(String indication)
		{
			Pattern p = Pattern.compile("\\+?\\d+");
			Matcher m = p.matcher(indication);
			if (m.find()) return Integer.parseInt(indication.substring(m.start(), m.end()).replaceAll("\"", ""));
			return -1;
		}

		protected String getOriginator(String indication)
		{
			Pattern p = Pattern.compile("\\+?\"\\S+\"");
			Matcher m = p.matcher(indication);
			if (m.find()) return indication.substring(m.start(), m.end()).replaceAll("\"", "");
			return "";
		}

		@SuppressWarnings("deprecation")
		@Override
		public void run()
		{
			String response;
			Event event;
			while (AModemDriver.this.connected)
			{
				try
				{
					event = this.eventQueue.take();
					getGateway().getService().getLogger().logDebug("Processing AsyncEvent: " + event, null, getGateway().getGatewayId());
					if (event.event == AsyncEvents.INBOUNDMESSAGE)
					{
						getGateway().getService().getLogger().logDebug("Inbound message detected!", null, getGateway().getGatewayId());
						event.event = AsyncEvents.NOTHING;
						response = event.response;
						AModemDriver.this.asyncMessageProcessor.setProcess();
					}
					else if (event.event == AsyncEvents.INBOUNDSTATUSREPORTMESSAGE)
					{
						getGateway().getService().getLogger().logDebug("Inbound status report message detected!", null, getGateway().getGatewayId());
						event.event = AsyncEvents.NOTHING;
						response = event.response;
						AModemDriver.this.asyncMessageProcessor.setProcess();
					}
					else if (event.event == AsyncEvents.INBOUNDCALL)
					{
						getGateway().getService().getLogger().logDebug("Inbound call detected!", null, getGateway().getGatewayId());
						event.event = AsyncEvents.NOTHING;
						synchronized (AModemDriver.this.SYNC_Commander)
						{
							getGateway().getATHandler().switchToCmdMode();
							getGateway().getModemDriver().write("ATH\r");
							getGateway().getModemDriver().getResponse();
							response = event.response;
						}
						if (getGateway().getCallNotification() != null) getGateway().getCallNotification().process(getGateway().getGatewayId(), getOriginator(response));
						if (getGateway().getService().getCallNotification() != null) getGateway().getService().getCallNotification().process(getGateway().getGatewayId(), getOriginator(response));
					}
				}
				catch (InterruptedException e)
				{
					if (!AModemDriver.this.connected) break;
				}
				catch (GatewayException e)
				{
					// Swallow this...
				}
				catch (IOException e)
				{
					// Swallow this...
				}
				catch (TimeoutException e)
				{
					// Swallow this...
				}
			}
			AModemDriver.this.gateway.getService().getLogger().logDebug("AsyncNotifier thread ended.", null, getGateway().getGatewayId());
		}
	}

	private class AsyncMessageProcessor extends Thread
	{
		private List<InboundMessage> msgList;

		private Object SYNC;

		private boolean process;

		public AsyncMessageProcessor()
		{
			this.msgList = new ArrayList<InboundMessage>();
			this.SYNC = new Object();
			this.process = false;
			setPriority(MAX_PRIORITY);
			start();
			getGateway().getService().getLogger().logDebug("AsyncMessageProcessor thread started.", null, getGateway().getGatewayId());
		}

		public void setProcess()
		{
			synchronized (this.SYNC)
			{
				if (this.process) return;
				this.process = true;
				this.SYNC.notify();
			}
		}

		@SuppressWarnings("deprecation")
		@Override
		public void run()
		{
			while (AModemDriver.this.connected)
			{
				try
				{
					synchronized (this.SYNC)
					{
						if (!this.process)
						{
							this.SYNC.wait();
							if (!AModemDriver.this.connected) break;
						}
					}
					if ((getGateway().getInboundNotification() != null) || (getGateway().getService().getInboundNotification() != null))
					{
						getGateway().readMessages(this.msgList, MessageClasses.ALL);
						for (InboundMessage msg : this.msgList)
						{
							switch (msg.getType())
							{
								case INBOUND:
									if (getGateway().getInboundNotification() != null) getGateway().getInboundNotification().process(getGateway().getGatewayId(), MessageTypes.INBOUND, msg);
									if (getGateway().getService().getInboundNotification() != null) getGateway().getService().getInboundNotification().process(getGateway().getGatewayId(), MessageTypes.INBOUND, msg);
									break;
								case STATUSREPORT:
									if (getGateway().getInboundNotification() != null) getGateway().getInboundNotification().process(getGateway().getGatewayId(), MessageTypes.STATUSREPORT, msg);
									if (getGateway().getService().getInboundNotification() != null) getGateway().getService().getInboundNotification().process(getGateway().getGatewayId(), MessageTypes.STATUSREPORT, msg);
									break;
								default:
									break;
							}
						}
					}
					this.msgList.clear();
					this.process = false;
				}
				catch (InterruptedException e)
				{
					if (!AModemDriver.this.connected) break;
				}
				catch (GatewayException e)
				{
					// Swallow this...
				}
				catch (IOException e)
				{
					// Swallow this...
				}
				catch (TimeoutException e)
				{
					// Swallow this...
				}
			}
			getGateway().getService().getLogger().logDebug("AsyncMessageProcessor thread ended.", null, getGateway().getGatewayId());
		}
	}

	void setLastError(int myLastError)
	{
		this.lastError = myLastError;
	}

	public int getLastError()
	{
		return this.lastError;
	}

	public String getLastErrorText()
	{
		if (getLastError() == 0) return "OK";
		else if (getLastError() == -1) return "Invalid or empty response";
		else if ((getLastError() / 1000) == 5) return "CME Error " + (getLastError() % 1000);
		else if ((getLastError() / 1000) == 6) return "CMS Error " + (getLastError() % 1000);
		else return "Error: unknown";
	}

	public boolean isOk()
	{
		return (getLastError() == OK);
	}

	ModemGateway getGateway()
	{
		return this.gateway;
	}
}

⌨️ 快捷键说明

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