public SocketManager(Socket socket) { this.socket = socket; users = new ArrayList<User>(); tUser = new User(); // Spring Framework IoC ClassPathXmlApplicationContext beanfactory = null; try { beanfactory = new ClassPathXmlApplicationContext(SPRING_CONFIG_DEFAULT); userSvc = (IUserSvc) beanfactory.getBean("userSvc"); } catch (Exception e) { dLog.error("Unable to configure Spring beans", e); } finally { if (beanfactory != null) { beanfactory.close(); } } dLog.trace("In constructor"); }
@Override public void run() { SOCKET_PORT = String.valueOf(socket.getRemoteSocketAddress()); dLog.trace("Starting to run server"); // get all users try { users = userSvc.getAllUsers(); } catch (Exception e2) { dLog.error("Unable to get all users", e2); } dLog.trace("Got " + users.size() + " users"); try { while (!exit) { out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream()); dLog.trace("Successfully got input/output streams"); String inputStr = ""; out.writeObject("Burrito POS Server Connected. Enter Username: "******"OUTPUT | Burrito POS Server Connected. Enter Username: "******"OUTPUT | Burrito POS Server Connected. Enter Username: "******"INPUT | " + inputStr); parent.updateStatus(appendInfo("INPUT | " + inputStr)); while (!inputStr.equals("exit") && !this.exit) { dLog.trace("Exit? " + exit); if (tUser.getUserName() == null) { tUser.setUserName(inputStr); out.writeObject("OK User " + inputStr + ", enter password: "******"OUTPUT | OK User " + inputStr + ", enter password: "******"OUTPUT | Burrito POS Server Connected. Enter Username: "******"Username: "******" | Password: "******"Stored user: "******" | stored pass: "******"OK User verified. Enter command: "); dLog.trace("OUTPUT | OK User verified. Enter command: "); parent.updateStatus(appendInfo("OUTPUT | OK User verified. Enter command: ")); auth = true; } else { tUser.setUserName(null); tUser.setPassword(null); out.writeObject("ERROR Invalid Credentials. Enter Username: "******"OUTPUT | ERROR Invalid Credentials. Enter Username: "******"OUTPUT | ERROR Invalid Credentials. Enter Username: "******"exit")) { out.writeObject("OK Command " + inputStr + " entered. Enter command: "); dLog.trace("OUTPUT | OK Command " + inputStr + " entered. Enter command: "); parent.updateStatus( appendInfo("OUTPUT | OK Command " + inputStr + " entered. Enter command: ")); } } inputStr = (String) in.readObject(); dLog.trace("INPUT | " + inputStr); parent.updateStatus(appendInfo("INPUT | " + inputStr)); } exit = true; try { dLog.trace("Closing the socket"); auth = false; out.writeObject("Exiting"); dLog.trace("Exiting"); parent.updateStatus(appendInfo("Exiting")); this.close(); } catch (Exception e1) { dLog.error("Exception in closing socket", e1); } } } catch (Exception e) { dLog.error("Exception in SocketManager run", e); } }
/** @author james.bloom */ public class SocketManager implements Runnable { private static Logger dLog = Logger.getLogger(SocketManager.class); private Socket socket = null; private ObjectInputStream in = null; private ObjectOutputStream out = null; private boolean exit = false; private boolean auth = false; private User tUser; private List<User> users; private static String SOCKET_PORT; private StatusUI parent; private IUserSvc userSvc; // Spring configuration private static final String SPRING_CONFIG_DEFAULT = "applicationContext.xml"; public SocketManager(Socket socket) { this.socket = socket; users = new ArrayList<User>(); tUser = new User(); // Spring Framework IoC ClassPathXmlApplicationContext beanfactory = null; try { beanfactory = new ClassPathXmlApplicationContext(SPRING_CONFIG_DEFAULT); userSvc = (IUserSvc) beanfactory.getBean("userSvc"); } catch (Exception e) { dLog.error("Unable to configure Spring beans", e); } finally { if (beanfactory != null) { beanfactory.close(); } } dLog.trace("In constructor"); } public SocketManager(Socket socket, StatusUI parent) { this(socket); this.parent = parent; } public void close() throws IOException { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null && !socket.isClosed()) { socket.close(); } } @Override public void run() { SOCKET_PORT = String.valueOf(socket.getRemoteSocketAddress()); dLog.trace("Starting to run server"); // get all users try { users = userSvc.getAllUsers(); } catch (Exception e2) { dLog.error("Unable to get all users", e2); } dLog.trace("Got " + users.size() + " users"); try { while (!exit) { out = new ObjectOutputStream(socket.getOutputStream()); in = new ObjectInputStream(socket.getInputStream()); dLog.trace("Successfully got input/output streams"); String inputStr = ""; out.writeObject("Burrito POS Server Connected. Enter Username: "******"OUTPUT | Burrito POS Server Connected. Enter Username: "******"OUTPUT | Burrito POS Server Connected. Enter Username: "******"INPUT | " + inputStr); parent.updateStatus(appendInfo("INPUT | " + inputStr)); while (!inputStr.equals("exit") && !this.exit) { dLog.trace("Exit? " + exit); if (tUser.getUserName() == null) { tUser.setUserName(inputStr); out.writeObject("OK User " + inputStr + ", enter password: "******"OUTPUT | OK User " + inputStr + ", enter password: "******"OUTPUT | Burrito POS Server Connected. Enter Username: "******"Username: "******" | Password: "******"Stored user: "******" | stored pass: "******"OK User verified. Enter command: "); dLog.trace("OUTPUT | OK User verified. Enter command: "); parent.updateStatus(appendInfo("OUTPUT | OK User verified. Enter command: ")); auth = true; } else { tUser.setUserName(null); tUser.setPassword(null); out.writeObject("ERROR Invalid Credentials. Enter Username: "******"OUTPUT | ERROR Invalid Credentials. Enter Username: "******"OUTPUT | ERROR Invalid Credentials. Enter Username: "******"exit")) { out.writeObject("OK Command " + inputStr + " entered. Enter command: "); dLog.trace("OUTPUT | OK Command " + inputStr + " entered. Enter command: "); parent.updateStatus( appendInfo("OUTPUT | OK Command " + inputStr + " entered. Enter command: ")); } } inputStr = (String) in.readObject(); dLog.trace("INPUT | " + inputStr); parent.updateStatus(appendInfo("INPUT | " + inputStr)); } exit = true; try { dLog.trace("Closing the socket"); auth = false; out.writeObject("Exiting"); dLog.trace("Exiting"); parent.updateStatus(appendInfo("Exiting")); this.close(); } catch (Exception e1) { dLog.error("Exception in closing socket", e1); } } } catch (Exception e) { dLog.error("Exception in SocketManager run", e); } } public void setExit(boolean exit) { dLog.trace("setExit: " + exit); this.exit = exit; } public boolean getExit() { return this.exit; } /** * Append some additional information to info messages to help better track requests as they come * across the UI * * @param msg * @return */ private String appendInfo(String msg) { return new Date() + " | " + SOCKET_PORT + " | " + msg; } }
public void setExit(boolean exit) { dLog.trace("setExit: " + exit); this.exit = exit; }