📄 router.java
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.smslib.routing;
import java.util.ArrayList;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.gateway.AbstractGateway;
import org.smslib.gateway.AbstractGateway.Attributes;
/**
* Base message routing class. Service owns instance of Router (or its
* subclass), and uses its memeber functions to designate gateway(s) to send
* particular outgoing message.
*
* It is allowed that router designates more than modem to send message. It is
* responsibility of Router to decide which gateway will send the message.
*
* Custom routing rules are posiible by creating subclass.
*
* @author heretique
*/
public class Router
{
/** List of candidate gateways */
protected ArrayList<AbstractGateway> candidates;
/** List of gateways that are allowed to send message */
protected ArrayList<AbstractGateway> allowed;
protected Service service;
public Router()
{
candidates = new ArrayList<AbstractGateway>();
allowed = new ArrayList<AbstractGateway>();
}
/**
* Perform early-stage routing, pick gateways that meet minimal
* requirements to send message (for example are set to handle
* outbound messages).
*
* @param msg
* Message to be routed
*/
protected void preroute(OutboundMessage msg)
{
for (AbstractGateway gw : service.getGatewayList())
{
// TODO should we skip gateway if it lacks features that are not critical?
if (gw.isOutbound() && gw.conformsTo(Attributes.BIGMESSAGES, msg.isBig()) )
{
candidates.add(gw);
}
}
}
/**
* Heart of routing & loadbalancing mechanism
* @param msg
* @throws Exception
*/
public void route(OutboundMessage msg) throws Exception
{
beginRouting();
preroute(msg);
// perform custom routing
customRouting(msg);
// chain to loadbalancer
service.getLoadBalancer().sendMessage(msg, allowed);
// finish
finishRouting();
}
/**
* Place for custom routing performed by specialized subclass.
*
* A "positive" approach is taken. Method has to copy references
* to gateways from <code>candidates</code> list to <code>allowed</code>.
* So, default behavior is to copy all references.
*
* Another possibility is to take "negative" approach, where
* method should delete unwanted gateways from list. This approach was
* found difficult to use at this time.
*
* @param msg
*/
protected void customRouting(OutboundMessage msg)
{
allowed.addAll(candidates);
}
/**
* Prepare internal data for routing (clean internal data structures).
* Must be called when new message is routed.
*
*/
protected final void beginRouting()
{
candidates.clear();
allowed.clear();
}
protected final void finishRouting()
{
candidates.clear();
allowed.clear();
}
public Service getService()
{
return service;
}
public void setService(Service service)
{
this.service = service;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -