package voxo.client.controllers;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.EnumSet;
import voxo.client.actions.LogoutAction;
import voxo.client.interfaces.GUIEventInterface;
import voxo.client.newviews.components.NoticeView;
import voxo.client.services.NetworkService;
import voxo.client.threads.NetworkReceiver;
import voxo.client.threads.VoiceReceiver;
import voxo.client.threads.VoiceSender;
import voxo.client.views.MainView;
import voxo.common.Verbose;
import voxo.common.entities.Packet;
import voxo.common.entities.User;
import voxo.common.enums.EnumPacket;
import voxo.common.enums.EnumVerbose;
import voxo.common.packets.ChatPacket;
import voxo.common.packets.ContactListPacket;
import voxo.common.packets.VoiceConnexionPacket;
public class ClientController {
private ArrayList<GUIEventInterface> algui;
private User me;
private NetworkReceiver serverReceiver;
private VoiceReceiver voiceReceiver;
private VoiceSender voiceSender;
private Verbose verbose;
private boolean debug;
public ClientController(String[] args) {
// Init the verbose
ArrayList<String> ala = new ArrayList<String>();
for(String s : args)
ala.add(s);
debug = ala.contains("debug"); //$NON-NLS-1$
this.verbose = new Verbose(ala.contains("gui"), !ala.contains("noconsole"), !ala.contains("nolog"));
NetworkService.verbose = this.verbose;
VoiceReceiver.verbose = this.verbose;
VoiceSender.verbose = this.verbose;
// Init vars
algui = new ArrayList<GUIEventInterface>();
// Partir le GUI et l'ajouter a la liste des views a notifier
algui.add(new MainView(this));
// Partir le network listener
serverReceiver = new NetworkReceiver(this);
Thread t = new Thread(serverReceiver);
t.start();
}
public void parsePacket(Packet p) {
if(debug)
verbose.addConsoleMsg(String.format("=Received packet: %s=", p.getP().toString()), EnumSet.of(EnumVerbose.ToConsole));
switch (p.getP()) {
case SERVER_LoginAccepted:
this.me = ((User) p.getO());
verbose.updateUser(me);
handleLoginAccepted();
break;
case SERVER_UpdateContact:
handleContactUpdated(((ContactListPacket) (p.getO())));
break;
case CLIENT_SendChat:
ChatPacket cP = (ChatPacket) p.getO();
handleChatMsgReceived(cP);
break;
case CLIENT_AskVoice:
handleVoiceRequest((VoiceConnexionPacket) p.getO());
break;
case CLIENT_ConfirmVoice:
VoiceConnexionPacket vcp = (VoiceConnexionPacket) p.getO();
if (voiceSender != null)
voiceSender.killSocket();
voiceSender = new VoiceSender(vcp.getUserIp());
Thread t1 = new Thread(voiceSender);
t1.start();
handleVoiceReceived(vcp);
break;
case SERVER_SearchResults:
ArrayList<User> alu = (ArrayList<User>) p.getO();
handleSearchResults(alu);
break;
case SERVER_Error:
String msg = (String) p.getO();
handleServerError(msg);
break;
case SERVER_Notice:
String msgn = (String) p.getO();
new NoticeView(msgn);
break;
case SERVER_HeartBeat:
if(me!=null) {
try {
NetworkService.sendPacket(new Packet(EnumPacket.CLIENT_HeartBeat, null));
} catch (IOException e) {
verbose.addConsoleMsg(e, EnumSet.of(EnumVerbose.ToConsole, EnumVerbose.ToLog));
}
}
break;
case CLIENT_CancelVoice:
VoiceConnexionPacket vcp2 = (VoiceConnexionPacket) p.getO();
if (voiceReceiver != null)
voiceReceiver.killSocket();
if (voiceSender != null)
voiceSender.killSocket();
handleVoiceRefused(vcp2);
break;
case SERVER_Logout:
MainView mView = null;
if (me != null) {
for (GUIEventInterface IGUI : algui) {
if (IGUI.getClass() == MainView.class)
mView = (MainView) IGUI;
}
}
if( mView != null )
mView.dispose();
break;
case SERVER_KickUser:
MainView mView2 = null;
if (me != null) {
for (GUIEventInterface IGUI : algui) {
if (IGUI.getClass() == MainView.class)
mView2 = (MainView) IGUI;
}
}
if( mView2 != null )
mView2.dispose();
break;
default:
// TODO log unknown packet
break;
}
}
/*
* Events
*/
// Register the GUIs
public void addGUIListener(GUIEventInterface gui) {
algui.add(gui);
}
public void removeGUIListener(GUIEventInterface gui) {
algui.remove(gui);
}
// Fire events
public void handleVoiceRefused(VoiceConnexionPacket vcp) {
for (GUIEventInterface gui : algui)
gui.voiceConfirmationDenied(vcp);
}
public void handleLoginAccepted() {
for (GUIEventInterface gui : algui)
gui.loginAccepted(me);
}
public void handleChatMsgReceived(ChatPacket p) {
for (GUIEventInterface gui : algui)
gui.chatMsgReceived(p);
}
public void handleContactUpdated(ContactListPacket contacts) {
for (GUIEventInterface gui : algui)
gui.contactModified(contacts);
}
public void handleVoiceRequest(VoiceConnexionPacket clp) {
for (GUIEventInterface gui : algui)
gui.voiceRequest(clp);
}
public void handleVoiceReceived(VoiceConnexionPacket clp) {
for (GUIEventInterface gui : algui)
gui.voiceReceived(clp);
}
public void handleServerError(String msg) {
for (GUIEventInterface gui : algui)
gui.serverError(msg);
}
public void handleSearchResults(ArrayList<User> alu) {
for (GUIEventInterface gui : algui)
gui.receiveSearchResults(alu);
}
public void handleOpenChatEvent(User u) {
for (GUIEventInterface gui : algui)
gui.openChatEvent(u);
}
/*
* Properties
*/
public User getMe() {
return me;
}
public void setMe(User me) {
this.me = me;
verbose.updateUser(me);
}
public NetworkReceiver getServerReceiver() {
return serverReceiver;
}
public VoiceReceiver getVoiceReceiver() {
return voiceReceiver;
}
public void setVoiceReceiver(VoiceReceiver vr) {
this.voiceReceiver = vr;
}
public VoiceSender getVoiceSender() {
return voiceSender;
}
public void setVoiceSender(VoiceSender vs) {
this.voiceSender = vs;
}
public ArrayList<GUIEventInterface> getAlgui() {
return algui;
}
public void setAlgui(ArrayList<GUIEventInterface> algui) {
this.algui = algui;
}
public Verbose getVerbose() {
return verbose;
}
public void setVerbose(Verbose verbose) {
this.verbose = verbose;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
}