1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-24 06:03:28 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/util/IDAssigner.java
SquidDev 932f8a44fc
WIP: Http rework (#98)
- Move all HTTP tasks to a unified "MonitoredResource" model. This
   provides a uniform way of tracking object's lifetimes and disposing
   of them when complete.

 - Rewrite HTTP requests to use Netty instead of standard Java. This
   offers several advantages:
    - We have access to more HTTP verbs (mostly PATCH).
    - We can now do http -> https redirects.
    - We no longer need to spawn in a new thread for each HTTP request.
      While we do need to run some tasks off-thread in order to resolve
      IPs, it's generally a much shorter task, and so is less likely to
      inflate the thread pool.

 - Introduce several limits for the http API:
    - There's a limit on how many HTTP requests and websockets may exist
      at the same time. If the limit is reached, additional ones will be
      queued up until pending requests have finished.
    - HTTP requests may upload a maximum of 4Mib and download a maximum
      of 16Mib (configurable).

 - .getResponseCode now returns the status text, as well as the status
   code.
2019-01-11 11:33:05 +00:00

114 lines
3.3 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.util;
import dan200.computercraft.ComputerCraft;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class IDAssigner
{
private IDAssigner()
{
}
public static int getNextIDFromDirectory( File dir )
{
return getNextID( dir, true );
}
public static int getNextIDFromFile( File file )
{
return getNextID( file, false );
}
private static int getNextID( File location, boolean directory )
{
// Determine where to locate ID file
File lastidFile;
if( directory )
{
location.mkdirs();
lastidFile = new File( location, "lastid.txt" );
}
else
{
location.getParentFile().mkdirs();
lastidFile = location;
}
// Try to determine the id
int id = 0;
if( !lastidFile.exists() )
{
// If an ID file doesn't exist, determine it from the file structure
if( directory && location.exists() && location.isDirectory() )
{
String[] contents = location.list();
for( String content : contents )
{
try
{
int number = Integer.parseInt( content );
id = Math.max( number + 1, id );
}
catch( NumberFormatException e )
{
ComputerCraft.log.error( "Unexpected file '" + content + "' in '" + location.getAbsolutePath() + "'", e );
}
}
}
}
else
{
// If an ID file does exist, parse the file to get the ID string
String idString;
try
{
FileInputStream in = new FileInputStream( lastidFile );
InputStreamReader isr;
isr = new InputStreamReader( in, StandardCharsets.UTF_8 );
try( BufferedReader br = new BufferedReader( isr ) )
{
idString = br.readLine();
}
}
catch( IOException e )
{
ComputerCraft.log.error( "Cannot open ID file '" + lastidFile + "'", e );
return 0;
}
try
{
id = Integer.parseInt( idString ) + 1;
}
catch( NumberFormatException e )
{
ComputerCraft.log.error( "Cannot parse ID file '" + lastidFile + "', perhaps it is corrupt?", e );
return 0;
}
}
// Write the lastID file out with the new value
try
{
BufferedWriter out = new BufferedWriter( new FileWriter( lastidFile, false ) );
out.write( Integer.toString( id ) );
out.newLine();
out.close();
}
catch( IOException e )
{
ComputerCraft.log.error( "An error occured while trying to create the computer folder. Please check you have relevant permissions.", e );
}
return id;
}
}