package files.sharing;
import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Iterator;
import common.info.FileSource;
import connection.ClientConnection;
import files.IndexedFile;
import static main.ServerMain.getConnectionManager;
public class Sharer
{
private int clientId;
private HashSet<IndexedFile> sharedFiles;
public Sharer(int clientId)
{
this.clientId = clientId;
sharedFiles = new HashSet<IndexedFile>();
}
public int hashCode()
{
return clientId;
}
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final Sharer other = (Sharer) obj;
if (clientId != other.clientId) return false;
return true;
}
public void registerSharedFile(IndexedFile indexedFile)
{
sharedFiles.add(indexedFile);
}
public void unregisterSharedFile(IndexedFile file)
{
sharedFiles.remove(file);
}
public void removeAllSharedFiles()
{
Iterator<IndexedFile> sharedFilesIterator = sharedFiles.iterator();
while (sharedFilesIterator.hasNext())
{
IndexedFile file = sharedFilesIterator.next();
sharedFilesIterator.remove();
file.removeSharer(this);
}
}
public FileSource asSource()
{
ClientConnection client = getConnectionManager().getClientById(clientId);
if (client == null)
{
return null;
}
return new FileSource(clientId, new InetSocketAddress(client.getAddress(), client
.getPort()));
}
}