@Override public Job jobFinishedWithNext(int jobId, int exitCode) { SqlManager.getInstance() .executeUpdate( SqlManager.getInstance() .getPreparedStatement( "update job set exitCode=" + exitCode + " where id=" + jobId)); if (exitCode == 0) { Job jobO = null; SqlManager sql = SqlManager.getInstance(); Vector<String[]> jobs = sql.runQuery(sql.getPreparedStatement("Select id from job where waiting_for=" + jobId)); for (String[] job : jobs) { jobO = sql.getJob(Integer.parseInt(job[0])); if (jobO.getWrapper() != null) { break; } } if (jobO != null) { CommunicationImpl.getInstance().preventJob(jobO.getId()); } JobEvent evt = new JobEvent(JobEvent.FINISHED, jobId); evt.setChange("Finished"); CommunicationImpl.getInstance().eventOccured(new ETAEvent(ETAEvent.JOB, evt), -1); return jobO; } else { JobEvent evt = new JobEvent(JobEvent.STATUS_CHANGED, jobId); evt.setChange("Failed"); CommunicationImpl.getInstance().eventOccured(new ETAEvent(ETAEvent.JOB, evt), -1); } return null; }
@Override public void jobFinished(int jobId, int exitCode) { SqlManager.getInstance() .executeUpdate( SqlManager.getInstance() .getPreparedStatement( "update job set exitCode=" + exitCode + " where id=" + jobId)); if (exitCode == 0) { JobEvent evt = new JobEvent(JobEvent.FINISHED, jobId); evt.setChange("Finished"); CommunicationImpl.getInstance().eventOccured(new ETAEvent(ETAEvent.JOB, evt), -1); } else { JobEvent evt = new JobEvent(JobEvent.STATUS_CHANGED, jobId); evt.setChange("Failed"); CommunicationImpl.getInstance().eventOccured(new ETAEvent(ETAEvent.JOB, evt), -1); } }
@Override public void jobStarted(int jobId, String machine) { SqlManager.getInstance() .executeUpdate( SqlManager.getInstance() .getPreparedStatement("update job set machine=? where id=" + jobId, machine)); JobEvent evt = new JobEvent(JobEvent.STATUS_CHANGED, jobId); evt.setChange("Running"); CommunicationImpl.getInstance().eventOccured(new ETAEvent(ETAEvent.JOB, evt), -1); }
/** * Called whenever the program ETAStart attempts to make a connection to this instance of ETA. * This should always be under the local IP and the last 30bytes are extracted and checked against * the database and checked to see if this is a valid token. * * <p>This provides a 2 way communication gateway for talking back and forth in order for the file * io calls and other calls to be secured. * * <p>There can only ever be one connection per user so if there is already a connection deny the * current one and close the socket. * * @param client The {@link Socket} that the {@link ServerSocket} created when this connection was * established. * @param info The first 31 bytes that was sent to this socket */ private void etaStartConnected(Socket client, byte[] info) { if (Arrays.equals(info, empty)) { // it looks like this is a new user. get the next line and check the userName in the database. String userName = ""; char letter; try { while ((letter = (char) client.getInputStream().read()) != '\n' && letter > -1) { userName += letter; } // see if this user is in the database yet Vector<String[]> query = sql.runQuery( sql.getPreparedStatement("select id from user where username=?", userName)); int userId; if (query.size() > 0) { // cool this person isn't new. save the user id to the int userId userId = Integer.parseInt(query.get(0)[0]); } else { // drat this person isn't in the database. get their information and add them in sql.addUser( AuthenticationService.getService().getUserFullName(userName), userName, "", 0); userId = SqlManager.getInstance().getUserInfoFromUsername(userName).getId(); } String newToken = CommunicationImpl.getInstance().generateToken(); System.out.println(newToken + " for user " + userName); info = (" " + newToken).getBytes(); sql.executeUpdate( sql.getPreparedStatement("delete from user_connection where user="******"insert into user_connection values(?," + userId + ")", newToken)); client.getOutputStream().write(newToken.getBytes()); client.getOutputStream().flush(); } catch (IOException e) { e.printStackTrace(); try { client.close(); } catch (IOException e1) { e1.printStackTrace(); } return; } } final User user = getUserForToken(info); if (user == null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } return; } if (userServices.containsKey(user.getId())) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } return; } System.out.println("user " + user.getId() + " is connected"); RMIConnection con = new RMIConnection( client, new RemoteETAConnectionServiceImpl(user.getId()), false, new ConnectionListener() { @Override public void connectionLost() { System.out.println("Connection lost :("); userServices.remove(user.getId()); sql.executeUpdate( sql.getPreparedStatement("delete from token where user=" + user.getId())); } }); userServices.put(user.getId(), (RemoteUserService) con.getService(RemoteUserService.class)); }