Difference between revisions of "Talk:RoboRumble/Development"
Jump to navigation
Jump to search
m (fix code formatting) |
Lestofante (talk | contribs) |
||
Line 254: | Line 254: | ||
} | } | ||
</pre> | </pre> | ||
− | + | thank to [[User:Rednaxela|Rednaxela]] for code formatting | |
--[[User:Lestofante|lestofante]] 21:16, 4 December 2008 (UTC) | --[[User:Lestofante|lestofante]] 21:16, 4 December 2008 (UTC) |
Revision as of 02:00, 5 December 2008
Background Uploader
Test N°1:
import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; public class UploaderMain { String clientVersion = "1"; long maxTry = 10; public static void main(String args[]){ String infoFilePath = "./roborumble/roborumble.txt"; if (args.length != 0) if (args.length == 1) infoFilePath = args[0]; else{ System.out.println("Usage: java UploaderMain [option]\nOption: Path to roborumble settings file"); return; } File test = new File(infoFilePath); if ( !test.exists() ){ } new UploaderMain(infoFilePath); } public UploaderMain(String infoFilePath){ // read info from file ArrayList<String> allInfo = read( infoFilePath ); // extact info String[] info = findInfo(allInfo); if ((info == null) || (info.length != 2)){ logErr("Roborumble settings file invalid"); return; } // set info String uploadServerUrl = info[0]; String battleFilePath = info[1]; // read battle from file ArrayList<String> battles = read( battleFilePath ); if (battles == null){ logErr("Battle files format invalid"); return; } // now delete the battles File battleFileOld = new File(battleFilePath); if (!battleFileOld.delete()) throw new IllegalArgumentException("Delete: deletion failed"); // prepare data to be uploaded (only 1vs1 supported) battles = prepareToUpload1VS1(battles); ArrayList<String> battlesNotSend; long countTry = 10; while ((battles.size() > 0) || (countTry > maxTry)){ battlesNotSend = new ArrayList<String>(); for (String battle : battles){ if ( !sendBattle(battle, uploadServerUrl) ) battlesNotSend.add(battle); } battles = battlesNotSend; } } private String[] findInfo(ArrayList<String> allInfo) { /* * FIND INFO: * at [0]: uploadServerUrl - RESULTSURL= * at [1]: battleFilePath - OUTPUT= */ String[] info = new String[2]; info[0] = null; info[1] = null; for (String ris : allInfo){ if (ris.indexOf("RESULTSURL=") >= 0){ logOut(ris.substring(ris.indexOf("=")+1)); info[0] = ris.substring(ris.indexOf("=")+1); } if (ris.indexOf("OUTPUT=") >= 0){ logOut(ris.substring(ris.indexOf("=")+1)); info[1] = ris.substring(ris.indexOf("=")+1); } } if ((info[0] != null)&&(info[1] != null)) return info; return null; } private ArrayList<String> prepareToUpload1VS1(ArrayList<String> battles) { ArrayList<String> preparedBattles = new ArrayList<String>(); String[] parametres = new String[6]; String preparingBattle; for(int i = 0; i < battles.size(); i++){ //extrapolate battlefield information parametres = battles.get(i).split(","); if ( parametres.length != 6 ){ logErr( "invalid battlefield: "+battles.get(i) ); continue; } // preparing string to upload preparingBattle = new String(); preparingBattle += "version="+clientVersion; preparingBattle += "&game="+parametres[0]; preparingBattle += "&rounds="+parametres[1]; preparingBattle += "&field="+parametres[2]; preparingBattle += "&user="+parametres[3]; preparingBattle += "&time="+parametres[4]; // extrapolate robot 1 i++; parametres = battles.get(i).split(","); // if invalid information if ( parametres.length != 4 ){ logErr( "invalid robot: "+battles.get(i) ); continue; } // preparing string to upload preparingBattle += "&fname="+parametres[0]; preparingBattle += "&fscore="+parametres[1]; preparingBattle += "&fbulletd="+parametres[2]; preparingBattle += "&fsurvival="+parametres[3]; // extrapolate robot 2 i++; parametres = battles.get(i).split(","); // if invalid information if ( parametres.length != 4 ){ logErr( "invalid robot: "+battles.get(i) ); continue; } // preparing string to upload preparingBattle += "&sname="+parametres[0]; preparingBattle += "&sscore="+parametres[1]; preparingBattle += "&sbulletd="+parametres[2]; preparingBattle += "&ssurvival="+parametres[3]; preparedBattles.add(preparingBattle); } return preparedBattles; } private boolean sendBattle(String battle, String uploadServerURL){ // crea l'URL e la connessione HTTP con la servlet try{ URL url = new URL(uploadServerURL); HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod( "POST" ) ; httpURLConnection.connect() ; OutputStreamWriter dataOutputStream = new OutputStreamWriter(httpURLConnection.getOutputStream()); dataOutputStream.write( battle ); dataOutputStream.flush(); dataOutputStream.close(); BufferedReader dataInputStream = new BufferedReader( new InputStreamReader( httpURLConnection.getInputStream() ) ); String response = new String(); while(true){ response = dataInputStream.readLine(); if ( response == null ) break; logOut("risposta: "+response); } dataInputStream.close(); }catch(Exception e){ logErr("error: "+e); } return false; } private void logOut(String string) { log(string, "./log/outUploader"); } private void logErr(String string) { log(string, "./log/errUploader"); } private void log(String string, String fileName) { FileOutputStream file; try { file = new FileOutputStream(fileName); PrintStream output = new PrintStream(file); output.println(string); output.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private ArrayList<String> read( String filePath ) { try{ ArrayList<String> battles = new ArrayList<String>(); FileReader battleFile = new FileReader( filePath ); BufferedReader battleReader = new BufferedReader(battleFile); String battle = null; while(true){ battle = battleReader.readLine(); if (battle==null) break; battles.add(battle); } battleReader.close(); return battles; }catch(Exception e){ System.out.println("Error: "+e); return null; } } }
thank to Rednaxela for code formatting --lestofante 21:16, 4 December 2008 (UTC)