Socket简易分布式的多线程通信

分布式计算中processor与master需要进行通信,两端程序应用了简单的C/S通信模型,一个job在处理时,server进行任务分配,多个client执行完当前任务之后,主动向server请求,server根据schedule algorithm,为client返回任务号,并注明是local task(task需要的data block在内存中)还是remote task(task需要的data block不在内存中)

Server端代码

//server端程序
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class Server {
    private ServerSocket serverSocket;
    private HashSet<Socket> allSockets;
    public static List<Integer> Job = new ArrayList<>();
    public static HashMap<Integer,List<Integer>> ClientMemory = new HashMap<>();
    public static HashMap<Integer,Integer> Local = new HashMap<>();

    public Server(){
        try{
            serverSocket = new ServerSocket(4700);
        }catch (IOException e){
            e.printStackTrace();
        }
        allSockets = new HashSet<Socket>();
    }
    public void startService() throws IOException{
        //ServerThread sT;
        while(true){
            Socket s = serverSocket.accept();
            System.out.println("已接收到Client的请求...");
            allSockets.add(s);
            ServerThread ss = new ServerThread(s);
            ss.start();
            System.out.println(ss.getId());
        }
        //sT.sendMessageToAllClient("Job is finished");
    }
    private class ServerThread extends Thread{
        Socket socket;
        public ServerThread(Socket socket){
            this.socket = socket;
        }
        public void run(){
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                while(true){
                    String str = br.readLine().toString();
                    while(!Job.isEmpty()){
                        if(str == "Apply"){
                            str = br.readLine().toString();
                            int taskId = Job.get(Job.size()-1);
                            int cId = Integer.valueOf(str);
                            if(ClientMemory.get(cId).contains(taskId)){
                                pw.println("local");
                                pw.flush();
                            }else{
                                pw.println("remote");
                                pw.flush();
                                pw.print(Local.get(taskId));
                                pw.flush();
                            }
                            str = br.readLine().toString();
                            if(str == "update"){
                                str = br.readLine().toString();
                                ClientMemory.get(cId).add(Local.get(taskId));
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void sendMessageToAllClient(String message) throws IOException{
            for(Socket s: allSockets){
                PrintWriter pw =new PrintWriter(s.getOutputStream());
                pw.println(message);
                pw.flush();
            }
        }
        public void sendMessageToClient(Socket s,String message) throws IOException{
                PrintWriter pw =new PrintWriter(s.getOutputStream());
                pw.println(message);
                pw.flush();
        }
        //生成给Client的消息
        //如果任务所需data在该Client本地 通知local
        //如果data在其他服务器 告知remote和data位置
        public String assignTaskToClient(int taskId){
            String result = null;
            return result;
        }
        public void updateClientMemory(int ClientId,int BlockId){}
        //判断task需要的data是否在Client内存中
        public boolean isLocal(int taskId){
            return false;
        }
        //返回算法结果中 将分配给Client的任务是哪个
        public int result(){
            int result = 0;
            return result;
        }
    }
    public static void main(String arg[]){
            Server ser = new Server();
        try {
            ser.startService();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Client端代码

//client端程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.in;

public class Client extends Thread{
    private Socket socket;
    private int ClientId;
    //那几块数据在内存
    static List<Integer> Memory = new ArrayList<Integer>();
    //取远程数据,同时要调用更新Memory的函数

    public Client(String host,int port,int clientId)throws UnknownHostException,IOException {
        socket = new Socket(host, port);
        ClientId = clientId;
    }


    //与主机建立连接
    public void connectSer() throws IOException{
        new ClientThread(socket).start();
    }
    private class ClientThread extends Thread{
        Socket socket;
        public ClientThread(Socket socket){
            this.socket = socket;
        }
        public void run(){
            try{
                String message = null;
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                while(br.readLine().toString()!="Finish"){
                    pw.println("Apply");
                    pw.flush();
                    pw.println(ClientId);
                    pw.flush();
                    message = br.readLine().toString();
                    if(message =="local"){
                        ProcessTask(3000);
                    }else if(message == "remote"){
                        message = br.readLine().toString();
                        getData(Integer.valueOf(message));
                        pw.print("update");
                        pw.flush();
                        pw.print(Integer.valueOf(message));
                        pw.flush();
                        ProcessTask(3000);
                    }
                    br.readLine();
                }
                socket.close();
            }catch (IOException e){
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void getData(int serverId){
        System.out.println("Get data from No."+ serverId);
    }
    synchronized public void updateMemory(int BlockId){
        Memory.add(BlockId);
    }
    //模拟执行任务需要的时间
    public void ProcessTask(long taskCost) throws InterruptedException {
        ClientThread.sleep(taskCost);
    }
    //请求一个任务,需要告知Server远程task的data已经拉入本地内存
    synchronized public int ApplyTask(){
        String dataMessage = null;
        int serId = 0;
        return serId;
    }
    public void close(){
        try{
            socket.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main(String arg[]){
        try {
            Client c1 = new Client("127.0.0.1",4700,1);
            Client c2 = new Client("127.0.0.1",4700,2);
            Client c3 = new Client("127.0.0.1",4700,3);
            Client c4 = new Client("127.0.0.1",4700,4);
            Client c5 = new Client("127.0.0.1",4700,5);
            Client c6 = new Client("127.0.0.1",4700,6);
            c1.connectSer();
            c2.connectSer();
            c3.connectSer();
            c4.connectSer();
            c5.connectSer();
            c6.connectSer();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Socket的通信太过麻烦,后期会用其他的框架替换掉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值