• Tidak ada hasil yang ditemukan

OverCord Classes

B.3 Plugin Manager

public boolean m_bootstrapPeer;

public String m_bootstrapHost;

public int m_bootstrapPort;

public Vector m_bootstrapList;

public DocumentBuilderFactory m_docBuilderFactory;

public DocumentBuilder m_docBuilder;

public Document m_document;

@SuppressWarnings("unchecked") public PluginManager() {

// Variable declarations String domain, pluginClass;

int countFoundOverlays,numPlugins,numOverlays;

boolean foundaddr;

// Initializations

m_mcastLayer = new McastLayer();

m_docBuilderFactory = DocumentBuilderFactory.newInstance();

m_domains = new Vector();

m_usernames = new Vector();

m_bootstrapList = new Vector();

m_discover = false;

m_bootstrapPeer = false;

m_plugin = null;

m_tempPlugin = null;

// Other variables pluginClass = "";

m_strAddress = "";

countFoundOverlays = numPlugins = numOverlays = 0;

foundaddr = false;

m_inetAddr = null;

try{

foundaddr = false;

Enumeration enumface = NetworkInterface.getNetworkInterfaces();

while(enumface.hasMoreElements()) {

NetworkInterface netface = (NetworkInterface)

enumface.nextElement();

if(netface.getName() != "lo") {

Enumeration enumaddr = netface.getInetAddresses();

while (enumaddr.hasMoreElements()){

//enumaddr.nextElement();

m_inetAddr = (InetAddress) enumaddr.nextElement();

m_strAddress = m_inetAddr.getHostAddress();

if(m_strAddress.indexOf("%") == -1 ) { foundaddr = true;

break;

} } }

if(foundaddr) break;

} }

catch(NoSuchElementException un_ex) {

System.out.println("Could not get machine IP address");

System.out.println("failed to start node, exiting");

System.exit(0);

}catch(SocketException sock_ex){

System.out.println("Could not get machine IP address");

System.out.println("Failed to start node, exiting");

System.exit(0);

}

System.out.println("");

System.out.println("### Starting overlay discovery and

bootstrapping module ... ###");

m_mcastLayer.clearList();

m_mcastLayer.discover();

m_discover = m_mcastLayer.getFoundOverlays();

//if we were able to discover overlays we list them if(m_discover) {

System.out.println("--- Listing discovered overlays:

("+m_mcastLayer.getNumOverlays()+")");

m_mcastLayer.listOverlays();

numOverlays = m_mcastLayer.getNumOverlays();

try{

for(countFoundOverlays = 0; countFoundOverlays <

numOverlays; countFoundOverlays++) { try {

Vector foundOverlay = m_mcastLayer.

getOverlayProfile(countFoundOverlays);

domain = (String)foundOverlay.elementAt(0);

pluginClass = (String)foundOverlay.elementAt(1);

m_bootstrapHost = (String)foundOverlay.elementAt(2);

String temp = (String)foundOverlay.elementAt(3);

temp = temp.trim();

m_bootstrapPort = Integer.parseInt(temp,10);

try {

if(verifyPlugin(pluginClass)) {

System.out.println("---Compatible plugin found:

"+pluginClass);

m_domains.add(countFoundOverlays,

new String(domain));

Vector bootlist = new Vector();

bootlist.add(0,new String(pluginClass));

bootlist.add(1,new String(m_bootstrapHost));

bootlist.add(2,new Integer(m_bootstrapPort));

m_bootstrapList.add(countFoundOverlays,bootlist);

//parse the xml file with user account details m_docBuilder = m_docBuilderFactory.

newDocumentBuilder();

m_document = m_docBuilder.parse

(new File("networks.xml"));

m_document.getDocumentElement().normalize();

NodeList listOfOverlays = m_document.

getElementsByTagName("network");

int numOfRecords = listOfOverlays.getLength();

for(int i = 0; i < numOfRecords; i++) {

Node firstOverlayNode = listOfOverlays.item(i);

if(firstOverlayNode.getNodeType()

== Node.ELEMENT_NODE) { Element firstOverlayElement =

(Element)firstOverlayNode;

NodeList overlayList = firstOverlayElement.

getElementsByTagName("overlay");

Element firstOverlayNameElement =

(Element)overlayList.item(0);

NodeList textFOList = firstOverlayNameElement.

getChildNodes();

String strFOList = textFOList.item(0).

getNodeValue().trim();

NodeList usernameList = firstOverlayElement.

getElementsByTagName("username");

Element userNameElement =

(Element)usernameList.item(0);

NodeList textUNList = userNameElement.

getChildNodes();

String strUNList = textUNList.item(0).

getNodeValue().trim();

if(strFOList.compareToIgnoreCase

(new String(domain)) == 0){

m_usernames.add(new String(strUNList));

} } } }

else {

System.out.println("--- Incompatible plugin class");

}

}catch(PluginNotCreatedException pn_ex) { System.out.println(pn_ex.getMessage());

}catch(SAXParseException saxpe) {

System.out.println("--- SaxParse Exception!!");

}catch (SAXException e) {

Exception x = e.getException();

((x == null) ? e : x).printStackTrace();

}catch(ParserConfigurationException pce) {

System.out.println("--- Parser Exception!!");

}catch(IOException ioex) {

System.out.println("--- IOException on File!!");

}

}catch(NumberFormatException num_ex) { }

}

}catch (NoSuchElementException e) {

System.out.println("Invalid option!");

} }

// we get here after discovered domains were enumerated m_enumPlugins = PluginDetector.detect();

if(m_enumPlugins == null) {

System.err.println("Error while detecting plugins");

return;

}

else {

System.out.println("--- You can start an overlay with any of the following :");

pluginClass = "";

for ( ; m_enumPlugins.hasMoreElements() ; ) {

pluginClass = (String)m_enumPlugins.nextElement();

if(isUniquePluginClass(pluginClass)) { try{

m_plugin = createPlugin(pluginClass,m_strAddress,0);

String strPluginDomain = m_plugin.getDomain();

m_domains.add(countFoundOverlays,

new String(strPluginDomain));

System.out.println(pluginClass);

Vector bootlist = new Vector();

bootlist.add(0,new String(pluginClass));

bootlist.add(1,new String(m_strAddress));

bootlist.add(2,new Integer(0));

m_bootstrapList.add(countFoundOverlays,

(Vector)bootlist);

//parse the xml file with user account details m_docBuilder = m_docBuilderFactory.

newDocumentBuilder();

m_document = m_docBuilder.parse(

new File("networks.xml"));

m_document.getDocumentElement().normalize();

NodeList listOfOverlays = m_document.

getElementsByTagName("network");

int numOfRecords = listOfOverlays.getLength();

boolean foundName = false;

for(int i = 0; i < numOfRecords; i++) {

Node firstOverlayNode = listOfOverlays.item(i);

if(firstOverlayNode.getNodeType() ==

Node.ELEMENT_NODE) { Element firstOverlayElement =

(Element)firstOverlayNode;

NodeList overlayList = firstOverlayElement.

getElementsByTagName("overlay");

Element firstOverlayNameElement =

(Element)overlayList.item(0);

NodeList textFOList = firstOverlayNameElement.

getChildNodes();

String strFOList = textFOList.item(0).

getNodeValue().trim();

NodeList usernameList = firstOverlayElement.

getElementsByTagName("username");

Element userNameElement =

(Element)usernameList.item(0);

NodeList textUNList =

userNameElement.getChildNodes();

String strUNList = textUNList.item(0).

getNodeValue().trim();

if(strFOList.compareToIgnoreCase(

m_plugin.getDomain()) == 0){

m_usernames.add(countFoundOverlays,

new String(strUNList));

foundName = true;

} } }

if(foundName == false){

m_usernames.add(countFoundOverlays,

new String("unassigned"));

}

}catch(PluginNotCreatedException e) {

System.out.println("--- Plugin not created);

}catch(ClassCastException e) {

System.out.println("--- Tried to load a non-plugin as a plugin!!");

}catch(SAXParseException saxpe){

System.out.println("---SaxParse Exception!!");

}catch (SAXException e){

Exception x = e.getException();

((x == null) ? e : x).printStackTrace();

}catch(ParserConfigurationException pce){

System.out.println("--- Parser Exception!!");

}catch(IOException ioex){

System.out.println("--- IOException on File!!");

} }

numPlugins++;

} }

System.out.print("");

}

public boolean isUniquePluginClass(String pluginClass) { int counter;

boolean flag = true;

counter = m_bootstrapList.size();

if(counter == 0) return flag;

for (int i = 0; i < counter; i++) {

Vector vector = (Vector) m_bootstrapList.elementAt(i);

String className = (String) vector.elementAt(0);

if(className.compareToIgnoreCase(pluginClass) == 0) { flag = false;

break;

} }

return flag;

}

public void printBootlist() { int counter;

counter = m_bootstrapList.size();

for (int i = 0; i < counter; i++) {

Vector vector = (Vector) m_bootstrapList.elementAt(i);

String className = (String) vector.elementAt(0);

String host = (String) vector.elementAt(1);

Integer port = (Integer) vector.elementAt(2);

}

}

public void printDomains() { int counter;

counter = m_domains.size();

for (int i = 0; i < counter; i++) {

String domain = (String) m_domains.elementAt(i);

System.out.println(domain);

} }

public Plugin getPlugin() { return m_plugin;

}

public Vector getDomains() { return m_domains;

}

public Vector getUsernames() { return m_usernames;

}

public Vector getBootList() { return m_bootstrapList;

}

public int getDHTPort() { return m_plugin.getPort();

}

public void register(String pluginToUse, String host, int port) { try{

m_plugin = createPlugin(pluginToUse,host,port);

m_plugin.joinOverlay();

}

catch(PluginNotCreatedException e) { System.out.println(e.getMessage());

}

catch(ClassCastException e) {

System.out.println("--- Tried to load a non-plugin as a plugin!!");

} }

public void deregister(String key, String value) { m_plugin.remove(key, value);

m_plugin.leaveOverlay();

m_plugin = null;

}

public void createBinding(String key, String value) { m_plugin.put(key,value);

}

public String get(String key) { String strValue = "";

String domain = key.substring(key.indexOf("@")+1);

String user = key.substring(0,key.indexOf("@"));

if(isInDomain(domain)) {

strValue = m_plugin.get(key);

return strValue;

}

else {

m_mcastLayer.clearList();

m_mcastLayer.discover();

boolean discover = m_mcastLayer.getFoundOverlays();

if(discover) {

int overlayIndex = m_mcastLayer.findOverlay(domain);

if(overlayIndex != -1) {

Vector overlayChoice = m_mcastLayer.

getOverlayProfile(overlayIndex);

String pluginClass =

(String)overlayChoice.elementAt(1);

String bootstrapHost =

(String)overlayChoice.elementAt(2);

String temp = (String)overlayChoice.elementAt(3);

temp = temp.trim();

int bootstrapPort = Integer.parseInt(temp,10);

try {

if(verifyPlugin(pluginClass)) {

m_tempPlugin = createPlugin(pluginClass,

bootstrapHost,bootstrapPort);

for(int i = 0; i < 10; i++) {

strValue = m_tempPlugin.getClientRequest(key);

if(strValue != "GET_FAILED") break;

}

System.out.println("Found "+strValue);

m_tempPlugin = null;

}

else {

System.out.println("Incompatible plugin");

}

}catch(PluginNotCreatedException pn_ex) { System.out.println(pn_ex.getMessage());

}

return strValue;

}

else{

System.out.println("Could not find overlay");

strValue = user+"@"+domain+":5060";

return strValue;

} }

else {

System.out.println("Could not find overlay");

strValue = user+"@"+domain+":5060";

return strValue;

} }

}

public boolean isInDomain(String domain){

boolean flag = true;

String pluginDomain = m_plugin.getDomain();

if(pluginDomain.compareTo(domain) == 0){

flag = true;

}

else flag = false;

return flag;

} }