Changeset - 4f50bc8e5de6
[Not reviewed]
default
0 4 0
Nathan Brink (binki) - 16 years ago 2010-01-03 12:39:20
ohnobinki@ohnopublishing.net
remove struct distrend_action, improve distrend_accept()
4 files changed with 75 insertions and 38 deletions:
0 comments (0 inline, 0 general)
src/server/distrend.c
Show inline comments
 
@@ -92,31 +92,24 @@ char *job_getserialfilename(struct gener
 
/** Dumps all data in RAM to an xml file (such as current jobs, etc) which is parsed by start_data. Remember to invoke this before shutting down! @TODO: Fill this stub*/
 
int xml_dump()
 
{
 
  return 0;
 
}
 
/**
 
   Performs command stored in a client's request. @TODO: Fill stub
 
*/
 
int distrend_do()
 
{
 
  return 0;
 
}
 
/**
 
   Frees the action. @TODO: Fill stub
 
*/
 
void distrend_action_free()
 
{
 

	
 
}
 

	
 
/** 
 
    Fill variables at startup from XML dumps or defaults
 
 */
 
int start_data(struct general_info *general_info, char *datadir)
 
{
 
  struct stat buffer;
 

	
 
  /**
 
     defaults
 
   */
 
  memset(&general_info->head, '\0', sizeof(struct distrenjob));
 
@@ -1271,31 +1264,29 @@ int main(int argc, char *argv[])
 
      fprintf(stderr, "\nTotal render power: %ld\n", general_info.total_render_power);
 
      break;
 
    case 8:
 
      fprintf(stderr,"Goodbye.\n");
 
      return 0;
 
    default:
 
      fprintf(stderr, "Invalid input, please try again.\n");
 
    }
 
  }
 

	
 
  distrend_listen(general_info.config, &clients);
 
  /* This is called the "main loop" */
 
  while(cont)
 
  while(!general_info.config->die)
 
    {
 
      struct distrend_action *action;
 
      int clientsays = 0; /*< temporary example variable, will be replaced when we can handle messages */
 

	
 
      distrend_accept(general_info.config, clients, &action);
 
      cont = distrend_do(action);
 
      distrend_accept(general_info.config, clients);
 

	
 
      /* Make the following code more event-driven */
 
      frame_watchdog(&general_info.head);
 

	
 

	
 
      struct frameset *frame;
 
      struct distrenjob *job;
 

	
 
      /* If the client is idle, must be modified for climbing through linked list of clients (client->clientnum) */
 
      if(clientstatus == CLIENTSTATUS_IDLE)
 
	{
 
	  int returnnum = find_jobframe(&general_info, &job, &frame); // Finds a frame to render
 
@@ -1303,27 +1294,25 @@ int main(int argc, char *argv[])
 
	    {
 
	      fprintf(stderr,"No frames are available to render at this time. Idling...\n");
 
	      sleep(10);
 
	    }
 
	  else
 
	    remotio_send_to_client(frame->num, job->jobnum); // Pseudo-sends data to client
 
	}
 
      /* If the client states that they finished the frame */
 
      	if(clientsays == DISTREN_REQUEST_DONEFRAME){
 
      	  clientstatus = CLIENTSTATUS_IDLE; // Sets the client back to idle
 
      	  finish_frame(&general_info, job, frame->num); // @TODO: Make sure this actually works.
 
      	}
 

	
 
      distrend_action_free(action);
 
    }
 
    } /* while(!general_info.config->die) */
 

	
 
  distrend_unlisten(general_info.config->listens, clients);
 
  distrend_config_free(general_info.config);
 

	
 
  xmlcleanup();
 

	
 
  return 0;
 
}
 

	
 
/**
 
   constructs the filename for a distrenjob's serialized XML
 
   to be stored/retrieved from/in.
src/server/distrend.h
Show inline comments
 
@@ -24,20 +24,17 @@ struct distrend_config;
 
#define _DISTREN_DISTREN_H_
 

	
 
#include <confuse.h>
 

	
 
#include "listen.h"
 

	
 
struct distrend_config
 
{
 
  cfg_t *mycfg;
 
  struct options_common *options;
 
  struct distrend_listen *listens; /*< Null terminated array of structs */
 
  char *datadir;
 
};
 

	
 
struct distrend_action
 
{
 
  struct distrend_client *client;
 
  int die;
 
};
 

	
 
#endif
src/server/listen.c
Show inline comments
 
@@ -33,24 +33,26 @@
 
struct distrend_clientset
 
{
 
  LIST *clients;
 

	
 
  /*
 
    for select()
 
   */
 
  fd_set readfds;
 
  fd_set writefds;
 
  int nfds;
 
};
 

	
 
int distrend_client_new(struct distrend_client **client, int sock, enum distrend_client_state state);
 
int distrend_client_free(struct distrend_client *client);
 

	
 
int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients)
 
