Skip to content

Commit

Permalink
BYTE CODE SENT
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadsadek999 committed May 17, 2024
1 parent db6cd28 commit 898175d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 75 deletions.
2 changes: 0 additions & 2 deletions compose.override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ services:

service_webserver:
image: workup:webserver
controller:
image: workup:controller
68 changes: 25 additions & 43 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import asg.cliche.CLIException;
import asg.cliche.Command;
import com.workup.shared.commands.controller.*;
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
import com.workup.shared.commands.jobs.requests.CreateJobRequest;
import com.workup.shared.enums.ControllerQueueNames;
import com.workup.shared.enums.ServiceQueueNames;
import java.util.Base64;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.*;
import org.apache.logging.log4j.Level;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -100,9 +98,8 @@ public String setLoggingLevel(String app, String level) {

@Command(description = "test")
public void test() {
CreateWalletRequest request =
CreateWalletRequest.builder().withFreelancerId("123").withUserId("123").build();
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.PAYMENTS, request);
CreateJobRequest request = CreateJobRequest.builder().withTitle("Ziko").build();
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.JOBS, request);
}

@Command(description = "Creates a new command")
Expand All @@ -116,47 +113,32 @@ public String updateCommand(String app, String commandName, String className) th
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}
updateCommand(commandName, className);
return "Command sent!!";
}

public void updateCommand(String commandName, String className)
throws InstantiationException, IllegalAccessException {
ClassPool pool = ClassPool.getDefault();
// this line defines the root class used to query classes. so when
// we access a command to edit it, we write its name relative to the Main class,
// that's why the format is commands.CommandName
pool.insertClassPath(new ClassClassPath(ControllerApplication.class));
try {
CtClass ctClass = pool.get(className);
// That's the compiled class byte code
byte[] classBytes = ctClass.toBytecode();
// I encoded the byte code to a string to be able
// to send it in a json and send it over the network,
// not sure if it could be done better.
String byteString = java.util.Base64.getEncoder().encodeToString(classBytes);
// decoding the string back to byte array
byte[] byteArray = Base64.getDecoder().decode(byteString);
MyClassLoader loader = new MyClassLoader();
// This method loads a class on the fly given its byte code.
// Usually it is called when an object is created as it reads
// the byte code from the memory to load the class,
// create an instance of it then run it.
// But here we give it the byte code directly instead of reading it from memory.
Class<com.workup.shared.commands.Command> newCommand =
(Class<com.workup.shared.commands.Command>) loader.loadClass(byteArray, className);
com.workup.shared.commands.Command command = newCommand.newInstance();
command.Run(null);
rabbitTemplate.convertAndSend(command);
byte[] byteArray = getByteCode(commandName, className);
rabbitTemplate.convertAndSend(
appQueueMap.get(app),
"",
UpdateCommandRequest.builder()
.withCommandName(commandName)
.withByteCode(byteArray)
.build());
} catch (Exception ex) {
ex.printStackTrace();
}
return "Command sent!!";
}

static class MyClassLoader extends ClassLoader {
public Class<?> loadClass(byte[] byteCode, String className) {
return defineClass(className, byteCode, 0, byteCode.length);
}
private byte[] getByteCode(String commandName, String className)
throws InstantiationException,
IllegalAccessException,
NotFoundException,
IOException,
CannotCompileException {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(ControllerApplication.class));
CtClass ctClass = pool.get(className);
// That's the compiled class byte code
return ctClass.toBytecode();
}

@Command(description = "Deletes an existing command")
Expand Down
4 changes: 2 additions & 2 deletions controller/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.application.name=controller
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=${JOBS_MQ_USER}
spring.rabbitmq.password=${JOBS_MQ_PASSWORD}
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
8 changes: 4 additions & 4 deletions services/jobs/cassandra-config/cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ seed_provider:
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "10.0.5.18"
- seeds: "10.0.4.12"

# For workloads with more data than can fit in memory, Cassandra's
# bottleneck will be reads that need to fetch data from
Expand Down Expand Up @@ -663,7 +663,7 @@ ssl_storage_port: 7001
#
# Setting listen_address to 0.0.0.0 is always wrong.
#
listen_address: 10.0.5.18
listen_address: 10.0.4.12

