Skip to content

Commit

Permalink
Make socket port configurable (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyz32 authored Dec 23, 2024
1 parent 92698c1 commit bbd3b25
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ public class WatchMsgEventSockets extends ManzanRoute {
private final Map<String, String> m_formatMap;
private final Map<String, String> m_destMap;
private final String m_socketIp = "0.0.0.0";
private final String m_socketPort = "8080";
private final String m_socketPort;

public WatchMsgEventSockets(final String _name, final Map<String, String> _formatMap,
final Map<String, String> _destMap) {
super(_name);
m_formatMap = _formatMap;
m_destMap = _destMap;
m_socketPort = getSocketPort();
}

private String getSocketPort() {
String portEnv = System.getenv("MANZAN_SOCKET_PORT");
if (portEnv != null && portEnv.matches("\\d+")) {
return portEnv;
}
return "8080";
}

//@formatter:off
Expand Down
6 changes: 6 additions & 0 deletions docs/config/messaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Socket communication is recommended, because by providing a fallback option to S
* `MANZAN_MESSAGING_PREFERENCE = SQL`: Prefer SQL based communication
* `MANZAN_MESSAGING_PREFERENCE != SQL`: Prefer socket based communication

### Messaging port
By default, Manzan will try to use port 8080 for socket communication, but this can be changed by setting the environment variable `MANZAN_SOCKET_PORT`. Ex.
```cl
ADDENVVAR ENVVAR(MANZAN_SOCKET_PORT) VALUE(8096) LEVEL(*SYS) REPLACE(*YES)
```

### Setting the MANZAN_MESSAGING_PREFERENCE

The `MANZAN_MESSAGING_PREFERENCE` environment variable can be set with the `ADDENVVAR` command. For example, to set the messaging preference to prefer SQL based communication run the command:
Expand Down
4 changes: 4 additions & 0 deletions ile/src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ extern "C" void STRDBG()
{
#ifdef DEBUG_ENABLED
debug_fd = fopen("/tmp/manzan_debug.txt", "a");
if (!debug_fd) {
perror("Failed to open debug file");
}

#endif
}

Expand Down
21 changes: 18 additions & 3 deletions ile/src/pub_db2.sqlc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
memset(dest, 0, sizeof(dest)); \
strncpy(dest, parm ? parm : "", -1 + sizeof(dest));

const int PORT = 8080;
const std::string SERVER = "127.0.0.1";
char* MSG_PREF_SOCKETS = "SOCKETS";
char* MSG_PREF_SQL = "SQL";
Expand Down Expand Up @@ -50,14 +49,30 @@ struct messageParams {
const char* sending_procedure_name;
};

// Function to get the MANZAN_SOCKET_PORT value or return default 8080
int get_socket_port() {
const char *port_env = getenv("MANZAN_SOCKET_PORT");
DEBUG_INFO("Socket port: %s\n", port_env);
if (port_env != NULL) {
char *endptr;
errno = 0; // Reset errno before calling strtol
long port = strtol(port_env, &endptr, 10);
if (errno == 0 && *endptr == '\0' && port > 0 && port <= 65535) {
return (int)port;
}
}
return 8080; // Default port
}

bool send_message(std::string message){
// Create a SocketClient instance
SockClient client;
int port = get_socket_port();

// Open a socket and connect to server
if (!client.openSocket(SERVER, PORT)) {
if (!client.openSocket(SERVER, port)) {
// TODO: How do we want to handle this error? Drop the message, insert into table?
DEBUG_ERROR("Failed to connect to socket: %s:%d\n", SERVER.c_str(), PORT);
DEBUG_ERROR("Failed to connect to socket: %s:%d\n", SERVER.c_str(), port);
return false;
}

Expand Down

0 comments on commit bbd3b25

Please sign in to comment.