{
 
  int tmp;
 

	
 
  struct sockaddr_in6 sockaddr =
 
    {
 
      .sin6_family = AF_INET6,
 
      .sin6_port = 0,
 
      .sin6_flowinfo = 0,
 
      .sin6_addr = IN6ADDR_ANY_INIT,
 
      .sin6_scope_id = 0
 
@@ -71,67 +73,105 @@ int distrend_listen(struct distrend_conf
 

	
 
      return 1;
 
    }
 

	
 
  tmp = listen(config->listens->sock, 1);
 

	
 
  FD_ZERO(&(*clients)->readfds);
 
  FD_ZERO(&(*clients)->writefds);
 

	
 
  return 0;
 
}
 

	
 
int distrend_accept_find_client(fd_set *fdset, struct distrend_client *client)
 
int distrend_handleread(struct distrend_config *config,
 
		    struct distrend_client *client)
 
{
 
  fprintf(stderr, "%s:%d: STUB: I'm supposed to read data from the client\n",
 
	  __FILE__, __LINE__);
 

	
 
  return 0;
 
}
 

	
 
struct distrend_accept_client_proc_data
 
{
 
  if(FD_ISSET(client->sock, fdset))
 
    return FALSE;
 
  fd_set *fdset;
 
  struct distrend_config *config;
 
};
 
int distrend_accept_client_proc(struct distrend_accept_client_proc_data *data,
 
				struct distrend_client *client)
 
{
 
  if(!FD_ISSET(client->sock, data->fdset))
 
    /** continue iteration through the list */
 
    return TRUE;
 

	
 
  fprintf(stderr, "%s:%d: My traversal says that sock %d has data waiting\n",
 
	  __FILE__, __LINE__, client->sock);
 
  distrend_handleread(data->config, client);
 

	
 
  /** continue iteration through the list */
 
  return TRUE;
 
}
 

	
 
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action)
 
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients)
 
{
 
  int tmp;
 
  fd_set readfds;
 
  fd_set writefds;
 

	
 
  struct distrend_client *cli;
 

	
 
  memcpy(&readfds, &clients->readfds, sizeof(fd_set));
 
  memcpy(&writefds, &clients->writefds, sizeof(fd_set));
 

	
 
  tmp = select(clients->nfds, &readfds, &writefds, NULL, (struct timeval *)NULL);
 
  if(tmp == -1)
 
    {
 
      perror("select");
 

	
 
      return 1;
 
    }
 

	
 
  /**
 
     check if our traversal function found a bit that was set
 
     deal with all sockets that have data waiting
 
   */
 
  if(list_traverse(clients->clients, &readfds, &distrend_accept_find_client, LIST_FRNT | LIST_ALTR) == LIST_OK)
 
    {
 
      cli = (struct distrend_client *)list_curr(clients->clients);
 
      fprintf(stderr, "%s:%d: My traversal says that sock %d has data waiting, which is %s\n",
 
	     __FILE__, __LINE__,
 
	     cli->sock, FD_ISSET(cli->sock, &readfds) ? "true" : "false");
 
      fprintf(stderr, "stub\n");
 
      return 1;
 
    }
 
  list_traverse(clients->clients, &readfds, (list_traverse_func_t)&distrend_accept_client_proc, LIST_FRNT | LIST_ALTR);
 

	
 

	
 
  return 0;
 
}
 

	
 
int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients)
 
{
 
  fprintf(stderr, "%s:%d: I am a stub that needn't be implemented 'til later\n", __FILE__, __LINE__);
 

	
 
  return 1;
 
}
 
/**
 
   This is probably just NOT a placeholder for remotio
 
*/
 
void remotio_send_to_client()
 
void remotio_send_to_client(struct distrend_client *client, const char *msg, size_t len)
 
{
 
    fprintf(stderr, "%s:%d: STUB I should queue data for writing to a client\n", __FILE__, __LINE__);
 
}
 

	
 
int distrend_client_new(struct distrend_client **client, int sock, enum distrend_client_state state)
 
{
 
  *client = malloc(sizeof(struct distrend_client));
 
  if(!*client)
 
{
 
  fprintf(stderr, "%s:%d: I am futile! And I'm happy because of it :-D\n", __FILE__, __LINE__);
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 
  (*client)->sock = sock;
 
  (*client)->state = state;
 
  (*client)->inmsgs = list_init();
 
  (*client)->outmsgs = list_init();
 

	
 
  return 0;
 
}
 

	
 
int distrend_client_free(struct distrend_client *client)
 
{
 
  list_free(client->inmsgs, (void *)LIST_DEALLOC);
 
  list_free(client->outmsgs, (void *)LIST_DEALLOC);
 

	
 
  fprintf(stderr, "%s:%d: stub!", __FILE__, __LINE__);
 
  return 1;
 
}
src/server/listen.h
Show inline comments
 
@@ -17,53 +17,64 @@
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
struct distrend_clientset;
 
struct distrend_listen;
 
struct distrend_client;
 

	
 
#ifndef _DISTREN_LISTEN_H
 
#define _DISTREN_LISTEN_H
 

	
 
#include "distrend.h"
 

	
 
#include <list.h>
 

	
 
enum distrend_client_state
 
  {
 
    DISTREND_CLIENT_PREAUTH,
 
    DISTREND_CLIENT_GOODDOGGY,
 
    DISTREND_CLIENT_BADBOY
 
  };
 

	
 
struct distrend_listen
 
{
 
  int port;
 
  int sock;
 
};
 

	
 
struct distrend_client
 
{
 
  int sock;
 
  int state;
 
  enum distrend_client_state state;
 
  LIST *inmsgs;
 
  LIST *outmsgs;
 
};
 

	
 

	
 

	
 
/**
 
   initializes the listens and clientset
 
   @param config the configuration from distrend
 
   @param clients a pointer to a struct distrend_clientset pointer which will be set to memory allocated for the clientset
 
 */
 
int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients);
 

	
 
/**
 
   checks states of the sockets I'm managing. If there are any new connections,
 
   or activity on any sockets, I'll call the appropriate function.
 
   I will block until there is some sort of activity, including
 
   signals. If you want to cleanly shut down, it's best to register
 
   signal handlers somewhere
 
 */
 
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action);
 
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients);
 

	
 
/**
 
   cleans listening socket. Unnecessary for a working server, currently a stub.
 
 */
 
int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients);
 

	
 
/**
 
   This is probably just NOT a placeholder for remotio
 
*/
 
void remotio_send_to_client();
 

	
 
#endif
0 comments (0 inline, 0 general)