Navigation Menu

Skip to content

Commit

Permalink
Added synchronous mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Luc Paour committed Oct 13, 2002
1 parent 3a6c819 commit cc5dc06
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
6 changes: 3 additions & 3 deletions com/gallery/GalleryRemote/GalleryComm.java
Expand Up @@ -40,23 +40,23 @@ public interface GalleryComm {
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void uploadFiles( StatusUpdate su );
public void uploadFiles( StatusUpdate su, boolean async );

/**
* Causes the GalleryComm instance to fetch the albums contained by
* associated Gallery from the server.
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void fetchAlbums( StatusUpdate su );
public void fetchAlbums( StatusUpdate su, boolean async );

/**
* Causes the GalleryComm instance to fetch the album properties
* for the given Album.
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void albumInfo( StatusUpdate su, Album a );
public void albumInfo( StatusUpdate su, Album a, boolean async );

/**
* Causes the GalleryComm instance to fetch the album properties
Expand Down
21 changes: 14 additions & 7 deletions com/gallery/GalleryRemote/GalleryComm1.java
Expand Up @@ -69,14 +69,21 @@ public GalleryComm1(StatusUpdate su, Gallery g) {
this.g = g;
}

public void uploadFiles( StatusUpdate su ) {
Thread t = new Thread(new UploadTask());
t.start();
void doTask(GalleryTask task, boolean async) {
if (async) {
Thread t = new Thread(task);
t.start();
} else {
task.run();
}
}

public void uploadFiles( StatusUpdate su, boolean async ) {
doTask(new UploadTask(), async);
}

public void fetchAlbums( StatusUpdate su ) {
Thread t = new Thread(new AlbumListTask());
t.start();
public void fetchAlbums( StatusUpdate su, boolean async ) {
doTask(new AlbumListTask(), async);
}

/**
Expand All @@ -85,7 +92,7 @@ public void fetchAlbums( StatusUpdate su ) {
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void albumInfo( StatusUpdate su, Album a ) {
public void albumInfo( StatusUpdate su, Album a, boolean async ) {
/* TEMPORARY */
throw new RuntimeException( "This method is not implemented yet" );
}
Expand Down
57 changes: 41 additions & 16 deletions com/gallery/GalleryRemote/GalleryComm2.java
Expand Up @@ -71,17 +71,26 @@ public class GalleryComm2 implements GalleryComm, GalleryComm2Consts {

/**
* The process ID for StatusUpdate. XXX: What's -1? -- tim
* -1 is an initialization value. means no id.
*/
int pId = -1;

/**
* The minor revision of the server (2.x)
* Use this to decide whether some functionality
* should be disabled (because the server would not understand.
*/
protected int serverMinorVersion = 0;


/* -------------------------------------------------------------------------
* STATIC INITIALIZATON
*/

static {
/* Configures HTTPClient to accept all cookies */
/* XXX: Why should this be static? -- tim */
/* Configures HTTPClient to accept all cookies
* this should be done at least once per GalleryRemote
* invokation */
CookieModule.setCookiePolicyHandler(new CookiePolicyHandler() {
public boolean acceptCookie(Cookie cookie, RoRequest req, RoResponse resp) {
return true;
Expand Down Expand Up @@ -119,9 +128,8 @@ public GalleryComm2( Gallery g ) {
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void uploadFiles( StatusUpdate su ) {
Thread t = new Thread( new UploadTask( su ) );
t.start();
public void uploadFiles( StatusUpdate su, boolean async ) {
doTask( new UploadTask( su ), async );
}

/**
Expand All @@ -130,9 +138,8 @@ public void uploadFiles( StatusUpdate su ) {
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void fetchAlbums( StatusUpdate su ) {
Thread t = new Thread( new AlbumListTask( su ) );
t.start();
public void fetchAlbums( StatusUpdate su, boolean async ) {
doTask( new AlbumListTask( su ), async );
}

/**
Expand All @@ -141,9 +148,8 @@ public void fetchAlbums( StatusUpdate su ) {
*
* @param su an instance that implements the StatusUpdate interface.
*/
public void albumInfo( StatusUpdate su, Album a ) {
Thread t = new Thread( new AlbumPropertiesTask( su, a ) );
t.start();
public void albumInfo( StatusUpdate su, Album a, boolean async ) {
doTask( new AlbumPropertiesTask( su, a ), async );
}

/**
Expand All @@ -160,6 +166,15 @@ public void logOut() {
/* -------------------------------------------------------------------------
* UTILITY METHODS
*/
void doTask( GalleryTask task, boolean async ) {
if ( async ) {
Thread t = new Thread( task );
t.start();
} else {
task.run();
}
}

void status( StatusUpdate su, String message) {
Log.log(Log.INFO, MODULE, message);
if (pId != -1) {
Expand Down Expand Up @@ -301,6 +316,16 @@ private boolean login() {
Properties p = requestResponse( form_data );
if ( p.getProperty( "status" ).equals( GR_STAT_SUCCESS ) ) {
status(su, "Logged in");
try {
String serverVersion = p.getProperty( "server_version" );
int i = serverVersion.indexOf( "." );
serverMinorVersion = Integer.parseInt( serverVersion.substring( i+1 ) );

Log.log(Log.TRACE, MODULE, "Server minor version: " + serverMinorVersion);
} catch (Exception e) {
Log.log( Log.ERROR, MODULE, "Malformed server_version: " + p.getProperty( "server_version" ) );
Log.logException(Log.ERROR, MODULE, e);
}
return true;
} else {
error(su, "Login Error: " + p.getProperty( "status_text" ));
Expand Down Expand Up @@ -563,19 +588,19 @@ public static void main( String [] args ) {
StatusUpdate su = new StatusUpdateAdapter(){};
Gallery g = new Gallery( new URL( "http://www.deathcult.com/gallery/" ), "ted", "1qwe2asd", /*TEMPORARY*/ su );
GalleryComm2 gc = new GalleryComm2( g );
gc.fetchAlbums( su );
gc.fetchAlbums( su, false );

try { Thread.sleep( 10000 ); } catch ( InterruptedException ie ) {}
//try { Thread.sleep( 10000 ); } catch ( InterruptedException ie ) {}

ArrayList albumList = g.getAlbumList();
System.err.println( "albumList size = " + albumList.size() );
for ( int i = 0; i < albumList.size(); i++ ) {
Album a = (Album)albumList.get( i );
a.getAlbumProperties( su );
try { Thread.sleep( 500 ); } catch ( InterruptedException ie ) {}
a.fetchAlbumProperties( su );
//try { Thread.sleep( 500 ); } catch ( InterruptedException ie ) {}
}

try { Thread.sleep( 10000 ); } catch ( InterruptedException ie ) {}
//try { Thread.sleep( 10000 ); } catch ( InterruptedException ie ) {}

ArrayList albumList2 = g.getAlbumList();
System.err.println( "albumList2 size = " + albumList2.size() );
Expand Down

0 comments on commit cc5dc06

Please sign in to comment.