Skip to content

Commit

Permalink
Basic spin mux is now implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jun 16, 2015
1 parent 0e89738 commit 135f45d
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 14 deletions.
24 changes: 22 additions & 2 deletions src/camio.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include <deps/chaste/parsing/numeric_parser.h>
#include <deps/chaste/parsing/bool_parser.h>
#include <deps/chaste/utils/util.h>
#include <src/multiplexers/muxable.h>

#include <src/multiplexers/muxable.h>
#include <src/multiplexers/spin_mux.h>


camio_t* init_camio()
Expand Down Expand Up @@ -351,9 +352,28 @@ camio_error_t camio_transport_get_global(ch_ccstr scheme, void** global_store)

*global_store = found->global_store;
return CAMIO_ENOERROR;
}


}

camio_error_t camio_mux_new( camio_mux_hint_e hint, camio_mux_t** mux_o)
{
camio_mux_t* result = NULL;
switch(hint){
case CAMIO_MUX_HINT_PERFORMANCE:{
result = NEW_MUX(spin);
if(! *mux_o){
return CAMIO_ENOMEM;
}
break;
}
default:
return CAMIO_EINVALID;//TODO XXX better error code
}

result->vtable.construct(result);
*mux_o = result;
return CAMIO_ENOERROR;
}


14 changes: 9 additions & 5 deletions src/multiplexers/mux.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

#include <src/types/types.h>
#include "muxable.h"

#include <time.h>

/**
* Every CamIO mux must implement this interface. See mux.h and api.h for more details.
*/
typedef struct camio_mux_interface_s{
camio_error_t (*insert)(camio_mux_t* this, camio_muxable_t* muxable, camio_mux_mode_e modes);
camio_error_t (*construct)(camio_mux_t* this);
camio_error_t (*insert)(camio_mux_t* this, camio_muxable_t* muxable);
camio_error_t (*remove)(camio_mux_t* this, camio_muxable_t* muxable);
camio_error_t (*select)(camio_mux_t* this, struct timespec timeout, camio_muxable_t** muxable_o,
camio_mux_mode_e* modes_o);
camio_error_t (*select)(camio_mux_t* this, struct timespec timeout, camio_muxable_t** muxable_o);
void (*destroy)(camio_mux_t* this);
} camio_mux_interface_t;

Expand Down Expand Up @@ -57,7 +57,11 @@ typedef struct camio_mux_s {

#define NEW_MUX_DEFINE(NAME, PRIVATE_TYPE) \
const static camio_mux_interface_t NAME##_mux_interface = {\
.ready = NAME##_ready,\
.construct = NAME##_construct,\
.insert = NAME##_insert,\
.remove = NAME##_remove,\
.select = NAME##_select,\
.destroy = NAME##_destroy,\
};\
\
NEW_MUX_DECLARE(NAME)\
Expand Down
5 changes: 3 additions & 2 deletions src/multiplexers/muxable.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef struct camio_muxable_s {
} parent;

/**
* Let the muxor know what sort of event's you are looking for.
* Let the mux know what sort of event's we are looking for.
*/
camio_mux_mode_e mode;
} camio_muxable_t;
Expand All @@ -73,7 +73,7 @@ typedef struct camio_muxable_s {
#define NEW_MUXABLE_DECLARE(NAME)\
camio_muxable_t* new_##NAME##_muxable()

#define NEW_MUXABLE_DEFINE(NAME, PRIVATE_TYPE) \
#define NEW_MUXABLE_DEFINE(NAME, PRIVATE_TYPE, MODE) \
const static camio_muxable_interface_t NAME##_muxable_interface = {\
.ready = NAME##_ready,\
};\
Expand All @@ -83,6 +83,7 @@ typedef struct camio_muxable_s {
camio_muxable_t* result = (camio_muxable_t*)calloc(1,sizeof(camio_muxable_t) + sizeof(PRIVATE_TYPE));\
if(!result) return NULL;\
result->vtable = NAME##_muxable_interface;\
result->mode = MODE;\
return result;\
}

Expand Down
89 changes: 89 additions & 0 deletions src/multiplexers/spin_mux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* CamIO - The Cambridge Input/Output API
* Copyright (c) 2015, All rights reserved.
* See LICENSE.txt for full details.
*
* Created: Jun 16, 2015
* File name: spin_mux.c
* Description:
* A very simple spinning multiplexer. This will work ok so long as you don't mind burning a CPU and the rate at which new
* transports come and go is low.
*/

#include "mux.h"
#include <src/types/muxable_vec.h>

typedef struct {
CH_VECTOR(CAMIO_MUXABLE_VEC)* muxables;
ch_word idx;
} mux_spin_priv_t;

camio_error_t spin_construct(camio_mux_t* this){
mux_spin_priv_t* priv = MUX_GET_PRIVATE(this);
priv->muxables = CH_VECTOR_NEW(CAMIO_MUXABLE_VEC,1024,CH_VECTOR_CMP(CAMIO_MUXABLE_VEC));
if(!priv->muxables){
return CAMIO_ENOMEM;
}

priv->idx = 0;
return CAMIO_ENOERROR;
}


