[Help] Use OPCUA server to work with infinite loop

Hello I am trying to create lot of nodes from the messages receiving from tcp server. I have an issue for borrowing the server variable. I am newbie to rust as well. Can someone help to resolve the issue. I have continuous loop in the one of the rust file so opc server never gets start.

Module: simulator


mod client;

fn main() {


use std::os::unix::net::UnixDatagram;
use std::{thread, time};
use serde_json::json;

pub fn tcp_datagram_client() {
    pub static FILE_PATH: &'static str = "/tmp/socket.sock";
    let socket = UnixDatagram::unbound().unwrap();
    match socket.connect(FILE_PATH) {
        Ok(socket) => socket,
        Err(e) => {
            println!("Couldn't connect: {:?}", e);
    println!("TCP client Connected to TCP Server {:?}", socket);
    loop {
        let message_payload = json!({
            "name": "NJ/Mercer/Pennington/street1",
            "status": "close",
        socket.send(message_payload.to_string().as_bytes()).expect("recv function failed");

Module: src


mod server;
mod process_tcp_messages;
mod opc_server;
mod global_nodes;

fn main() {


in this code is there a better way I can use server variable, if I try to create a thread it gives error of can't borrow moved variable.

use opcua_server::prelude::*;
use std::path::PathBuf;
use std::thread;
use std::borrow::BorrowMut;

use crate::global_opc_nodes;
use crate::process_tcp_messages;

pub fn run_opc_server() {
    let mut server = Server::new(ServerConfig::load(&PathBuf::from("/tmp/server.conf")).unwrap());

    let ns = {
        let address_space = server.address_space();
        let mut address_space = address_space.write().unwrap();

    global_nodes::create_global_variables(&mut server, ns);
    process_incoming_tcp_messages::run(&mut server, ns);

    println!("OPC Server stared and running...");



use opcua_server::prelude::*;
use std::path::PathBuf;
use std::thread;

static ROOT_NODE_PATH: &'static str = "OPC UA SAMPLE SERVER";

pub fn create_global_variables(server: &mut Server, ns: u16) {
    let address_space = server.address_space();
    let mut address_space = address_space.write().unwrap();

    let metrics_folder = address_space.add_folder("Folder",
    let test_node = NodeId::new(ns, UAString::from("metrics"));
    let _ = address_space.add_variables(
            Variable::new(&test_node, "metrics", "metrics", 0 as i32),
        ], &metrics_folder,



I have a infinite loop here to process tcp server messages received from client. I try to put this code in thread but did not work , may be I am not doing proper threading. Based on the received message, I will create nodes and update its values continuously. Currently that function is comment out.

use std::thread;
use std::sync::mpsc;
use serde_json::{Value, from_str};
use opcua_server::server::Server;
use std::ops::DerefMut;
use std::borrow::BorrowMut;

use crate::unix_datagram_server;

static ROOT_NODE_PATH: &'static str = "OPC UA SAMPLE SERVER";

pub fn run(server: &mut Server, ns: u16) {
    let (tx_to_opc_server, rx_from_opc_server) = mpsc::channel();
    thread::spawn(|| unix_datagram_server::tcp_datagram_server(tx_to_opc_server));
    println!("TCP server started...");
    loop {
        let message_from_tcp_server = rx_from_opc_server.recv().unwrap();
        let closing_bracket_index = message_from_tcp_server.
            position(|&x| x == b'}').
            map(|i| i + 1).
            unwrap_or_else(|| message_from_tcp_server.len());
        let message_json: Value = serde_json::from_str(&message_from_tcp_server[..closing_bracket_index]).unwrap();
        let mut path = message_json["name"].clone().to_string();
        path = path.replace("\"", "");
        let tag_to_update = ROOT_NODE_PATH.to_owned() + &*path;
        let split_path: Vec<&str> = path.as_str().split("/").collect(); // this gives iterator, convert this to vector using collect()
        let tag_status = message_json["status"].clone();
        let tag_value = message_json["value"].clone();
        println!("{:?}, {:?}, {}, {}", tag_to_update, split_path, tag_status, tag_value);
        // create_opc_nodes::create_nodes_recursively(server.borrow_mut(), split_path, tag_value, ns);

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.