📄 subnode.java
字号:
package com.node;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import com.fileAccess.FileAccess;
class SubThread extends Thread {
private Socket s = null;
private SubNode sn = null;
public SubThread(Socket s, SubNode sn) {
this.s = s;
this.sn = sn;
}
public void run() {
System.out.println("you get start" + this);
InputStream is = null;
try {
is = s.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while (true) {
try {
line = br.readLine();
if (line != null) {
System.out.println("计数" + line);
String sl[]=line.split(":");
if (sl[0].equals("a")) {
sn.aCount(line);// 写入下一跳
} else {
sn.totalCount(line);
}
line = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class SubNode extends Thread {
private boolean stopflag;
private int selfPort;// 自链接控制端口
private int nextPort;
private String nextIp;
private PrintWriter pw;
private String sname;
public SubNode(int selfPort, int nextPort, String nextIp, String sname) {
stopflag = false;
this.selfPort = selfPort;
this.nextPort = nextPort;
this.nextIp = nextIp;
this.sname = sname;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
private PrintWriter getPw() {
return pw;
}
public boolean isStopflag() {
return stopflag;
}
public void setStopflag(boolean stopflag) {
this.stopflag = stopflag;
}
public int getSelfPort() {
return selfPort;
}
public void setSelfPort(int selfPort) {
this.selfPort = selfPort;
}
public int getNextPort() {
return nextPort;
}
public void setNextPort(int nextPort) {
this.nextPort = nextPort;
}
public String getNextIp() {
return nextIp;
}
public void setNextIp(String nextIp) {
this.nextIp = nextIp;
}
private void setPw(PrintWriter pw) {
this.pw = pw;
}
synchronized public void aCount(String str) {
// setA_count(getA_count() + 1);
PrintWriter pw = getPw();
if (pw != null) {
pw.println(str + sname + "->");
System.out.println("printed a");
} else {
System.out.println("can not pw");
}
}
public void initPw() {
Socket s = null;
OutputStream os = null;
try {
System.out.println("in pw()");
s = new Socket(this.nextIp, this.nextPort);
os = s.getOutputStream();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (s != null) {
this.pw = new PrintWriter(os, true);
System.out.println("you get the pw for next " + pw.toString());
} else {
System.out.println("get next socket error! nextPort:"
+ this.nextPort);
}
}
synchronized public void totalCount(String str) {
PrintWriter pw = getPw();
if (pw != null) {
pw.println(str + sname + "->");
System.out.println("printed t");
} else {
System.out.println("can not pw");
}
}
public void run() {
ServerSocket mss = null;
try {
mss = new ServerSocket(selfPort);
} catch (IOException e) {
e.printStackTrace();
}
initPw();
System.out.println("initpw over");
if (mss != null) {
stopflag = false;
while (!stopflag) {
Socket s = null;
try {
s = mss.accept();
if (s != null) {
System.out.println("get connection" + this);
SubThread st = new SubThread(s, this);
st.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -