先介绍一下基本的模块代表的功能
1.net.floodlightcontroller.core.internal.Controller (主要的controller类,处理启动和网络监听)
2.net.floodlightcontroller.core.module.FloodlightModuleContext (IFloodlightProvider服务的注册表)
3.net.floodlightcontroller.core.module.IFloodlightModule (为加载的Floodlight模块定义了一个接口)
其功能如下:
(1)getServices():这个模块提供了什么样的服务。
(2)getModuleDependencies()获得模块依赖关系的列表。
(3)init():内部初始化(不涉及其他模块)。
(4)startUp();外部初始化。
4.net.floodlightcontroller.core.module.IFloodlightService (任何提供服务的IFloodlightModule包的基本接口)
下面直接上代码
public class FloodlightProvider implements IFloodlightModule {
Controller controller;
//实现IFloodlightModule接口,重写里面的抽象方法。并且声明了Controller类
public FloodlightProvider() {
controller = new Controller();
}//构造器里面完成Controller的初始化
@Override //泛型方法,获得模块实现的服务列表
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Collection<Class<? extends IFloodlightService>> services =
new ArrayList<Class<? extends IFloodlightService>>(1);
services.add(IFloodlightProviderService.class);
return services;
}
@Override //泛型方法,返回服务实现类-实现此服务的对象 的映射关系
public Map<Class<? extends IFloodlightService>,
IFloodlightService> getServiceImpls() {
controller = new Controller();
Map<Class<? extends IFloodlightService>,
IFloodlightService> m =
new HashMap<Class<? extends IFloodlightService>,
IFloodlightService>();
m.put(IFloodlightProviderService.class, controller);
return m;
}
@Override //有的模块是没有依赖关系的,例如:ThreadPool模块
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Collection<Class<? extends IFloodlightService>> dependencies =
new ArrayList<Class<? extends IFloodlightService>>(4);
dependencies.add(IStorageSourceService.class);
dependencies.add(IPktInProcessingTimeService.class);
dependencies.add(IRestApiService.class);
dependencies.add(IDebugCounterService.class);
dependencies.add(IDebugEventService.class);
dependencies.add(IOFSwitchService.class);
dependencies.add(IThreadPoolService.class);
dependencies.add(ISyncService.class);
return dependencies;
}
在FloodlightProvider里面包含了Controller类,控制器是通过FloodlightProvider模块的加载而启动的。 继续按照代码执行跟踪,看下Controller里面是干什么的,直接上代码:
public class Controller implements IFloodlightProviderService, IStorageSourceListener, IInfoProvider {
//一个更新的队列
protected static BlockingQueue<IUpdate> updates;
//类的内部定义了一个接口
public interface IUpdate {
/**
* Calls the appropriate listeners
*/
public void dispatch();
}
}
//向队列添加一个update
@Override
public void addUpdateToQueue(IUpdate update) {
try {
updates.put(update);
} catch (InterruptedException e) {
// This should never happen
log.error("Failure adding update {} to queue.", update);
}
}
//run方法中是从队列中取出IUpdate实现并调用dispatch方法
@Override
public void run() {
moduleLoaderState = ModuleLoaderState.COMPLETE;
if (log.isDebugEnabled()) {
logListeners();
}
while (true) {
try {
IUpdate update = updates.take();
update.dispatch();
} catch (InterruptedException e) {
log.error("Received interrupted exception in updates loop;" +
"terminating process");
log.info("Calling System.exit");
System.exit(1);
} catch (StorageException e) {
log.error("Storage exception in controller " +
"updates loop; terminating process", e);
log.info("Calling System.exit");
System.exit(1);
} catch (Exception e) {
log.error("Exception in controller updates loop", e);
}
}
}
class SwitchUpdate implements IUpdate {
@Override
public void dispatch() {
if (log.isTraceEnabled()) {
log.trace("Dispatching switch update {} {}", swId, switchUpdateType);
}
if (switchListeners != null) {
for (IOFSwitchListener listener : switchListeners) {
switch(switchUpdateType) {
case ADDED:
// don't count here. We have more specific
// counters before the update is created
listener.switchAdded(swId);
break;
case REMOVED:
// don't count here. We have more specific
// counters before the update is created
listener.switchRemoved(swId);
break;
case PORTCHANGED:
counters.switchPortChanged
.increment();
listener.switchPortChanged(swId, port, changeType);
break;
case ACTIVATED:
// don't count here. We have more specific
// counters before the update is created
listener.switchActivated(swId);
break;
case DEACTIVATED:
// Called on master to slave transitions, ROLE_STATUS message.
listener.switchDeactivated(swId);
break;
case OTHERCHANGE:
counters.switchOtherChange
.increment();
listener.switchChanged(swId);
break;
}
}
}
}
}
从上面代码可以看到这个类是用来处理交换机事件的(新增、移除等等),在事件来临的时候,调用listener中对应的方法.
那么我们知道原来IUpdate是Controller类提供给其它类的一个接口,通过调用Controller暴露的addUpdateToQueue方法,将IUpdate实例放入队列中,将它加入Controller的调度当中,所以Controller在这里的作用主要是进行调度.