Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[interface] Convert all job-control interfaces to generic interfaces
Remove job-control as an interface type, and replace job-control
interfaces with generic interfaces supporting the close() method.
(Both done() and kill() are absorbed into the function of close();
kill() is merely close(-ECANCELED).)

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jun 22, 2010
1 parent e71b83b commit a03dd97
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 293 deletions.
69 changes: 30 additions & 39 deletions src/core/downloader.c
Expand Up @@ -41,7 +41,7 @@ struct downloader {
struct refcnt refcnt;

/** Job control interface */
struct job_interface job;
struct interface job;
/** Data transfer interface */
struct xfer_interface xfer;

Expand Down Expand Up @@ -74,13 +74,14 @@ static void downloader_free ( struct refcnt *refcnt ) {
*/
static void downloader_finished ( struct downloader *downloader, int rc ) {

/* Block further incoming messages */
job_nullify ( &downloader->job );
xfer_nullify ( &downloader->xfer );
/* Register image if download was successful */
if ( rc == 0 )
rc = downloader->register_image ( downloader->image );

/* Free resources and close interfaces */
/* Shut down interfaces */
xfer_nullify ( &downloader->xfer );
xfer_close ( &downloader->xfer, rc );
job_done ( &downloader->job, rc );
intf_shutdown ( &downloader->job, rc );
}

/**
Expand Down Expand Up @@ -120,29 +121,14 @@ static int downloader_ensure_size ( struct downloader *downloader,
*
*/

/**
* Handle kill() event received via job control interface
*
* @v job Downloader job control interface
*/
static void downloader_job_kill ( struct job_interface *job ) {
struct downloader *downloader =
container_of ( job, struct downloader, job );

/* Terminate download */
downloader_finished ( downloader, -ECANCELED );
}

/**
* Report progress of download job
*
* @v job Downloader job control interface
* @v downloader Downloader
* @v progress Progress report to fill in
*/
static void downloader_job_progress ( struct job_interface *job,
struct job_progress *progress ) {
struct downloader *downloader =
container_of ( job, struct downloader, job );
static void downloader_progress ( struct downloader *downloader,
struct job_progress *progress ) {

/* This is not entirely accurate, since downloaded data may
* arrive out of order (e.g. with multicast protocols), but
Expand All @@ -152,13 +138,6 @@ static void downloader_job_progress ( struct job_interface *job,
progress->total = downloader->image->len;
}

/** Downloader job control interface operations */
static struct job_interface_operations downloader_job_operations = {
.done = ignore_job_done,
.kill = downloader_job_kill,
.progress = downloader_job_progress,
};

/****************************************************************************
*
* Data transfer interface
Expand Down Expand Up @@ -215,10 +194,6 @@ static void downloader_xfer_close ( struct xfer_interface *xfer, int rc ) {
struct downloader *downloader =
container_of ( xfer, struct downloader, xfer );

/* Register image if download was successful */
if ( rc == 0 )
rc = downloader->register_image ( downloader->image );

/* Terminate download */
downloader_finished ( downloader, rc );
}
Expand All @@ -233,6 +208,22 @@ static struct xfer_interface_operations downloader_xfer_operations = {
.deliver_raw = xfer_deliver_as_iob,
};

/****************************************************************************
*
* Job control interface
*
*/

/** Downloader job control interface operations */
static struct interface_operation downloader_job_op[] = {
INTF_OP ( job_progress, struct downloader *, downloader_progress ),
INTF_OP ( intf_close, struct downloader *, downloader_finished ),
};

/** Downloader job control interface descriptor */
static struct interface_descriptor downloader_job_desc =
INTF_DESC ( struct downloader, job, downloader_job_op );

/****************************************************************************
*
* Instantiator
Expand All @@ -253,7 +244,7 @@ static struct xfer_interface_operations downloader_xfer_operations = {
* the specified image object. If the download is successful, the
* image registration routine @c register_image() will be called.
*/
int create_downloader ( struct job_interface *job, struct image *image,
int create_downloader ( struct interface *job, struct image *image,
int ( * register_image ) ( struct image *image ),
int type, ... ) {
struct downloader *downloader;
Expand All @@ -265,8 +256,8 @@ int create_downloader ( struct job_interface *job, struct image *image,
if ( ! downloader )
return -ENOMEM;
ref_init ( &downloader->refcnt, downloader_free );
job_init ( &downloader->job, &downloader_job_operations,
&downloader->refcnt );
intf_init ( &downloader->job, &downloader_job_desc,
&downloader->refcnt );
xfer_init ( &downloader->xfer, &downloader_xfer_operations,
&downloader->refcnt );
downloader->image = image_get ( image );
Expand All @@ -278,7 +269,7 @@ int create_downloader ( struct job_interface *job, struct image *image,
goto err;

/* Attach parent interface, mortalise self, and return */
job_plug_plug ( &downloader->job, job );
intf_plug_plug ( &downloader->job, job );
ref_put ( &downloader->refcnt );
va_end ( args );
return 0;
Expand Down
85 changes: 21 additions & 64 deletions src/core/job.c
Expand Up @@ -28,70 +28,27 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/

void job_done ( struct job_interface *job, int rc ) {
struct job_interface *dest = job_get_dest ( job );

job_unplug ( job );
dest->op->done ( dest, rc );
job_put ( dest );
}

void job_kill ( struct job_interface *job ) {
struct job_interface *dest = job_get_dest ( job );

job_unplug ( job );
dest->op->kill ( dest );
job_put ( dest );
}

void job_progress ( struct job_interface *job,
struct job_progress *progress ) {
struct job_interface *dest = job_get_dest ( job );

dest->op->progress ( dest, progress );
job_put ( dest );
}

/****************************************************************************
*
* Helper methods
*
* These functions are designed to be used as methods in the
* job_interface_operations table.
*
*/

void ignore_job_done ( struct job_interface *job __unused, int rc __unused ) {
/* Nothing to do */
}

void ignore_job_kill ( struct job_interface *job __unused ) {
/* Nothing to do */
}

void ignore_job_progress ( struct job_interface *job __unused,
struct job_progress *progress ) {
memset ( progress, 0, sizeof ( *progress ) );
}

/** Null job control interface operations */
struct job_interface_operations null_job_ops = {
.done = ignore_job_done,
.kill = ignore_job_kill,
.progress = ignore_job_progress,
};

/**
* Null job control interface
* Get job progress
*
* This is the interface to which job control interfaces are connected
* when unplugged. It will never generate messages, and will silently
* absorb all received messages.
* @v intf Object interface
* @v progress Progress data to fill in
*/
struct job_interface null_job = {
.intf = {
.dest = &null_job.intf,
.refcnt = NULL,
},
.op = &null_job_ops,
};
void job_progress ( struct interface *intf, struct job_progress *progress ) {
struct interface *dest;
job_progress_TYPE ( void * ) *op =
intf_get_dest_op ( intf, job_progress, &dest );
void *object = intf_object ( dest );

DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " job_progress\n",
INTF_INTF_DBG ( intf, dest ) );

if ( op ) {
op ( object, progress );
} else {
/* Default is to mark progress as zero */
memset ( progress, 0, sizeof ( *progress ) );
}

intf_put ( dest );
}
29 changes: 10 additions & 19 deletions src/core/monojob.c
Expand Up @@ -36,25 +36,19 @@ FILE_LICENCE ( GPL2_OR_LATER );

static int monojob_rc;

static void monojob_done ( struct job_interface *job __unused, int rc ) {
static void monojob_close ( struct interface *intf, int rc ) {
monojob_rc = rc;
intf_restart ( intf, rc );
}

/** Single foreground job operations */
static struct job_interface_operations monojob_operations = {
.done = monojob_done,
.kill = ignore_job_kill,
.progress = ignore_job_progress,
static struct interface_operation monojob_intf_op[] = {
INTF_OP ( intf_close, struct interface *, monojob_close ),
};

/** Single foreground job */
struct job_interface monojob = {
.intf = {
.dest = &null_job.intf,
.refcnt = NULL,
},
.op = &monojob_operations,
};
static struct interface_descriptor monojob_intf_desc =
INTF_DESC_PURE ( monojob_intf_op );

struct interface monojob = INTF_INIT ( monojob_intf_desc );

/**
* Wait for single foreground job to complete
Expand All @@ -77,9 +71,8 @@ int monojob_wait ( const char *string ) {
key = getchar();
switch ( key ) {
case CTRL_C:
job_kill ( &monojob );
rc = -ECANCELED;
goto done;
monojob_close ( &monojob, -ECANCELED );
break;
default:
break;
}
Expand All @@ -92,8 +85,6 @@ int monojob_wait ( const char *string ) {
}
rc = monojob_rc;

done:
job_done ( &monojob, rc );
if ( rc ) {
printf ( " %s\n", strerror ( rc ) );
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/include/ipxe/dhcp.h
Expand Up @@ -18,7 +18,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/netdevice.h>
#include <ipxe/uaccess.h>

struct job_interface;
struct interface;
struct dhcp_options;
struct dhcp_packet;

Expand Down Expand Up @@ -622,8 +622,8 @@ extern int dhcp_create_request ( struct dhcp_packet *dhcppkt,
struct net_device *netdev,
unsigned int msgtype, struct in_addr ciaddr,
void *data, size_t max_len );
extern int start_dhcp ( struct job_interface *job, struct net_device *netdev );
extern int start_pxebs ( struct job_interface *job, struct net_device *netdev,
extern int start_dhcp ( struct interface *job, struct net_device *netdev );
extern int start_pxebs ( struct interface *job, struct net_device *netdev,
unsigned int pxe_type );

/* In environments that can provide cached DHCP packets, this function
Expand Down
4 changes: 2 additions & 2 deletions src/include/ipxe/downloader.h
Expand Up @@ -9,10 +9,10 @@

FILE_LICENCE ( GPL2_OR_LATER );

struct job_interface;
struct interface;
struct image;

extern int create_downloader ( struct job_interface *job, struct image *image,
extern int create_downloader ( struct interface *job, struct image *image,
int ( * register_image ) ( struct image *image ),
int type, ... );

Expand Down

0 comments on commit a03dd97

Please sign in to comment.