s/var/const/

tmp
Philippe PITTOLI 2024-06-06 01:07:33 +02:00
parent e20a917409
commit e43e34ffbd
3 changed files with 31 additions and 31 deletions

View File

@ -116,7 +116,7 @@ fn create_service() !void {
// 1. split message
var iterator = std.mem.split(u8, m.payload, ";");
var service_to_contact = iterator.first();
const service_to_contact = iterator.first();
// print("service to contact: {s}\n", .{service_to_contact});
var final_destination: ?[]const u8 = null;
@ -124,8 +124,8 @@ fn create_service() !void {
while (iterator.next()) |next| {
// print("next part: {s}\n", .{next});
var iterator2 = std.mem.split(u8, next, " ");
var sname = iterator2.first();
var target = iterator2.next();
const sname = iterator2.first();
const target = iterator2.next();
if (target) |t| {
// print ("sname: {s} - target: {s}\n", .{sname, t});
if (std.mem.eql(u8, service_to_contact, sname)) {
@ -142,22 +142,22 @@ fn create_service() !void {
print("Connecting to {s} (service requested: {s})\n"
, .{dest, service_to_contact});
var uri = URI.read(dest);
const uri = URI.read(dest);
// 1. in case there is no URI
if (std.mem.eql(u8, uri.protocol, dest)) {
var newfd = try ctx.connect_service (dest);
const newfd = try ctx.connect_service (dest);
send_fd (some_event.origin, "ok", newfd);
try ctx.close_fd (newfd);
}
else if (std.mem.eql(u8, uri.protocol, "unix")) {
var newfd = try ctx.connect_service (uri.address);
const newfd = try ctx.connect_service (uri.address);
send_fd (some_event.origin, "ok", newfd);
try ctx.close_fd (newfd);
}
// 2. else, contact <protocol>d or directly the dest in case there is none.
else {
var servicefd = try ctx.connect_service (uri.protocol);
const servicefd = try ctx.connect_service (uri.protocol);
defer ctx.close_fd (servicefd) catch {};
// TODO: make a simple protocol between IPCd and <protocol>d
// NEED inform about the connection (success or fail)
@ -169,7 +169,7 @@ fn create_service() !void {
var message = try Message.init(servicefd, allocator, dest);
defer message.deinit();
try ctx.write(message);
var response_from_service = try ctx.read_fd(servicefd);
const response_from_service = try ctx.read_fd(servicefd);
if (response_from_service) |r| {
defer r.deinit();
if (std.mem.eql(u8, r.payload, "ok")) {

View File

@ -21,7 +21,7 @@ pub fn main() !u8 {
// The service to contact, either provided with the SERVICE envvar
// or simply using "pong".
var should_free_service_to_contact: bool = true;
var service_to_contact = std.process.getEnvVarOwned(allocator, "SERVICE") catch blk: {
const service_to_contact = std.process.getEnvVarOwned(allocator, "SERVICE") catch blk: {
should_free_service_to_contact = false;
break :blk "pong";
};
@ -30,8 +30,8 @@ pub fn main() !u8 {
allocator.free(service_to_contact);
}
var pongfd = try ctx.connect_ipc(service_to_contact);
var message = try Message.init(pongfd, allocator, "bounce me");
const pongfd = try ctx.connect_ipc(service_to_contact);
const message = try Message.init(pongfd, allocator, "bounce me");
try ctx.schedule(message);
var some_event: ipc.Event = undefined;

View File

@ -19,7 +19,7 @@ const print_eq = ipc.util.print_eq;
const URI = ipc.util.URI;
fn init_tcp_server(allocator: std.mem.Allocator, server: *net.StreamServer) !i32 {
var address = std.process.getEnvVarOwned(allocator, "ADDRESS") catch |err| switch(err) {
const address = std.process.getEnvVarOwned(allocator, "ADDRESS") catch |err| switch(err) {
error.EnvironmentVariableNotFound => blk: {
print ("no ADDRESS envvar: TCPd will listen on 127.0.0.1:9000\n", .{});
break :blk try allocator.dupe(u8, "127.0.0.1:9000");
@ -28,14 +28,14 @@ fn init_tcp_server(allocator: std.mem.Allocator, server: *net.StreamServer) !i32
};
defer allocator.free(address);
var iterator = std.mem.split(u8, address, ":");
var real_tcp_address = iterator.first();
var real_tcp_port = try std.fmt.parseUnsigned(u16, iterator.rest(), 10);
const iterator = std.mem.split(u8, address, ":");
const real_tcp_address = iterator.first();
const real_tcp_port = try std.fmt.parseUnsigned(u16, iterator.rest(), 10);
print ("TCP address [{s}] port [{}]\n", .{real_tcp_address, real_tcp_port});
server.* = net.StreamServer.init(.{.reuse_address = true});
var socket_addr = try net.Address.parseIp(real_tcp_address, real_tcp_port);
const socket_addr = try net.Address.parseIp(real_tcp_address, real_tcp_port);
try server.listen(socket_addr);
const newfd = server.sockfd orelse return error.SocketLOL; // TODO
@ -48,11 +48,11 @@ fn create_service() !void {
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var ctx = try ipc.Context.init(allocator);
const ctx = try ipc.Context.init(allocator);
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
// SERVER SIDE: creating a service.
var service_name = std.process.getEnvVarOwned(allocator, "IPC_SERVICE_NAME") catch |err| switch(err) {
const service_name = std.process.getEnvVarOwned(allocator, "IPC_SERVICE_NAME") catch |err| switch(err) {
error.EnvironmentVariableNotFound => blk: {
print ("no IPC_SERVICE_NAME envvar: TCPd will be named 'tcp'\n", .{});
break :blk try allocator.dupe(u8, "tcp");
@ -84,7 +84,7 @@ fn create_service() !void {
}
};
var sa = os.Sigaction{
const sa = os.Sigaction{
.handler = .{ .sigaction = &S.handler },
.mask = os.empty_sigset, // Do not mask any signal.
.flags = os.SA.SIGINFO,
@ -93,8 +93,8 @@ fn create_service() !void {
// Quit on SIGHUP (kill -1).
try os.sigaction(os.SIG.HUP, &sa, null);
var server: net.StreamServer = undefined;
var serverfd = try init_tcp_server(allocator, &server);
const server: net.StreamServer = undefined;
const serverfd = try init_tcp_server(allocator, &server);
try ctx.add_external (serverfd);
var some_event: ipc.Event = undefined;
@ -125,15 +125,15 @@ fn create_service() !void {
.EXTERNAL => {
print ("Message received from a non IPC socket.\n", .{});
var client = try server.accept(); // net.StreamServer.Connection
const client = try server.accept(); // net.StreamServer.Connection
errdefer client.stream.close();
// Receiving a new client from the EXTERNAL socket.
// New client = new switch from a distant TCP connection to a
// local libipc service.
var buffer: [10000]u8 = undefined;
var size = try client.stream.read(&buffer);
var service_to_contact = buffer[0..size];
const buffer: [10000]u8 = undefined;
const size = try client.stream.read(&buffer);
const service_to_contact = buffer[0..size];
if (service_to_contact.len == 0) {
print("Error, no service provided, closing the connection.\n", .{});
@ -142,7 +142,7 @@ fn create_service() !void {
}
print ("Ask to connect to service [{s}].\n", .{service_to_contact});
var servicefd = ctx.connect_service (service_to_contact) catch |err| {
const servicefd = ctx.connect_service (service_to_contact) catch |err| {
print("Error while connecting to the service {s}: {}.\n"
, .{service_to_contact, err});
print ("Closing the connection.\n", .{});
@ -192,15 +192,15 @@ fn create_service() !void {
defer m.deinit(); // Do not forget to free the message payload.
print ("URI to contact {s}\n", .{m.payload});
var uri = URI.read(m.payload);
const uri = URI.read(m.payload);
print ("proto [{s}] address [{s}] path [{s}]\n"
, .{uri.protocol, uri.address, uri.path});
var iterator = std.mem.split(u8, uri.address, ":");
var real_tcp_address = iterator.first();
var real_tcp_port = try std.fmt.parseUnsigned(u16, iterator.rest(), 10);
const real_tcp_address = iterator.first();
const real_tcp_port = try std.fmt.parseUnsigned(u16, iterator.rest(), 10);
var socket_addr = try net.Address.parseIp(real_tcp_address, real_tcp_port);
const socket_addr = try net.Address.parseIp(real_tcp_address, real_tcp_port);
var stream = try net.tcpConnectToAddress(socket_addr);
errdefer stream.close();
@ -209,7 +209,7 @@ fn create_service() !void {
print ("Writing URI PATH - written, waiting for the final 'ok'.\n", .{});
var buffer: [10000]u8 = undefined;
var size = try stream.read(&buffer);
const size = try stream.read(&buffer);
if (! std.mem.eql(u8, buffer[0..size], "ok")) {
print ("didn't receive 'ok', let's kill the connection\n", .{});
stream.close();