# Set listen_address OR listen_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
Expand All @@ -677,7 +677,7 @@ listen_address: 10.0.5.18

# Address to broadcast to other Cassandra nodes
# Leaving this blank will set it to the same value as listen_address
broadcast_address: 10.0.5.18
broadcast_address: 10.0.4.12

# When using multiple physical network interfaces, set this
# to true to listen on broadcast_address in addition to
Expand Down Expand Up @@ -763,7 +763,7 @@ rpc_address: 0.0.0.0
# be set to 0.0.0.0. If left blank, this will be set to the value of
# rpc_address. If rpc_address is set to 0.0.0.0, broadcast_rpc_address must
# be set.
broadcast_rpc_address: 10.0.5.18
broadcast_rpc_address: 10.0.4.12

# enable or disable keepalive on rpc/native connections
rpc_keepalive: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.workup.jobs;

import com.workup.jobs.commands.JobCommand;
import com.workup.jobs.commands.JobCommandMap;
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.CommandResponse;
import com.workup.shared.commands.controller.*;
import com.workup.shared.enums.ServiceQueueNames;
import com.workup.shared.enums.ThreadPoolSize;
Expand Down Expand Up @@ -77,17 +80,26 @@ public void receive(ContinueRequest in) throws Exception {
@RabbitHandler
public void receive(UpdateCommandRequest in) throws Exception {
try {
String className = commandMap.getCommand(in.getName()).getClass().getName();
System.out.println("Updating command: " + in.getName());
System.out.println("Class: " + className);
Class newClass = new MyClassLoader().loadClass(in.getByteCode(), className);
commandMap.replaceCommand(in.getName(), newClass);
String className = commandMap.getCommand(in.getCommandName()).getClass().getName();
byte[] byteArray = in.getByteCode();
Class<? extends JobCommand<? extends CommandRequest, ? extends CommandResponse>> clazz =
(Class<? extends JobCommand<? extends CommandRequest, ? extends CommandResponse>>)
(new MyClassLoader().loadClass(byteArray, className));
commandMap.replaceCommand(in.getCommandName(), clazz);
System.out.println("Updated command: " + in.getCommandName());
// clazz.newInstance().Run(null);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

static class MyClassLoader extends ClassLoader {
public Class<?> loadClass(byte[] byteCode, String className) {
return defineClass(className, byteCode, 0, byteCode.length);
}
}

private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessException {
ThreadPoolTaskExecutor myBean = context.getBean(ThreadPoolTaskExecutor.class);
Field maxPoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("maxPoolSize");
Expand All @@ -98,10 +110,3 @@ private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessE
corePoolSize.set(myBean, threads);
}
}

class MyClassLoader extends ClassLoader {
public Class<?> loadClass(byte[] byteCode, String className) {
System.out.println("Loading class: " + className);
return defineClass(className, byteCode, 0, byteCode.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CreateJobCommand extends JobCommand<CreateJobRequest, CreateJobResp
@Override
public CreateJobResponse Run(CreateJobRequest request) {

System.out.println("ABDOOOOOO!");
System.out.println("TESTING TOCLASS");
return null;
// logger.info("Information");
// logger.error("Error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ public void receive(ContinueRequest in) throws Exception {

@RabbitHandler
public void receive(UpdateCommandRequest in) throws Exception {
try {
String className = commandMap.getCommand(in.getName()).getClass().getName();
System.out.println("Updating command: " + in.getName());
System.out.println("Class: " + className);
Class newClass = new MyClassLoader().loadClass(in.getByteCode(), className);
commandMap.replaceCommand(in.getName(), newClass);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
// try {
// String className = commandMap.getCommand(in.getName()).getClass().getName();
// System.out.println("Updating command: " + in.getName());
// System.out.println("Class: " + className);
// Class newClass = new MyClassLoader().loadClass(in.getByteCode(), className);
// commandMap.replaceCommand(in.getName(), newClass);
// } catch (Exception e) {
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
}

@RabbitHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@Builder(setterPrefix = "with")
@Jacksonized
public class UpdateCommandRequest {
String name;
String commandName;
byte[] byteCode;
}

0 comments on commit 898175d

Please sign in to comment.