/** * Called whenever the program ETAMonitor 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>A connection should be established any time a job gets started on a machine. The idea of * this is to provide a gateway to the job that is running in case the user wants to send data to * job and it tells ETA that the job started on which machine and when it finishes. It also gets * the exit code to check if the job finished properly. * * @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 etaMonitorConnected(Socket client, byte[] info) { System.out.println("monitor connected with info--" + new String(info)); User user = getUserForToken(info); if (user == null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } return; } String jobNumber = ""; char letter; try { System.out.println("reading job for user " + user.getName()); while ((letter = (char) client.getInputStream().read()) != '\n' && letter > -1) { jobNumber += letter; } System.out.println("job #" + jobNumber); } catch (IOException e) { return; } final int job = Integer.parseInt(jobNumber); RMIConnection con = new RMIConnection( client, this, false, new ConnectionListener() { @Override public void connectionLost() { monitorServices.remove(job); } }); monitorServices.put(job, (RemoteMonitorService) con.getService(RemoteMonitorService.class)); }
/** * 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)); }