/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package logic;
import enums.ServiceTags;
import framework.IATMServer;
import framework.IListenThread;
import framework.IServiceObject;
import framework.IUserInfo;
import java.io.IOException;
import java.util.List;
import messages.PlaneDataObject;
import messages.ServiceObject;
import model.ATMSModel;
/**
*
* @author swift
*/
public class ATMServer implements IATMServer {
private int portNr;
private IListenThread lt;
private CaptureThread ct;
private ATMSModel model;
public ATMServer(int port) {
this.portNr = port;
System.out.println("####################################################");
System.out.println("Server version beta 1.5 starting ond port port " + portNr);
System.out.println("####################################################");
model = new ATMSModel(this);
lt = new ListenThread(portNr, model);
ct = new CaptureThread(this);
}
public void broadcast(Object o, IUserInfo senderUi) {
List<IUserInfo> userList = model.getUserList();
for (IUserInfo ui : userList) {
if (!ui.equals(senderUi)) {
sendMessage(o, ui);
}
}
}
public void broadcast(Object o) {
List<IUserInfo> userList = model.getUserList();
for (IUserInfo ui : userList) {
sendMessage(o, ui);
}
if (o instanceof PlaneDataObject) {
model.setActivity(((PlaneDataObject) o).getIP());
}
}
private void sendMessage(Object o, IUserInfo ui) {
try {
ui.getOOS().writeObject(o);
} catch (IOException ex) {
System.out.println("User " + ui.getNickName() + " seems dead, removing...");
model.removeUserInfo(ui);
//Logger.getLogger(ATMServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void keepAlive(IServiceObject so, IUserInfo ui) {
sendMessage(so, ui);
}
private String[] getUserArray() {
Object[] tempObj = model.getUserNames();
String[] tempStr = new String[tempObj.length];
for (int i = 0; i < tempStr.length; i++) {
tempStr[i] = (String) tempObj[i];
}
return tempStr;
}
public void sendUserList(IUserInfo to) {
ServiceObject sout = new ServiceObject("Server", ServiceTags.UserList);
sout.setUserList(getUserArray());
sendMessage(sout, to);
}
public void broadcasUserList(IUserInfo changer) {
ServiceObject sout = new ServiceObject("Server", ServiceTags.UserList);
sout.setUserList(getUserArray());
broadcast(sout, changer);
}
public static void main(String[] args) {
int port = 4242;
if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Specified portnumber must be an integer");
System.exit(1);
}
}
new ATMServer(port);
}
}