📄 smsserver.java
字号:
package edu.soft.buaa.message.sms;
import java.util.*;
/**
* @author Hermitte
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class SMSServer {
public LinkedList receivedList=new LinkedList();
public static boolean CLOSE_SIGNAL=false;
private boolean isServerStarted=false;
private boolean hasNewSMS=false;
public static Object initLock=new Object();
private static SMSServer server=null;
public static void main(String[] args) {
SMSServer srv=SMSServer.getInstance();
srv.startServer();
try{
Thread.sleep(3000);
}catch(Exception ex){
ex.printStackTrace();
}
ShortMessage sm=new ShortMessage("13581828915","我好是不");
LinkedList tll=new LinkedList();
tll.add(sm);
srv.sendMessageList(tll);
try{
Thread.sleep(5000);
}catch(Exception ex){
ex.printStackTrace();
}
if(srv.hasNewSMS()){
LinkedList ll=srv.getNewMessageList();
for(int i=0;i<ll.size();i++){
ShortMessage sms= (ShortMessage)ll.get(i);
System.out.println(sms.getText());
}
}
try{
Thread.sleep(1000);
}catch(Exception ex){
ex.printStackTrace();
}
srv.closeServer();
}
public SMSServer(){
}
public static SMSServer getInstance(){
if(server==null){
synchronized(initLock){
if(server==null){
try{
server=new SMSServer();
}catch(Exception ex){
return null;
}
}
}
}
return server;
}
public void startServer(){
if(!isServerStarted){
SMSHelper sh=SMSHelper.getInstance();
sh.setPort("com5");
sh.setBaud(9600);
sh.setSMSCenterNo("+8613800100500");
if(sh.openConnection()){
ReceiveSMSThread recThread=new ReceiveSMSThread(this);
recThread.start();
isServerStarted=true;
System.out.println("SMS Server is starting......OK!");
}else{
System.out.println("SMS Server is starting......failed!");
}
}else{
System.out.println("SMS Server has been started already!");
}
}
public void sendMessageList(LinkedList ll){
SMSHelper sh=SMSHelper.getInstance();
if(!isServerStarted){
this.startServer();
}
sh.SendMessageListInThread(ll);
}
public LinkedList getNewMessageList(){
LinkedList ll=new LinkedList();
ll=(LinkedList)receivedList.clone();
receivedList.clear();
hasReadSMS();
return ll;
}
public boolean hasNewSMS(){
return hasNewSMS;
}
public void hasReadSMS(){
hasNewSMS=false;
}
public void notifyNewSMS(){
hasNewSMS=true;
}
public void closeServer(){
if(!isServerStarted){
System.out.println("SMS Server has NOT been started yet!");
}else{
ReceiveSMSThread.CLOSE_SIGNAL=true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -