Listener.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.furthurnet.furi |
![]() |
![]() |
Furthurnet |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* FURI - A distributed peer-to-peer file sharing system.
* Copyright (C) 2000 William W. Wong
* williamw@jps.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.furthurnet.furi;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.furthurnet.tray.TrayHandler;
public class Listener implements Runnable
{
private int mListeningPort;
private ServerSocket mListeningSocket;
private InetAddress mMyIP;
private volatile boolean mRequestToDie;
private volatile boolean mRunning;
private volatile boolean mShutdownCompleted;
private IHostChanged mHostChangedListener;
// JMA 9/27/01
private Vector handlers; // Vector of NewConnectionHandlers
public Listener() {
try {
mListeningPort = -1;
mListeningSocket = null;
mRequestToDie = false;
mRunning = false;
mShutdownCompleted = false;
mHostChangedListener = null;
handlers = new Vector();
mMyIP = InetAddress.getLocalHost();
ServiceManager.getCfg().mMyIP = mMyIP.getHostAddress();
}
catch (UnknownHostException e) {
// e.printStackTrace();
}
}
public void setHostChangedListener(IHostChanged listener) {
mHostChangedListener = listener;
}
public synchronized void startup() throws IOException {
Thread myThread = null;
if (mRunning)
return;
// init the ShareManager
ServiceManager.getShareManager();
ServiceManager.log("Listener starting...");
// create a listening socket on the port
while (true) {
try {
mListeningPort = ServiceManager.getCfg().mListeningPort;
mListeningSocket = new ServerSocket(mListeningPort);
ServiceManager.getCfg().save();
break;
}
catch (IOException e) {
invalidListeningPort();
}
}
mRequestToDie = false;
mRunning = true;
if (mHostChangedListener != null)
mHostChangedListener.updateMyIP();
myThread = new Thread(this);
myThread.setPriority(Thread.MAX_PRIORITY);
myThread.start();
}
public synchronized void shutdown(boolean waitForCompleted) {
// Already dead or been requested to die.
if (mRequestToDie)
return;
ServiceManager.log("Listener shutting down...");
// Set flag to die.
mShutdownCompleted = false;
mRequestToDie = true;
// Dummy connection to the listener to wake it up to die.
try {
Socket tmpSock = new Socket("127.0.0.1", mListeningPort);
tmpSock.close();
}
catch(IOException e) {
// Don't care.
}
if (waitForCompleted) {
// Wait until the thread is dead.
while (! mShutdownCompleted) {
try {
wait();
}
catch (InterruptedException e) {
break;
}
}
}
}
public boolean getRunning() {
return mRunning;
}
public String getSelfHostname() {
return mMyIP.getHostName();
}
public InetAddress getSelfHostAddress() {
return mMyIP;
}
// JMA 8/29/01
public String getMyIp() {
return ServiceManager.getManager().getMainFrame().getEffectiveIP();
}
public int getListeningPort() {
return mListeningPort;
}
public String getMyServerAddress() {
return mMyIP.getHostAddress() + ":" + mListeningPort;
}
// The listening thread.
public void run() {
FurthurThread.logPid("furi.Listener " + hashCode());
ServiceManager.log("Listener started.");
Socket incoming;
while (true) {
try {
// Waiting for incoming connection.
incoming = mListeningSocket.accept();
}
catch(IOException e) {
ServiceManager.log(e.toString());
break;
}
try {
incoming.setSoTimeout(2 * 60 * 1000);
}
catch (SocketException e) {}
// See if I have been asked to die.
if (mRequestToDie) {
break;
}
try {
boolean foundExisting = false;
for (int i = 0; (i < handlers.size()) && (!foundExisting); i++)
if (((NewConnectionHandler)handlers.elementAt(i)).isAvailable(incoming)) {
foundExisting = true;
}
if (! foundExisting) {
NewConnectionHandler nch =
new NewConnectionHandler(incoming);
nch.start();
handlers.add(nch);
}
}
catch (Exception e) {
ServiceManager.log(e.toString());
}
}
try {
mListeningSocket.close();
}
catch(IOException e) {
ServiceManager.log(e.toString());
}
try {
for (int i = 0; i < handlers.size(); i++)
((NewConnectionHandler)handlers.elementAt(i)).setDone();
}
catch (Exception e) {}
shutdownCompleted();
}
private synchronized void shutdownCompleted() {
mRunning = false;
ServiceManager.log("Listener stops.");
mShutdownCompleted = true;
handlers = null;
mHostChangedListener = null;
notifyAll();
}
public void invalidListeningPort()
{
MainFrame mf = ServiceManager.getManager().getMainFrame();
Object[] possibleValues = { "Modify Settings", "Choose New Port" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Furthur is unable to listen on port " +
ServiceManager.getCfg().mListeningPort +
". Furthur may either already be running\non this machine or this could be the result of improper proxy settings. Would you\nlike to continue with a new listening port number or modify your network (proxy) settings?\n(You can also click Cancel to simply quit)",
"Furthur Port Problem",
JOptionPane.INFORMATION_MESSAGE,
null,
possibleValues,
possibleValues[0]);
if (selectedValue == null) {
if (ServiceManager.getCfg().mMinimizeMethod !=
TrayHandler.minimizeTaskbarOnly)
ServiceManager.getTrayHandler().removeNotify();
System.exit(0);
}
if (selectedValue.equals(possibleValues[1])) {
ServiceManager.getCfg().generateRandomListeningPort();
}
else {
new ActionModifySettings(ServiceManager.getManager().getMainFrame(),
"Configure Furthurnet", null). actionPerformed(null);
mf.configureProxy();
}
return;
}
}
The table below shows all metrics for Listener.java.




