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

📄 cmainthread.java

📁 SMSLib一个很有用的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// SMSLib for Java
// An open-source API Library for sending and receiving SMS via a GSM modem.
// Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
// Web Site: http://www.smslib.org
//
// SMSLib is distributed under the LGPL license.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

//
// SMSServer for Java GUI Application.
// Please read _README.txt for further information.
//

package smsserver;

import java.io.*;
import java.util.*;
import javax.swing.*;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import org.smslib.*;

class CMainThread extends Thread
{
	protected SMSServer SmsServer;

	protected CSettings settings;

	protected CDatabase database;

	protected CService service;

	protected CMainWindow mainWindow;

	private boolean connectRequest;

	private boolean exitRequest;

	private boolean exitFinished;

	public CMainThread(SMSServer SmsServer, CMainWindow mainWindow, CSettings settings)
	{
		this.SmsServer = SmsServer;
		this.mainWindow = mainWindow;
		this.settings = settings;
	}

	public void initialize()
	{
		database = new CDatabase(settings, this);
		if (mainWindow != null) mainWindow.setConnected(false);
		exitRequest = false;
		exitFinished = false;
		connectRequest = false;
		start();
	}

	public boolean connect(boolean calledFromMenu)
	{
		try
		{
			if (mainWindow == null) System.out.println(CConstants.TEXT_CONNECTING);
			service = new CService(settings.getSerialDriverSettings().getPort(), settings.getSerialDriverSettings().getBaud(), settings.getPhoneSettings().getManufacturer(), settings
			        .getPhoneSettings().getModel());
			service.setSimPin(settings.getPhoneSettings().getSimPin());
			service.connect();
			service.setSmscNumber(settings.getPhoneSettings().getSmscNumber());
			if (mainWindow != null)
			{
				mainWindow.setManufText(service.getDeviceInfo().getManufacturer());
				mainWindow.setModelText(service.getDeviceInfo().getModel());
				mainWindow.setSerialNoText(service.getDeviceInfo().getSerialNo());
				mainWindow.setIMSIText(service.getDeviceInfo().getImsi());
				mainWindow.setSwVersionText(service.getDeviceInfo().getSwVersion());
				mainWindow.setBatteryIndicator(service.getDeviceInfo().getBatteryLevel());
				mainWindow.setSignalIndicator(service.getDeviceInfo().getSignalLevel());
				mainWindow.setStatusText(CConstants.STATUS_CONNECTED);
			}
			else
			{
				System.out.println(CConstants.TEXT_INFO);
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_MANUFACTURER) + " " + service.getDeviceInfo().getManufacturer());
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_MODEL) + " " + service.getDeviceInfo().getModel());
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_SERIALNO) + " " + service.getDeviceInfo().getSerialNo());
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_IMSI) + " " + service.getDeviceInfo().getImsi());
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_SWVERSION) + " " + service.getDeviceInfo().getSwVersion());
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_BATTERY) + " " + service.getDeviceInfo().getBatteryLevel() + "%");
				System.out.println("\t" + SmsServer.stripHtml(CConstants.LABEL_SIGNAL) + " " + service.getDeviceInfo().getSignalLevel() + "%");
			}
			if (settings.getDatabaseSettings().getEnabled()) try
			{
				database.open();
			}
			catch (Exception e)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_CANNOT_OPEN_DATABASE, CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_CANNOT_OPEN_DATABASE);
				database.close();
			}
			if (mainWindow != null) mainWindow.setConnected(true);
			else System.out.println(CConstants.LABEL_STATUS + CConstants.STATUS_CONNECTED);
			if (calledFromMenu) connectRequest = true;
			return true;
		}
		catch (InvalidPinException e)
		{
			if (!connectRequest)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_INVALID_PIN, CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_INVALID_PIN);
			}
			disconnect(false);
			return false;
		}
		catch (NoPinException e)
		{
			if (!connectRequest)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_NO_PIN, CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_NO_PIN);
			}
			disconnect(false);
			return false;
		}
		catch (NoPduSupportException e)
		{
			if (!connectRequest)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_NO_PDU_SUPPORT, CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_NO_PDU_SUPPORT);
			}
			disconnect(false);
			return false;
		}
		catch (NotConnectedException e)
		{
			if (!connectRequest)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_CANNOT_CONNECT + "\n" + e.getMessage(), CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_CANNOT_CONNECT + e.getMessage());
			}
			disconnect(false);
			return false;
		}
		catch (Exception e)
		{
			if (!connectRequest)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_CANNOT_CONNECT + "\n" + e.getMessage(), CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else
				{
					System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_CANNOT_CONNECT + e.getMessage());
					e.printStackTrace();
				}
			}
			disconnect(false);
			return false;
		}
	}

	public void disconnect(boolean calledFromMenu)
	{
		try { service.disconnect(); } catch (Exception e) {}
		if (settings.getDatabaseSettings().getEnabled()) database.close();
		if (mainWindow != null) mainWindow.setConnected(false);
		else System.out.println(CConstants.LABEL_STATUS + CConstants.STATUS_DISCONNECTED);
		if (calledFromMenu) connectRequest = false;
	}

	public boolean processMessage(CIncomingMessage message) throws Exception
	{
		if (mainWindow != null)
		{
			mainWindow.setInFrom(message.getOriginator());
			mainWindow.setInDate(message.getDate() != null ? message.getDate().toString() : "* N/A *");
			mainWindow.setInText(message.getText());
		}
		else
		{
			System.out.println(CConstants.TEXT_INMSG);
			System.out.println("\t" + CConstants.LABEL_INCOMING_FROM + message.getOriginator());
			System.out.println("\t" + CConstants.LABEL_INCOMING_DATE + message.getDate());
			System.out.println("\t" + CConstants.LABEL_INCOMING_TEXT + message.getText());
		}
		settings.getGeneralSettings().rawInLog(message);
		if (settings.getPhoneSettings().getXmlInQueue() != null) try
		{
			saveToXmlInQueue(message);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		if (settings.getPhoneSettings().getForwardNumber() != null) try
		{
			saveToXmlOutQueue(message);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		if (database.isOpen()) try
		{
			database.saveMessage(message);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return true;
	}

	public void run()
	{
		boolean proceed;

		while (!exitRequest)
		{
			try
			{
				sleep(settings.getPhoneSettings().getPeriodInterval());
			}
			catch (Exception e)
			{
			}
			proceed = false;
			if (!exitRequest)
			{
				if (service != null)
				{
					if (service.getConnected()) proceed = true;
					else if (connectRequest)
					{
						disconnect(false);
						proceed = connect(false);
					}
					if (proceed)
					{
						try
						{
							if (mainWindow != null) mainWindow.setStatusText(CConstants.STATUS_REFRESHING);
							else System.out.println(CConstants.LABEL_STATUS + CConstants.STATUS_REFRESHING);
							service.refreshDeviceInfo();
							if (mainWindow != null) mainWindow.setStatusText(CConstants.STATUS_PROCESS_IN);
							else System.out.println(CConstants.LABEL_STATUS + CConstants.STATUS_PROCESS_IN);
							processStoredMessages();
							if (mainWindow != null) mainWindow.setStatusText(CConstants.STATUS_PROCESS_OUT);
							else System.out.println(CConstants.LABEL_STATUS + CConstants.STATUS_PROCESS_OUT);
							if (settings.getPhoneSettings().getXmlOutQueue() != null) try
							{
								checkXmlOutQueue();

⌨️ 快捷键说明

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