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

📄 cmainthread.java

📁 gsm modem 发送短信 闪信 WAP PUSH开发包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//	jSMSEngine API.
//	An open-source API package for sending and receiving SMS via a GSM device.
//	Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
//		Web Site: http://www.jsmsengine.org
//
//	jSMSEngine is distributed under the GPL license.
//
//	This program is free software; you can redistribute it and/or
//	modify it under the terms of the GNU General Public License
//	version 2 as published by the Free Software Foundation
//
//	This program 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 General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//

//
//	jSMSServer GUI Application.
//	This application is based on the old jSMSServer GUI, and provides a general purpose
//		graphical interface. It can be used for a quick-start, if you don't want
//		to mess around with the API itself.
//	Please read jSMSServer.txt for further information.
//

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

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

import org.jsmsengine.*;

class CMainThread extends Thread
{
	protected jSMSServer jSmsServer;
	protected CSettings settings;
	protected CDatabase database;
	protected CService service;
	protected CMainWindow mainWindow;

	private boolean connectRequest;
	private boolean exitRequest;
	private boolean exitFinished;

	public CMainThread(jSMSServer jSmsServer, CMainWindow mainWindow, CSettings settings)
	{
		this.jSmsServer = jSmsServer;
		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 org.jsmsengine.CService(settings.getSerialDriverSettings().getPort(), settings.getSerialDriverSettings().getBaud(), settings.getPhoneSettings().getManufacturer(), settings.getPhoneSettings().getModel());
			if (settings.getPhoneSettings().getPhoneBookFile() != null) service.setPhoneBook(settings.getPhoneSettings().getPhoneBookFile());
			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" + jSmsServer.stripHtml(CConstants.LABEL_MANUFACTURER) + " " + service.getDeviceInfo().getManufacturer());
				System.out.println("\t" + jSmsServer.stripHtml(CConstants.LABEL_MODEL) + " " + service.getDeviceInfo().getModel());
				System.out.println("\t" + jSmsServer.stripHtml(CConstants.LABEL_SERIALNO) + " " + service.getDeviceInfo().getSerialNo());
				System.out.println("\t" + jSmsServer.stripHtml(CConstants.LABEL_IMSI) + " " + service.getDeviceInfo().getImsi());
				System.out.println("\t" + jSmsServer.stripHtml(CConstants.LABEL_SWVERSION) + " " + service.getDeviceInfo().getSwVersion());
				System.out.println("\t" + jSmsServer.stripHtml(CConstants.LABEL_BATTERY) + " " + service.getDeviceInfo().getBatteryLevel() + "%");
				System.out.println("\t" + jSmsServer.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 (CannotDisableIndicationsException e)
		{
			if (!connectRequest)
			{
				if (mainWindow != null) JOptionPane.showMessageDialog(mainWindow, CConstants.ERROR_CANNOT_DISABLE_INDICATIONS, CConstants.ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
				else System.out.println(CConstants.ERROR_TITLE + " " + CConstants.ERROR_CANNOT_DISABLE_INDICATIONS);
			}
			disconnect(false);
			return false;
		}
		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)
	{
		service.disconnect();
		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);

⌨️ 快捷键说明

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