camio_error_t spin_insert(camio_mux_t* this, camio_muxable_t* muxable)
{
mux_spin_priv_t* priv = MUX_GET_PRIVATE(this);
CH_VECTOR(CAMIO_MUXABLE_VEC)* muxables = priv->muxables;
muxables->push_back(muxables, *muxable);
return CAMIO_ENOERROR;
}


camio_error_t spin_remove(camio_mux_t* this, camio_muxable_t* muxable)
{
mux_spin_priv_t* priv = MUX_GET_PRIVATE(this);
CH_VECTOR(CAMIO_MUXABLE_VEC)* muxables = priv->muxables;
camio_muxable_t* result = muxables->find(muxables, muxables->first, muxables->end, *muxable);
if(!result){
return CAMIO_EINVALID; //TODO XXX figure out better names for this
}
muxables->remove(muxables, result); //This may be a little expensive depending on the rate.

return CAMIO_NOTIMPLEMENTED;
}


camio_error_t spin_select(camio_mux_t* this, struct timespec timeout, camio_muxable_t** muxable_o)
{
(void)timeout; //Ignore the timeouts for the moment

mux_spin_priv_t* priv = MUX_GET_PRIVATE(this);
CH_VECTOR(CAMIO_MUXABLE_VEC)* muxables = priv->muxables;

while(1){
priv->idx %= muxables->count; //Adjust for overflow
camio_muxable_t* muxable = muxables->off(muxables,priv->idx);
if(muxable->vtable.ready(muxable)){
priv->idx += 1;//Make sure we look at the next transport first
*muxable_o = muxable;
return CAMIO_ENOERROR;
}

priv->idx += 1;
}
return CAMIO_NOTIMPLEMENTED;
}

void spin_destroy(camio_mux_t* this)
{
mux_spin_priv_t* priv = MUX_GET_PRIVATE(this);
if(priv->muxables){
CH_VECTOR(CAMIO_MUXABLE_VEC)* muxables = priv->muxables;
muxables->delete(muxables);
}
free(this);
return;
}


NEW_MUX_DEFINE(spin,mux_spin_priv_t)
13 changes: 13 additions & 0 deletions src/multiplexers/spin_mux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* CamIO - The Cambridge Input/Output API
* Copyright (c) 2015, All rights reserved.
* See LICENSE.txt for full details.
*
* Created: Jun 16, 2015
* File name: spin_mux.h
* Description:
* <INSERT DESCRIPTION HERE>
*/
#include "mux.h"

NEW_MUX_DECLARE(spin);
22 changes: 22 additions & 0 deletions src/types/muxable_vec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* CamIO - The Cambridge Input/Output API
* Copyright (c) 2015, All rights reserved.
* See LICENSE.txt for full details.
*
* Created: Jun 16, 2015
* File name: muxable_vec.c
* Description:
* <INSERT DESCRIPTION HERE>
*/

#include "types.h"
#include <deps/chaste/data_structs/vector/vector_typed_define_template.h>
#include "muxable_vec.h"

#include <src/multiplexers/muxable.h>

define_ch_vector(CAMIO_MUXABLE_VEC,camio_muxable_t)
define_ch_vector_compare(CAMIO_MUXABLE_VEC,camio_muxable_t)
{
return memcmp(lhs,rhs,sizeof(camio_muxable_t));
}
20 changes: 20 additions & 0 deletions src/types/muxable_vec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* CamIO - The Cambridge Input/Output API
* Copyright (c) 2015, All rights reserved.
* See LICENSE.txt for full details.
*
* Created: Jun 16, 2015
* File name: muxable_vec.h
* Description:
* <INSERT DESCRIPTION HERE>
*/
#ifndef MUXABLE_VEC_H_
#define MUXABLE_VEC_H_

#include <src/types/transport_params_vec.h>
#include <deps/chaste/data_structs/vector/vector_typed_declare_template.h>

declare_ch_vector(CAMIO_MUXABLE_VEC,camio_muxable_t)
declare_ch_vector_cmp(CAMIO_MUXABLE_VEC,camio_muxable_t)

#endif /* MUXABLE_VEC_H_ */
8 changes: 3 additions & 5 deletions src/types/transport_state_vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
#ifndef SRC_TYPES_STREAM_STATE_VEC_H_
#define SRC_TYPES_STREAM_STATE_VEC_H_


#include "types.h"
#include <src/transports/connector.h>
#include <src/types/transport_params_vec.h>
#include "types.h"
#include "../../deps/chaste/data_structs/vector/vector_typed_declare_template.h"
#include "../utils/uri_parser/uri_parser.h"
#include <deps/chaste/data_structs/vector/vector_typed_declare_template.h>
#include <src/utils/uri_parser/uri_parser.h>

typedef camio_error_t (*camio_construct_f)(void** params, ch_word params_size, camio_connector_t** connector_o);

Expand All @@ -38,7 +37,6 @@ typedef struct camio_transport_state_s {
} camio_transport_state_t;


//Declare a linked list of key_value items
declare_ch_vector(CAMIO_TRANSPORT_STATE_VEC,camio_transport_state_t)
declare_ch_vector_cmp(CAMIO_TRANSPORT_STATE_VEC,camio_transport_state_t)

Expand Down

0 comments on commit 135f45d

Please sign in to comment.