/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.sniffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sphene.goim.netstat.NetStat;
import net.sphene.goim.netstat.NetStat.App;
import net.sphene.goim.rcp.GOIMPlugin;
import net.sphene.goim.rcp.beans.GOIMGameItem;
import net.sphene.goim.rcp.beans.GOIMGameList;
import net.sphene.goim.sniffudpconns.SniffUdpConns;
public class GOIMSniffUdpConns {
//UniqueIDs<String> uniqueIDs;
Map<Integer, Object> apps;
SniffUdpConns sniffUdpConns;
public GOIMSniffUdpConns() {
//uniqueIDs = new UniqueIDs<String>();
//sniffUdpConns = new SniffUdpConns();
try {
sniffUdpConns = new GOIMSniffer();
} catch(UnsatisfiedLinkError e) {
e.printStackTrace();
sniffUdpConns = null;
}
}
public void go() {
if(sniffUdpConns == null || !sniffUdpConns.isInitialized()) return;
sniffUdpConns.startSniffing();
NetStatThread netStatThread = new NetStatThread();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
netStatThread.doRun();
netStatThread.start();
}
public void appsChanged(ArrayList<App> appList) {
Map<Integer, Object> apps = new HashMap<Integer,Object>();
int[][] conns = new int[appList.size()][6];
int i = 0;
for(App app : appList) {
GOIMGameItem proxy = getGame(app);
if(proxy == null) continue;
if(this.apps != null && this.apps.remove(app.port) == null) {
System.out.println("===== NEW BIND: " + app.port + ": " + app.path);
}
apps.put(app.port,proxy);
conns[i][0] = app.a;
conns[i][1] = app.b;
conns[i][2] = app.c;
conns[i][3] = app.d;
conns[i][4] = app.port;
conns[i][5] = app.port;
i++;
}
int[][] realconns = new int[i][6];
for(int j = 0 ; j < i ; j++) {
for(int k = 0 ; k < 6 ; k++)
realconns[j][k] = conns[j][k];
}
sniffUdpConns.setApps(apps,realconns);
if(this.apps != null) {
for(Integer app : this.apps.keySet()) {
GOIMGameItem gameItem = (GOIMGameItem)this.apps.get(app);
System.out.println("Disconnected: " + gameItem.path + " ?? port: " + app.toString());
gameItem.retrieveExtensionProxy().closedUdpPort(gameItem,app.intValue());
}
}
GOIMSniffUdpConns.this.apps = apps;
}
protected GOIMGameItem getGame(App app) {
// for(GameExtensionProxy proxy : GameExtensionPoint.getGameExtensions()) {
// System.out.println(proxy.exename + " vs. " + app.path);
// if(proxy.path.equals(app.path)) return proxy;
// }
for(GOIMGameItem game : GOIMPlugin.getPreferenceObject(GOIMGameList.class)) {
if(game.path.equalsIgnoreCase(app.path)) return game;
}
return null;
}
public class NetStatThread extends Thread {
public void run() {
while(true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
doRun();
}
}
public void doRun() {
//System.out.println("Updating app list");
NetStat netStat = new NetStat();
netStat.getUdpPortsApps();
appsChanged(netStat.appList);
}
}
}