Commit 1013d5c7 authored by Vít Kabele's avatar Vít Kabele

Closing session.

parent 16617009
Pipeline #3153 passed with stages
in 3 minutes and 4 seconds
......@@ -229,28 +229,15 @@ __instrument_class (
message.request_case = CLIENT_MESSAGE__REQUEST_INSTRUMENT_CLASS_REQUEST;
message.instrument_class_request = &request;
size_t send_size = client_message__get_packed_size(&message);
void * send_buffer = malloc (send_size);
assert (send_buffer != NULL);
client_message__pack(&message, send_buffer);
struct connection * conn = network_acquire_connection ();
message_send (conn, send_buffer, send_size);
free(send_buffer);
//
send_client_message(message, conn);
void * recv_buffer;
size_t recv_size = message_recv (conn, &recv_buffer);
network_release_connection (conn);
ServerMessage * resp = receive_server_message(conn);
ServerMessage * resp = server_message__unpack(NULL,recv_size,recv_buffer);
assert (resp != NULL);
free (recv_buffer);
network_release_connection(conn);
assert( resp->response_case == SERVER_MESSAGE__RESPONSE_INSTRUMENT_CLASS_RESPONSE );
InstrumentClassResponse * response = resp->instrument_class_response;
//server_message__free_unpacked(resp, NULL);
//
// Check if error occurred on the server.
......@@ -806,5 +793,6 @@ Agent_OnUnload (JavaVM * jvm) {
//
// Just close all the connections.
//
//session_end();
network_fini ();
}
/*
* This file is part of src_disl_agent
*
* Created by Vít Kabele on 12/10/2018.
* Formatted according to [C Style and Coding Standards for SunOS](https://www.cis.upenn.edu/~lee/06cse480/data/cstyle.ms.pdf)
*
* Created by Vit Kabele on 12/10/2018.
*/
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include "sessions.h"
......@@ -12,47 +15,80 @@
#include "dislserver.pb-c.h"
#include "network.h"
int32_t
session_start()
{
/*
* Send client message to socket.
*/
void
send_client_message(ClientMessage message, struct connection * conn) {
void * buffer;
/* Create, pack and send the session init request */
ClientMessage message = CLIENT_MESSAGE__INIT;
SessionInitRequest req = SESSION_INIT_REQUEST__INIT;
message.request_case = CLIENT_MESSAGE__REQUEST_SESSION_INIT_REQUEST;
message.session_init_request = &req;
message.session_init_request->code = 42;
size_t send_size = client_message__get_packed_size(&message);
buffer = malloc(send_size); // FREE!
assert(buffer != NULL);
client_message__pack(&message, buffer);
debug("Attempting to acquire the session id");
struct connection * conn = network_acquire_connection();
message_send(conn, buffer, send_size);
free(buffer); // FREEING!
}
/*
* Receive server message from opened socket.
*/
ServerMessage *
receive_server_message(struct connection * conn) {
void * buffer;
ServerMessage * response;
/* Receive the response and set the session_id */
size_t recv_size = message_recv(conn, &buffer); // FREE!
ServerMessage * response = server_message__unpack(NULL, recv_size, buffer);
response = server_message__unpack(NULL, recv_size, buffer);
assert(response != NULL);
free(buffer); // FREEING!
return (response);
}
int32_t
session_start() {
struct connection * conn = network_acquire_connection();
/* Create, pack and send the session init request */
ClientMessage message = CLIENT_MESSAGE__INIT;
SessionInitRequest req = SESSION_INIT_REQUEST__INIT;
message.request_case = CLIENT_MESSAGE__REQUEST_SESSION_INIT_REQUEST;
message.session_init_request = &req;
message.session_init_request->code = 42;
send_client_message(message, conn);
/* Receive the response and set the session_id */
ServerMessage * response = receive_server_message(conn);
assert(response->response_case == SERVER_MESSAGE__RESPONSE_SESSION_INIT_RESPONSE);
SessionInitResponse * initResponse = response->session_init_response;
int32_t sid = initResponse->session_id;
int32_t sid = response->session_init_response->session_id;
session_id = sid;
debug("Obtained session id is:");
server_message__free_unpacked(response, NULL);
network_release_connection(conn);
return sid;
return (sid);
}
void
session_end() {
struct connection * conn = network_acquire_connection();
ClientMessage message = CLIENT_MESSAGE__INIT;
CloseConnection cc = CLOSE_CONNECTION__INIT;
cc.reason = CLOSE_CONNECTION__CLOSE_REASON__FINISHED;
message.request_case = CLIENT_MESSAGE__REQUEST_CLOSE_CONNECTION;
message.session_id = session_id;
message.close_connection = &cc;
send_client_message(message, conn);
network_release_connection(conn);
}
......@@ -2,6 +2,8 @@
* Allow usage of the session enabled protocol
* This file is part of src_disl_agent
*
* Formatted according to [C Style and Coding Standards for SunOS](https://www.cis.upenn.edu/~lee/06cse480/data/cstyle.ms.pdf)
*
* Created by Vít Kabele on 12/10/2018.
*/
......@@ -9,6 +11,10 @@
#define _SESSIONS_H_
#include "msgchannel.h"
#include "connection.h"
#include "protobuf-c.h"
#include "dislserver.pb-c.h"
/*
* Global variable that holds the id of the session.
......@@ -16,10 +22,17 @@
*/
int32_t session_id;
void
send_client_message(ClientMessage message, struct connection * conn);
ServerMessage *
receive_server_message(struct connection * conn);
/*
* Negotiate the session with the server.
*
* @return Zero on success, nonzero otherwise.
* @return Obtained session id.
*/
int32_t
session_start();
......
......@@ -93,6 +93,7 @@ final class ConnectionHandler implements Runnable {
ClientMessage request = receiveMessage (timer,stats);
ServerMessage response = ServerMessage.getDefaultInstance ();
SessionCredentials credentials;
if (request == null) break;
......@@ -100,7 +101,7 @@ final class ConnectionHandler implements Runnable {
switch (requestCase){
case SESSION_INIT_REQUEST:
SessionCredentials credentials = __server.registerSession();
credentials = __server.registerSession();
response = ServerMessage
.newBuilder ()
.setSessionInitResponse (
......@@ -138,6 +139,9 @@ final class ConnectionHandler implements Runnable {
}
break;
case CLOSE_CONNECTION:
int session_id = request.getSessionId ();
credentials = __server.getSession (session_id);
credentials.State = SessionCredentials.SessionState.CLOSED;
case REQUEST_NOT_SET:
default:
break REQUEST_LOOP;
......
......@@ -9,7 +9,6 @@ import java.net.StandardSocketOptions;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
......@@ -55,14 +54,12 @@ public final class DiSLServer {
/**
* Session credentials storage.
*/
final ConcurrentHashMap<Integer,SessionCredentials> SessionStorage = new ConcurrentHashMap<> ();
private final ConcurrentHashMap<Integer,SessionCredentials> SessionStorage = new ConcurrentHashMap<> ();
/**
* Holds the last used session id.
*/
private int last_session_id = 0;
private final Object session_lock = new Object ();
private final AtomicInteger last_session_id = new AtomicInteger (0);
/**
*
......@@ -130,10 +127,7 @@ public final class DiSLServer {
* @return
*/
SessionCredentials registerSession(){
int curr;
synchronized (session_lock){
curr = ++last_session_id;
}
int curr = last_session_id.incrementAndGet ();
SessionCredentials credentials = new SessionCredentials (curr);
SessionStorage.put (curr, credentials);
return credentials;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment