📄 smsserver.java
字号:
readMessages();
if (SMSServer.this.optRunOnce) break;
Thread.sleep(Integer.parseInt(getProperties().getProperty("settings.inbound_interval", "60")) * 1000);
}
}
catch (InterruptedException e)
{
getService().getLogger().logDebug("InboundPollingThread() interrupted.", null, null);
}
catch (Exception e)
{
getService().getLogger().logDebug("Error in InboundPollingThread()", e, null);
}
}
}
class OutboundPollingThread extends Thread
{
@Override
public void run()
{
try
{
while (!SMSServer.this.shutdown)
{
getService().getLogger().logDebug("OutboundPollingThread() run.", null, null);
sendMessages();
if (SMSServer.this.optRunOnce) break;
Thread.sleep(Integer.parseInt(getProperties().getProperty("settings.outbound_interval", "60")) * 1000);
}
}
catch (InterruptedException e)
{
getService().getLogger().logDebug("OutboundPollingThread() interrupted.", null, null);
}
catch (Exception e)
{
getService().getLogger().logDebug("Error in OutboundPollingThread()", e, null);
}
}
}
private void process() throws Exception
{
this.inboundPollingThread = new InboundPollingThread();
this.inboundPollingThread.setName("SMSServer - InboundPollingThread");
this.inboundPollingThread.start();
this.outboundPollingThread = new OutboundPollingThread();
this.outboundPollingThread.setName("SMSServer - OutboundPollingThread");
this.outboundPollingThread.start();
while (!this.shutdown)
Thread.sleep(1000);
}
void startInterfaces() throws Exception
{
for (Interface<? extends Object> inf : getInfList())
inf.start();
}
void stopInterfaces() throws Exception
{
for (Interface<? extends Object> inf : getInfList())
inf.stop();
}
private void run() throws Exception
{
loadConfiguration();
try
{
startInterfaces();
getService().startService();
process();
}
catch (Exception e)
{
stopInterfaces();
getService().stopService();
if (this.inboundPollingThread != null)
{
this.inboundPollingThread.interrupt();
this.inboundPollingThread.join();
}
if (this.outboundPollingThread != null)
{
this.outboundPollingThread.interrupt();
this.outboundPollingThread.join();
}
}
}
void readMessages() throws Exception
{
List<InboundMessage> msgList = new ArrayList<InboundMessage>();
getService().readMessages(msgList, MessageClasses.ALL);
if (msgList.size() > 0)
{
for (Interface<? extends Object> inf : getInfList())
if (inf.isInbound()) inf.MessagesReceived(msgList);
if (getProperties().getProperty("settings.delete_after_processing", "no").equalsIgnoreCase("yes")) for (InboundMessage msg : msgList)
getService().deleteMessage(msg);
}
}
void sendMessages() throws Exception
{
boolean foundOutboundGateway = false;
for (org.smslib.AGateway gtw : getService().getGateways())
if (gtw.isOutbound())
{
foundOutboundGateway = true;
break;
}
if (foundOutboundGateway)
{
List<OutboundMessage> msgList = new ArrayList<OutboundMessage>();
for (Interface<? extends Object> inf : getInfList())
if (inf.isOutbound()) msgList.addAll(inf.getMessagesToSend());
if (getProperties().getProperty("settings.send_mode", "sync").equalsIgnoreCase(("sync")))
{
getService().getLogger().logInfo("SMSServer: sending synchronously...", null, null);
getService().sendMessages(msgList);
for (Interface<? extends Object> inf : getInfList())
if (inf.isOutbound()) inf.markMessages(msgList);
}
else
{
getService().getLogger().logInfo("SMSServer: sending asynchronously...", null, null);
getService().queueMessages(msgList);
}
}
}
class InboundNotification implements org.smslib.IInboundMessageNotification
{
public void process(String gtwId, MessageTypes msgType, InboundMessage msg)
{
List<InboundMessage> msgList = new ArrayList<InboundMessage>();
msgList.add(msg);
for (Interface<? extends Object> inf : getInfList())
if (inf.isInbound())
{
try
{
inf.MessagesReceived(msgList);
}
catch (Exception e)
{
// Shallow this...
}
}
if (getProperties().getProperty("settings.delete_after_processing", "no").equalsIgnoreCase("yes"))
{
try
{
getService().deleteMessage(msg);
}
catch (Exception e)
{
// Shallow this...
}
}
msgList.clear();
}
}
class OutboundNotification implements org.smslib.IOutboundMessageNotification
{
public void process(String gtwId, org.smslib.OutboundMessage msg)
{
try
{
for (Interface<? extends Object> inf : getInfList())
if (inf.isOutbound()) inf.markMessage(msg);
}
catch (Exception e)
{
getService().getLogger().logError("IOutboundMessageNotification error.", e, null);
}
}
}
class CallNotification implements org.smslib.ICallNotification
{
public void process(String gtwId, String callerId)
{
try
{
for (Interface<? extends Object> inf : getInfList())
inf.CallReceived(gtwId, callerId);
}
catch (Exception e)
{
getService().getLogger().logError("ICallNotification error.", e, null);
}
}
}
public boolean checkPriorityTimeFrame(int priority)
{
String timeFrame;
String from, to, current;
Calendar cal = Calendar.getInstance();
if (priority < 0) timeFrame = getProperties().getProperty("settings.timeframe.low", "0000-2359");
else if (priority == 0) timeFrame = getProperties().getProperty("settings.timeframe.normal", "0000-2359");
else if (priority >= 0) timeFrame = getProperties().getProperty("settings.timeframe.high", "0000-2359");
else timeFrame = "0000-2359";
from = timeFrame.substring(0, 4);
to = timeFrame.substring(5, 9);
cal.setTime(new java.util.Date());
current = cal.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + cal.get(Calendar.HOUR_OF_DAY) : "" + cal.get(Calendar.HOUR_OF_DAY);
current += cal.get(Calendar.MINUTE) < 10 ? "0" + cal.get(Calendar.MINUTE) : "" + cal.get(Calendar.MINUTE);
if ((Integer.parseInt(current) >= Integer.parseInt(from)) && (Integer.parseInt(current) < Integer.parseInt(to))) return true;
return false;
}
public static void main(String[] args)
{
System.out.println(Library.getLibraryDescription());
System.out.println("\nSMSLib API version: " + Library.getLibraryVersion());
System.out.println("SMSServer version: " + Library.getLibraryVersion());
SMSServer app = new SMSServer();
for (int i = 0; i < args.length; i++)
{
if (args[i].equalsIgnoreCase("-runonce")) app.optRunOnce = true;
else System.out.println("Invalid argument: " + args[i]);
}
try
{
app.run();
app.srv.getLogger().logInfo("SMSServer exiting normally.", null, null);
}
catch (Exception e)
{
app.srv.getLogger().logError("SMSServer Error: ", e, null);
try
{
app.srv.stopService();
}
catch (Exception e1)
{
// Swallow this...
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -