Skip to content

Commit

Permalink
added shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
libm3l committed Dec 1, 2014
1 parent 2a02890 commit 334e3dc
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Examples/Example8/CSHELL
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set i=1
echo $i
while ($i <= 100)
while ($i <= 10)

echo $i
set NAME=`echo HEAT$i`
Expand All @@ -12,3 +12,4 @@ while ($i <= 100)

@ i = $i + 1
end
./Client1_Shutdown.out localhost 31000
26 changes: 22 additions & 4 deletions Source/ACK.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,28 @@ node_t *ChannelList(const lmchar_t *name, lmsize_t Rproc, lmchar_t ATDT_mode, lm
dim[0] = 1;
if( (TmpNode = m3l_Mklist("request_type", "I", 1, dim, &Gnode, "/_sys_comm_", "./", (lmchar_t *)NULL)) == 0)
Error("m3l_Mklist");
if(Rproc == 0)
TmpNode->data.i[0] = 200;
else
TmpNode->data.i[0] = 100;

switch(Rproc){
case 0:
/*
* close Channel
*/
TmpNode->data.i[0] = 200;
break;

case -1:
/*
* shutdown Server
*/
TmpNode->data.i[0] = -200;
break;

default:
/*
* open new connection
*/
TmpNode->data.i[0] = 100;
}
/*
* add subset Channel
*/
Expand Down
2 changes: 1 addition & 1 deletion Source/Add_Data_Thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ lmsize_t Add_Data_Thread(node_t *Gnode, data_thread_str_t *Data_Thread, node_t *
/*
* malloc status_run variable and set it to 0; if 1 thread was joined
*/
if( (Data_Thread->Data_Str[newnum]->status_run = (lmint_t *)malloc(MAX_NAME_LENGTH* sizeof(lmint_t))) == NULL)
if( (Data_Thread->Data_Str[newnum]->status_run = (lmint_t *)malloc(sizeof(lmint_t))) == NULL)
Perror("Add_Data_Thread: Data_Thread->Data_Str->lmint_t malloc");
*Data_Thread->Data_Str[newnum]->status_run = 0;
/*
Expand Down
10 changes: 10 additions & 0 deletions Source/Ident_Sys_Comm_Channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ lmint_t Ident_Sys_Comm_Channel(node_t *RecNode, node_t **DataBuffer, data_thread
*tmpname='\0';

Popts_CR->opt_s = 's';
/*
* this option defines for Check_Request that the request is system request
*/
if( Check_Request( (*DataBuffer), name_of_required_data_set, *SR_mode, Popts_CR) == 1){
/*
* channel exists
Expand All @@ -360,6 +363,13 @@ lmint_t Ident_Sys_Comm_Channel(node_t *RecNode, node_t **DataBuffer, data_thread
}
break;

case -200:
/*
* shutdown server
*/
return -200;
break;

default:
return *reqtype;
break;
Expand Down
41 changes: 30 additions & 11 deletions Source/Server_Body.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){

lmsize_t i;
lmint_t sockfd, newsockfd, cycle,tmpval;
lmint_t sockfd, newsockfd, cycle,tmpval,activser;
struct sockaddr_in cli_addr;
data_thread_str_t *Data_Threads;
lmchar_t name_of_required_data_set[MAX_NAME_LENGTH], SR_mode;
Expand All @@ -76,9 +76,8 @@ lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){

m3l_set_Send_receive_tcpipsocket(&Popts);

int help;

cycle=0;
activser = 1;
/*
* create standard answers and queue
*/
Expand Down Expand Up @@ -128,12 +127,11 @@ lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){
Perror("Open_Bind_Listen");


printf(" Unique ID is %ld\n", Make_ID_Number(sockfd));
// printf(" Unique ID is %ld\n", Make_ID_Number(sockfd));

// help = 1;
// while(help++ < 11){
while(activser == 1){

while(1){
// while(1){
/*
* if already in cycle, you need to lock mutex here
*/
Expand Down Expand Up @@ -463,20 +461,23 @@ lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){
if( pthread_join(*Data_Threads->ThreadID, NULL) != 0)
Error("Server_Body case200: Joining thread failed");
*Data_Threads->ThreadID = 0;
printf(" Case 200 1,4 retval (%d) --- %s %c\n", *Data_Threads->retval, name_of_required_data_set, *Data_Threads->SR_mode);
printf(" Case 200 1,4 retval (%d) --- %s \n", *Data_Threads->retval, name_of_required_data_set);

break;

case 2:
case 3:
/*
* did not close conenction
*/
opts.opt_EOBseq = '\0'; // send EOFbuff sequence only
if( m3l_send_to_tcpipsocket(Answers->RR_NEG, (const char *)NULL, newsockfd, Popts) < 1)
Error("Server_Body: Error during sending data to socket");
if( m3l_Umount(&RecNode) != 1)
Perror("m3l_Umount");
if( close(newsockfd) == -1)
Perror("close");
printf(" Case 200 2,3 retval (%d) --- %s %c\n", *Data_Threads->retval, name_of_required_data_set, *Data_Threads->SR_mode);
printf(" Case 200 2,3 retval (%d) --- %s \n", *Data_Threads->retval, name_of_required_data_set);
Warning("Server_Body: Data_Thread busy");
break;

Expand All @@ -498,6 +499,23 @@ lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){
}
break;

case -200:
/*
* shutdown Server
*/
activser = 0;
/*
* confirm to client
*/
opts.opt_EOBseq = '\0'; // send EOFbuff sequence only
if( m3l_send_to_tcpipsocket(Answers->RR_POS, (const char *)NULL, newsockfd, Popts) < 1)
Error("Server_Body: Error during sending data to socket");
if( m3l_Umount(&RecNode) != 1)
Perror("m3l_Umount");
if( close(newsockfd) == -1)
Perror("close");
break;

case 501:

if( Popts_SB->opt_f == 'f'){
Expand Down Expand Up @@ -543,9 +561,9 @@ lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){
*/
for(i=0; i< Data_Threads->nall_data_threads; i++){

printf(" Joining thread %d %d \n", i, Data_Threads->Data_Str[i]->data_threadPID);
printf(" Joining thread %d %d %s\n", i, Data_Threads->Data_Str[i]->data_threadPID, Data_Threads->Data_Str[i]->name_of_channel);

if( *Data_Threads->Data_Str[i]->status_run == 1){
if( *Data_Threads->Data_Str[i]->status_run == 0){
if( pthread_join(*Data_Threads->Data_Str[i]->data_threadPID, NULL) != 0)
Error("Server_Body: Joining thread failed");
}
Expand All @@ -562,6 +580,7 @@ lmint_t Server_Body(node_t *Gnode, lmint_t portno, opts_t* Popts_SB){
Pthread_mutex_destroy(&Data_Threads->lock);
Pthread_cond_destroy(&Data_Threads->cond);
Sem_destroy(&Data_Threads->sem);
printf(" After joining1 \n");

free(Data_Threads->name_of_data_set);
free(Data_Threads->SR_mode);
Expand Down
4 changes: 2 additions & 2 deletions Source/lsipdx_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ typedef struct lsipdx_answer{
*/

typedef struct data_thread_int_str{
pthread_t *data_threadPID; /* thread ID of all threads in group data_threads */
pthread_t *data_threadPID; /* thread ID of all threads in group data_threads */
lmchar_t *name_of_channel; /* each spawn thread corresponds to required channel (data link)
store the name in this variable */
lmint_t *status_run; /* if 1, thread is active, if 0, thread should be terminated */
lmint_t *status_run; /* if 1, thread is active, if 0, thread should be terminated */
}data_thread_int_str_t;

typedef struct data_thread_args{
Expand Down
106 changes: 104 additions & 2 deletions Source/socket_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ lmint_t add_connection(const lmchar_t *hostname, lmint_t portno, const lmchar_t
return sockfd;
}

lmint_t del_connection(const lmchar_t *hostname, lmint_t portno, const lmchar_t *name_of_newchannel, opts_t *Popts){
lmint_t del_connection(const lmchar_t *hostname, lmint_t portno, const lmchar_t *name_of_channel, opts_t *Popts){

lmint_t sockfd, retval;
node_t *Gnode, *TmpNode;
Expand All @@ -258,8 +258,110 @@ lmint_t del_connection(const lmchar_t *hostname, lmint_t portno, const lmchar_t
if(hostname != NULL){
/*
* create header which will identify name of data set and Sender (S) or Receiver (R)
* set number of R processes to 0 to indicate you want to delete the channel
*/
if( ( Gnode = ChannelList(name_of_newchannel, 0, 'D', 'N')) == NULL)
if( ( Gnode = ChannelList(name_of_channel, 0, '0', '0')) == NULL)
Error("del_connection: NULL Gnode");
again:
if ( (sockfd = m3l_cli_open_socket(hostname, portno, (lmchar_t *)NULL)) < 0)
Error("del_connection: Could not open socket");
/*
* send header identifying name which connection will be used. Upon receiving this info,
* server will send back the answer
*/
if( (TmpNode = m3l_send_receive_tcpipsocket(Gnode, (lmchar_t *)NULL, sockfd, Popts)) == NULL){
Perror("del_connection: m3l_send_receive_tcpipsocket error");

if(m3l_Umount(&Gnode) != 1)
Perror("del_connection: m3l_Umount");
return -1;
}
/*
* get the value of the /RR/val
* this is an answer from the server which stores the return value in
* ret_receipt == 0 all connections to the server specified for
* given data set were taken, retry opening it again after certain time
*/
retval = TmpNode->child->data.i[0];
/*
* if retval == 1 adding new connection was succesfull
* if retval == 0 adding new connection failed
* if retval == 101 requested new connection already exist
*/
if(retval == 0){

if(m3l_Umount(&TmpNode) != 1)
Perror("m3l_Umount");

if( close(sockfd) == -1)
Perror("close");

if(nanosleep(&tim , &tim2) < 0 )
Error("Nano sleep system call failed \n");

if( ++conn_retry_counter > max_conn_attemps){
printf(" Number of connecitons exceeded max_conn_attemps\n");
return -2;
}
else{
goto again;
}
}
else if(retval == 101){
/*
* requested connection already exist
*/
if( close(sockfd) == -1)
Perror("close");
if(m3l_Umount(&Gnode) != 1)
Perror("m3l_Umount");
if(m3l_Umount(&TmpNode) != 1)
Perror("m3l_Umount");
return 101;
}

if(m3l_Umount(&Gnode) != 1)
Perror("m3l_Umount");
if(m3l_Umount(&TmpNode) != 1)
Perror("m3l_Umount");
}
else{
Error("Hostname not given");
return -1;
}

return sockfd;
}









lmint_t stop_server(const lmchar_t *hostname, lmint_t portno, opts_t *Popts){

lmint_t sockfd, retval;
node_t *Gnode, *TmpNode;
struct timespec tim, tim2;

lmsize_t conn_retry_counter;
lmsize_t max_conn_attemps = 100;

tim.tv_sec = 0;
// tim.tv_nsec = 100000000L; /* 0.1 secs */
tim.tv_nsec = 10000000L; /* 0.1 secs */

conn_retry_counter = 0;

if(hostname != NULL){
/*
* create header which will identify name of data set and Sender (S) or Receiver (R)
* set number of R processes to -1 to indicate you want to close the server
*/
if( ( Gnode = ChannelList("none", -1, '0', '0')) == NULL)
Error("del_connection: NULL Gnode");
again:
if ( (sockfd = m3l_cli_open_socket(hostname, portno, (lmchar_t *)NULL)) < 0)
Expand Down
1 change: 1 addition & 0 deletions Source/socket_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
extern lmint_t open_connection_to_server(const lmchar_t *, lmint_t, client_fce_struct_t *, opts_t *);
extern lmint_t add_connection(const lmchar_t *, lmint_t , const lmchar_t *, opts_t *);
extern lmint_t del_connection(const lmchar_t *, lmint_t , const lmchar_t *, opts_t *);
extern lmint_t stop_server(const lmchar_t *, lmint_t , opts_t *);

#endif

0 comments on commit 334e3dc

Please sign in to comment.