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

📄 modemgateway.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
						msg = new StatusReportMessage(refNo, memIndex, getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2), cal1.getTime(), cal2.getTime());
						msg.setGatewayId(getGatewayId());
						getService().getLogger().logDebug("IN-DTLS: MI:" + msg.getMemIndex(), null, getGatewayId());
						msgList.add(msg);
						incInboundMessageCount();
					}
					else
					{
						line = line.replaceAll(",,", ", ,");
						tokens = new StringTokenizer(line, ",");
						tokens.nextToken();
						tokens.nextToken();
						originator = tokens.nextToken().replaceAll("\"", "");
						tokens.nextToken();
						dateStr = tokens.nextToken().replaceAll("\"", "");
						cal1.set(Calendar.YEAR, 2000 + Integer.parseInt(dateStr.substring(0, 2)));
						cal1.set(Calendar.MONTH, Integer.parseInt(dateStr.substring(3, 5)) - 1);
						cal1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateStr.substring(6, 8)));
						dateStr = tokens.nextToken().replaceAll("\"", "");
						cal1.set(Calendar.HOUR_OF_DAY, Integer.parseInt(dateStr.substring(0, 2)));
						cal1.set(Calendar.MINUTE, Integer.parseInt(dateStr.substring(3, 5)));
						cal1.set(Calendar.SECOND, Integer.parseInt(dateStr.substring(6, 8)));
						msgText = reader.readLine();
						if (msgText == null) break;
						msgText = msgText.trim();
						msg = new InboundMessage(cal1.getTime(), originator, msgText, memIndex, getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2));
						msg.setGatewayId(getGatewayId());
						getService().getLogger().logDebug("IN-DTLS: MI:" + msg.getMemIndex(), null, getGatewayId());
						msgList.add(msg);
						incInboundMessageCount();
					}
					while (true)
					{
						line = reader.readLine();
						if (line == null) break;
						line = line.trim();
						if (line.length() > 0) break;
					}
					if ((limit > 0) && (msgList.size() == limit)) break;
				}
				reader.close();
			}
		}
	}

	private boolean sendMessagePDU(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		boolean ok = false;
		List<String> pdus = msg.getPdus(getSmscNumber(), this.outMpRefNo);
		for (String pdu : pdus)
		{
			Pdu newPdu = new PduParser().parsePdu(pdu);
			getService().getLogger().logDebug(newPdu.toString(), null, getGatewayId());
			int j = pdu.length() / 2;
			if (getSmscNumber() == null)
			{
				// Do nothing on purpose!
			}
			else if (getSmscNumber().length() == 0) j--;
			else
			{
				int smscNumberLen = getSmscNumber().length();
				if (getSmscNumber().charAt(0) == '+') smscNumberLen--;
				if (smscNumberLen % 2 != 0) smscNumberLen++;
				int smscLen = (2 + smscNumberLen) / 2;
				j = j - smscLen - 1;
			}
			getService().getLogger().logDebug("Sending Pdu: " + pdu, null, getGatewayId());
			int refNo = getATHandler().sendMessage(j, pdu, null, null);
			if (refNo >= 0)
			{
				msg.setGatewayId(getGatewayId());
				msg.setRefNo(String.valueOf(refNo));
				msg.setDispatchDate(new Date());
				msg.setMessageStatus(MessageStatuses.SENT);
				incOutboundMessageCount();
				ok = true;
			}
			else
			{
				msg.setRefNo(null);
				msg.setDispatchDate(null);
				msg.setMessageStatus(MessageStatuses.FAILED);
				ok = false;
			}
		}
		if (pdus.size() > 1)
		{
			this.outMpRefNo = (this.outMpRefNo + 1) % 65536;
		}
		return ok;
	}

	private void readMessagesPDU(Collection<InboundMessage> msgList, MessageClasses messageClass, int myLimit) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int i, j, memIndex;
		String response, line, pduString;
		BufferedReader reader;
		int limit = (myLimit < 0 ? 0 : myLimit);
		for (int ml = 0; ml < (getATHandler().getStorageLocations().length() / 2); ml++)
		{
			if (getATHandler().switchStorageLocation(getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2)))
			{
				response = getATHandler().listMessages(messageClass);
				response = response.replaceAll("\\s+OK\\s+", "\nOK");
				reader = new BufferedReader(new StringReader(response));
				for (;;)
				{
					line = reader.readLine();
					if (line == null) break;
					line = line.trim();
					if (line.length() > 0) break;
				}
				// use the parser to determine the message type
				PduParser parser = new PduParser();
				while (true)
				{
					if (line == null) break;
					line = line.trim();
					if (line.length() <= 0 || line.equalsIgnoreCase("OK")) break;
					i = line.indexOf(':');
					j = line.indexOf(',');
					memIndex = Integer.parseInt(line.substring(i + 1, j).trim());
					pduString = reader.readLine().trim();
					try
					{
						getService().getLogger().logDebug("READ PDU: " + pduString, null, getGatewayId());
						// this will throw an exception for PDUs
						// it can't classify
						Pdu pdu = parser.parsePdu(pduString);
						// NOTE: maybe a message validity vs the current
						//       date should be put here.
						//       if the message is invalid, the message should
						//       be ignored and but logged
						if (pdu instanceof SmsDeliveryPdu)
						{
							getService().getLogger().logDebug(pdu.toString(), null, getGatewayId());
							InboundMessage msg;
							String memLocation = getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2);
							if (pdu.isBinary())
							{
								msg = new InboundBinaryMessage((SmsDeliveryPdu) pdu, memIndex, memLocation);
								if (getService().getKeyManager().getKey(msg.getOriginator()) != null) msg = new InboundEncryptedMessage((SmsDeliveryPdu) pdu, memIndex, memLocation);
							}
							else
							{
								msg = new InboundMessage((SmsDeliveryPdu) pdu, memIndex, memLocation);
							}
							msg.setGatewayId(getGatewayId());
							getService().getLogger().logDebug("IN-DTLS: MI:" + msg.getMemIndex() + " REF:" + msg.getMpRefNo() + " MAX:" + msg.getMpMaxNo() + " SEQ:" + msg.getMpSeqNo(), null, getGatewayId());
							if (msg.getMpRefNo() == 0)
							{
								// single message
								msgList.add(msg);
								incInboundMessageCount();
							}
							else
							{
								// multi-part message
								int k, l;
								List<InboundMessage> tmpList;
								InboundMessage listMsg;
								boolean found, duplicate;
								found = false;
								for (k = 0; k < this.mpMsgList.size(); k++)
								{
									// List of List<InboundMessage>
									tmpList = this.mpMsgList.get(k);
									listMsg = tmpList.get(0);
									// check if current message list is for this message
									if (listMsg.getMpRefNo() == msg.getMpRefNo())
									{
										duplicate = false;
										// check if the message is already in the message list
										for (l = 0; l < tmpList.size(); l++)
										{
											listMsg = tmpList.get(l);
											if (listMsg.getMpSeqNo() == msg.getMpSeqNo())
											{
												duplicate = true;
												break;
											}
										}
										if (!duplicate) tmpList.add(msg);
										found = true;
										break;
									}
								}
								if (!found)
								{
									// no existing list present for this message
									// add one
									tmpList = new ArrayList<InboundMessage>();
									tmpList.add(msg);
									this.mpMsgList.add(tmpList);
								}
							}
						}
						else if (pdu instanceof SmsStatusReportPdu)
						{
							StatusReportMessage msg;
							msg = new StatusReportMessage((SmsStatusReportPdu) pdu, memIndex, getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2));
							msg.setGatewayId(getGatewayId());
							msgList.add(msg);
							incInboundMessageCount();
						}
						else
						{
							// this theoretically will never happen, but it occasionally does with phones 
							// like some Sony Ericssons (e.g. Z610i, SENT messages are included in this list)
							// instead of throwing a RuntimeException, just ignore any messages that are not of type
							// SmsDeliveryPdu
							// SmsStatusReportPdu
							if (displayIllegalReceivedMessages)
							{
								getService().getLogger().logError("Wrong type of PDU detected: " + pdu.getClass().getName(), null, getGatewayId());
								getService().getLogger().logError("ERROR PDU: " + pduString, null, getGatewayId());
							}
						}
					}
					catch (Exception e)
					{
						// PduFactory will give an exception
						// for PDUs it can't understand
						UnknownMessage msg;
						msg = new UnknownMessage(pduString, memIndex, getATHandler().getStorageLocations().substring((ml * 2), (ml * 2) + 2));
						msg.setGatewayId(getGatewayId());
						msgList.add(msg);
						incInboundMessageCount();
						getService().getLogger().logError("Unhandled SMS in inbox, skipping...", e, getGatewayId());
						getService().getLogger().logError("ERROR PDU: " + pduString, null, getGatewayId());
					}
					while (true)
					{
						line = reader.readLine();
						if (line == null) break;
						line = line.trim();
						if (line.length() > 0) break;
					}
					if ((limit > 0) && (msgList.size() == limit)) break;
				}
				reader.close();
			}
		}
		checkMpMsgList(msgList);
	}

	private boolean displayIllegalReceivedMessages;

	public void setDisplayIllegalReceivedMessages(boolean b)
	{
		displayIllegalReceivedMessages = b;
	}

	public String getMessageByIndex(int msgIndex) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		synchronized (getDriver().getSYNCCommander())
		{
			return getATHandler().getMessageByIndex(msgIndex);
		}
	}

	private void checkMpMsgList(Collection<InboundMessage> msgList)
	{
		int k, l, m;
		List<InboundMessage> tmpList;
		InboundMessage listMsg, mpMsg;
		boolean found;
		mpMsg = null;
		getService().getLogger().logDebug("CheckMpMsgList(): MAINLIST: " + this.mpMsgList.size(), null, getGatewayId());
		for (k = 0; k < this.mpMsgList.size(); k++)
		{
			tmpList = this.mpMsgList.get(k);
			getService().getLogger().logDebug("CheckMpMsgList(): SUBLIST[" + k + "]: " + tmpList.size(), null, getGatewayId());
			listMsg = tmpList.get(0);
			found = false;
			if (listMsg.getMpMaxNo() == tmpList.size())
			{
				found = true;
				for (l = 0; l < tmpList.size(); l++)
					for (m = 0; m < tmpList.size(); m++)
					{
						listMsg = tmpList.get(m);
						if (listMsg.getMpSeqNo() == (l + 1))
						{
							if (listMsg.getMpSeqNo() == 1)
							{
								mpMsg = listMsg;
								mpMsg.setMpMemIndex(mpMsg.getMemIndex());
							}
							else
							{
								if (mpMsg != null)
								{
									if (mpMsg instanceof InboundBinaryMessage)
									{
										InboundBinaryMessage mpMsgBinary = (InboundBinaryMessage) mpMsg;
										InboundBinaryMessage listMsgBinary = (InboundBinaryMessage) listMsg;
										mpMsgBinary.addDataBytes(listMsgBinary.getDataBytes());
									}
									else
									{
										// NEW
										String textToAdd = listMsg.getText();
										if (mpMsg.getEndsWithMultiChar())
										{
											// adjust first char of textToAdd
											getService().getLogger().logDebug("Adjusting dangling multi-char: " + textToAdd.charAt(0) + " --> " + PduUtils.getMultiCharFor(textToAdd.charAt(0)), null, getGatewayId());
											textToAdd = PduUtils.getMultiCharFor(textToAdd.charAt(0)) + textToAdd.substring(1);
										}
										mpMsg.setEndsWithMultiChar(listMsg.getEndsWithMultiChar());
										try
										{
											mpMsg.addText(textToAdd);
										}
										catch (UnsupportedEncodingException e)
										{
											// TODO: What to do with this?
										}
									}
									mpMsg.setMpSeqNo(listMsg.getMpSeqNo());
									mpMsg.setMpMemIndex(listMsg.getMemIndex());
									if (listMsg.getMpSeqNo() == listMsg.getMpMaxNo())
									{
										mpMsg.setMemIndex(-1);
										msgList.add(mpMsg);
										incInboundMessageCount();
										mpMsg = null;
									}
								}
							}
							break;
						}
					}
				tmpList.clear();
				tmpList = null;
			}
			if (found)
			{
				this.mpMsgList.remove(k);
				k--;
			}
		}
	}

	/**
	 * Sets the SIM PIN.
	 * 
	 * @param mySimPin
	 *            The SIM PIN.
	 */
	public void setSimPin(String mySimPin)
	{
		this.simPin = mySimPin;
	}

⌨️ 快捷键说明

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