Commit 16617009 authored by Vít Kabele's avatar Vít Kabele

Session initialization at server side and sending session_id to client.

parent 55f1d031
Pipeline #3136 passed with stages
in 2 minutes and 56 seconds
......@@ -5,6 +5,7 @@
#include "network.h"
#include "msgchannel.h"
#include "dislserver.pb-c.h"
#include "sessions.h"
#include "bytecode.h"
#include "classparser.h"
......@@ -761,6 +762,9 @@ Agent_OnLoad (JavaVM * jvm, char * options, void * reserved) {
rdaprintf ("agent loaded, initializing connections\n");
network_init (agent_config.server_host, agent_config.server_port);
int32_t sid = session_start();
rdaprintf("Obtained session id is %i\n", sid);
// register callbacks
jvmtiEventCallbacks callbacks = {
......
......@@ -12,10 +12,11 @@
#include "dislserver.pb-c.h"
#include "network.h"
int
int32_t
session_start()
{
void * buffer;
/* Create, pack and send the session init request */
ClientMessage message = CLIENT_MESSAGE__INIT;
SessionInitRequest req = SESSION_INIT_REQUEST__INIT;
......@@ -24,7 +25,7 @@ session_start()
message.session_init_request->code = 42;
size_t send_size = client_message__get_packed_size(&message);
void * buffer = malloc(send_size); // FREE!
buffer = malloc(send_size); // FREE!
assert(buffer != NULL);
......@@ -32,8 +33,26 @@ session_start()
debug("Attempting to acquire the session id");
struct connection * conn = network_acquire_connection();
message_send(conn, buffer, send_size);
free(buffer);
free(buffer); // FREEING!
return (0);
/* 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);
assert(response != NULL);
free(buffer); // FREEING!
assert(response->response_case == SERVER_MESSAGE__RESPONSE_SESSION_INIT_RESPONSE);
SessionInitResponse * initResponse = response->session_init_response;
int32_t sid = initResponse->session_id;
session_id = sid;
debug("Obtained session id is:");
server_message__free_unpacked(response, NULL);
network_release_connection(conn);
return sid;
}
......@@ -14,14 +14,14 @@
* Global variable that holds the id of the session.
* Zero means that there is no active session initiated.
*/
int session_id;
int32_t session_id;
/*
* Negotiate the session with the server.
*
* @return Zero on success, nonzero otherwise.
*/
int
int32_t
session_start();
/*
......
......@@ -19,6 +19,7 @@ import java.nio.channels.SocketChannel;
import ch.usi.dag.dislserver.Protocol.ClientMessage;
import ch.usi.dag.dislserver.Protocol.ServerMessage;
import ch.usi.dag.dislserver.Protocol.SessionInitResponse;
/**
......@@ -99,8 +100,15 @@ final class ConnectionHandler implements Runnable {
switch (requestCase){
case SESSION_INIT_REQUEST:
System.out.println ("Delivered code is: ");
System.out.print ( request.getSessionInitRequest ().getCode () ); System.out.println ("");
SessionCredentials credentials = __server.registerSession();
response = ServerMessage
.newBuilder ()
.setSessionInitResponse (
SessionInitResponse
.newBuilder()
.setSessionId(credentials.session_id)
).build ();
sendMessage (response, timer);
break;
case INSTRUMENTATION_DELIVERY:
break;
......
......@@ -9,6 +9,8 @@ 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;
import java.util.concurrent.TimeUnit;
......@@ -50,9 +52,24 @@ public final class DiSLServer {
final CounterSet<ElapsedTime> __globalStats = new CounterSet<ElapsedTime> (
ElapsedTime.class);
//
/**
* Session credentials storage.
*/
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 ();
/**
*
* @param serverSocket
* @param executor
* @param requestProcessor
*/
void run (
final ServerSocketChannel serverSocket,
final ExecutorService executor,
......@@ -106,8 +123,31 @@ public final class DiSLServer {
);
}
//
/**
* Takes care about generating unique session_ids etc.
* Current strategy: Incremental
* @return
*/
SessionCredentials registerSession(){
int curr;
synchronized (session_lock){
curr = ++last_session_id;
}
SessionCredentials credentials = new SessionCredentials (curr);
SessionStorage.put (curr, credentials);
return credentials;
}
/**
* Gets the session by the session id.
* @param session_id required session id.
* @return SessionCredentials on null while not present.
*/
SessionCredentials getSession(int session_id){
return SessionStorage.getOrDefault (session_id, null);
}
public static void main (final String[] args) {
__log.debug ("server starting");
......
/*
* Pack the values related to one session.
*
* File: SessionCredentials.java
* Created by Vit Kabele <vit@kabele.me> on 16/10/2018
*/
package ch.usi.dag.dislserver;
/**
* This file is part of disl
* Created by Vit Kabele <vit@kabele.me> on 16/10/2018
* Pack the values related to one session.
*/
public class SessionCredentials {
/**
* Session identifier
*/
public final int session_id;
/**
* Available states of the session.
*/
public enum SessionState {
/**
* Default
*/
INITIALIZED,
/**
* Instrumentation obtained and OK.
*/
READY,
/**
* Error while processing one of the previous requests.
*/
ERROR,
/**
* Session was closed by the client.
*/
CLOSED
}
/**
* State of the session.
*/
public SessionState State = SessionState.INITIALIZED;
/**
* Construct the class
* @param session_id
*/
SessionCredentials(int session_id){
this.session_id = session_id;
}
}
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