0

I am working on a Distributed System , the current state it's more of a client/server application as i'm missing the key part that makes it a distributed system. I don't know how to implement my client thread classs c_thread to pass a 'message' to all the worker threads w_thread that are running.

import java.net.*;
import java.io.*;

public class w_thread extends Thread {

    private Socket socket = null;
    private tracker track = null;
    private int tID;

    //Constructor method
    public w_thread(tracker t, Socket s) {
    super("w_thread");  
    this.track = t;     
    this.socket = s;            
    tID = track.add();
    }

    public void run() {
    try {

        String inputLine, outputLine;
        PrintWriter worker_out;
        BufferedReader worker_in;

        //set up IO to worker
        worker_out = new PrintWriter(socket.getOutputStream(), true);
        worker_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        //Get inputLine from c_thread?

        //...

        /*
        worker_out.close();
        worker_in.close();
        socket.close();
        */

    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

import java.net.*;
import java.io.*;

public class c_thread extends Thread {

    private Socket socket = null;
    private tracker track = null;

    //Constructor method
    public c_thread(tracker t, Socket s) {
    super("c_thread");  
    this.track = t;     
    this.socket = s;        
    }

    public void run() {
    try {

        String inputLine, outputLine;
        PrintWriter client_out;
        BufferedReader client_in;

        //set up IO to client
        client_out = new PrintWriter(socket.getOutputStream(), true);
        client_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        inputLine = client_in.readLine();

        if (track.numOfConnections() == 0) {
        outputLine = "No resources available!";     
        }

        else {      
        //send inputline to all w_threads       
        outputLine = "Resources was available!";        
        }
        client_out.println(outputLine);

        /*
        client_out.close();
        client_in.close();
        //close connection
        socket.close();
        */      

    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

My previous question describes what I'm am trying to implement: Distributed System

Community
  • 1
  • 1
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202

1 Answers1

0

I have solved this problem by storing an array of the w_threads in the shared object tracker.

private ArrayList<w_thread> threadlist = new ArrayList<w_thread>();

Meaning that from the c_thread class I now have access to all the w_threads.

for(int i=0;i<track.numOfConnections();i++) {
     worker = track.get_thread(i);
     worker.sendWork(inputLine);
}
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202