bahareh12
یک شنبه 07 مهر 1387, 12:25 عصر
سلام دوستان هرچی تا حالا سوال کردم یا توی این froum پاک شده یا اصلا جواب ندادید از اساتید جاوا این سوال را دارم که من برنامه ای دارم که client یه message رو میفرسته و برنامه ای به اسم loadbalancer پیغامی که از طرف client میره را دریافت کرده و به serverمیدهد البته بین دو server پخش یا distribute میکند و در آن طرف کار server پیغام را گرفته و جواب را هر دو server به loadbalancer میدهد و اینبار loadbalancer است که پیغام را به client میدهد. حال میخوام بدونم رفتار دو گانه loadbalancer در مواجه شدن با یک client و دو server چگونه است حال چگونه این loadbalancer به صورت mbean در میاد که قابل manage باشه ؟
البته کدی را که نوشتم برای دوستان میزارم تا منو راهنمایی کنید؟
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;
public class udp {
private ServerSocket ServerSocket;
public udp(int port) {
try {
ServerSocket = new ServerSocket(port);
System.out.println("mbean wating to client on port "
+ ServerSocket.getLocalPort());
while (true) {
Socket Socket = ServerSocket.accept();
System.out.println("new client asked for a connection");
TcpThread t = new TcpThread(Socket);
t.start();
}
} catch (IOException e) {
System.out.println("Exception on new ServerSocket:" + e);
}
}
public static void main(String[] args) {
new udp(1500);
}
}
class TcpThread extends Thread {
Socket Socket;
static ObjectInputStream Sinput;
static ObjectOutputStream Soutput;
TcpThread(Socket Socket) {
this.Socket = Socket;
}
public void run() {
System.out
.println("Thread Trying To Creat Object Input/Output Stream ");
try {
Soutput = new ObjectOutputStream(Socket.getOutputStream());
Soutput.flush();
Sinput = new ObjectInputStream(Socket.getInputStream());
} catch (IOException e) {
System.out.println("Exception Creating new Input/Output Stream:"
+ e);
return;
}
System.out.println("Thread wating to string client");
String msg = " Command = Invite " + " Call Id = 5" + " Seq No = [1-20]"
+ " Src Address = client IP:Port"
+ " Dest Address = udpMBean: Port";
System.out.println(" UdpMBean resive \"" + msg + "\" from client");
distribute(msg);
sendMessageserver1(msg);
theClientbehavior();
getResponse(msg);
try {
sendMessageserver2(msg);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Soutput.writeObject(msg);
Soutput.flush();
} catch (IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
}
public static void distribute(String msg){
msg="Command = Invite Call ID = 5 Seq No = [1-20] Src Address =UdpMBean IP :Port DestAddress = serverAddress IP:5070";
int count =+1;
if(count %2==0){
sendMessageserver1(msg);
}
else {
try {
sendMessageserver2(msg);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void sendMessageserver1(String msg) {
String Server1Address = "192.168.1.42:5070";
String msg1 = msg.replaceAll("udpMBean: Port", Server1Address);
msg1 = msg1.replaceAll(" client IP:Port", "udpMBean Ip: Port");
System.out.println(" UdpMBean sending \"" + msg1 + "\" to server1");
}
private static void sendMessageserver2(String msg) throws ClassNotFoundException {
String Server2Address = "192.168.1.79:5070";
String msg1 = msg.replaceAll("udpMBean: Port", Server2Address);
msg1 = msg1.replaceAll(" client IP:Port", "udpMBean Ip: Port");
System.out.println(" UdpMBean sending \"" + msg1 + "\" to server2");
}
private void theClientbehavior() {
// TODO Auto-generated method stub
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
}
private void getResponse(String msg) {
// TODO Auto-generated method stub
msg = " Command = Invite " + " Call Id = 5" + " Seq No = [1-20]"
+ " Src Address = client IP:Port"
+ " Dest Address = udpMBean: Port";
msg=msg.replaceAll( "client IP:Port", "Server1Address 192.168.1.42:5070");
msg= msg.replaceAll("UdpMBean IP :Port","UdpMBean IP :Port" );
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server1 : " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server1 : " );
try{
Sinput.close();
Soutput.close();
}
catch(Exception e1) {
}
}
}
private int port;
private Socket socket;
}
udp همون loadbalancer ماست.
البته کدی را که نوشتم برای دوستان میزارم تا منو راهنمایی کنید؟
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;
public class udp {
private ServerSocket ServerSocket;
public udp(int port) {
try {
ServerSocket = new ServerSocket(port);
System.out.println("mbean wating to client on port "
+ ServerSocket.getLocalPort());
while (true) {
Socket Socket = ServerSocket.accept();
System.out.println("new client asked for a connection");
TcpThread t = new TcpThread(Socket);
t.start();
}
} catch (IOException e) {
System.out.println("Exception on new ServerSocket:" + e);
}
}
public static void main(String[] args) {
new udp(1500);
}
}
class TcpThread extends Thread {
Socket Socket;
static ObjectInputStream Sinput;
static ObjectOutputStream Soutput;
TcpThread(Socket Socket) {
this.Socket = Socket;
}
public void run() {
System.out
.println("Thread Trying To Creat Object Input/Output Stream ");
try {
Soutput = new ObjectOutputStream(Socket.getOutputStream());
Soutput.flush();
Sinput = new ObjectInputStream(Socket.getInputStream());
} catch (IOException e) {
System.out.println("Exception Creating new Input/Output Stream:"
+ e);
return;
}
System.out.println("Thread wating to string client");
String msg = " Command = Invite " + " Call Id = 5" + " Seq No = [1-20]"
+ " Src Address = client IP:Port"
+ " Dest Address = udpMBean: Port";
System.out.println(" UdpMBean resive \"" + msg + "\" from client");
distribute(msg);
sendMessageserver1(msg);
theClientbehavior();
getResponse(msg);
try {
sendMessageserver2(msg);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Soutput.writeObject(msg);
Soutput.flush();
} catch (IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
}
public static void distribute(String msg){
msg="Command = Invite Call ID = 5 Seq No = [1-20] Src Address =UdpMBean IP :Port DestAddress = serverAddress IP:5070";
int count =+1;
if(count %2==0){
sendMessageserver1(msg);
}
else {
try {
sendMessageserver2(msg);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void sendMessageserver1(String msg) {
String Server1Address = "192.168.1.42:5070";
String msg1 = msg.replaceAll("udpMBean: Port", Server1Address);
msg1 = msg1.replaceAll(" client IP:Port", "udpMBean Ip: Port");
System.out.println(" UdpMBean sending \"" + msg1 + "\" to server1");
}
private static void sendMessageserver2(String msg) throws ClassNotFoundException {
String Server2Address = "192.168.1.79:5070";
String msg1 = msg.replaceAll("udpMBean: Port", Server2Address);
msg1 = msg1.replaceAll(" client IP:Port", "udpMBean Ip: Port");
System.out.println(" UdpMBean sending \"" + msg1 + "\" to server2");
}
private void theClientbehavior() {
// TODO Auto-generated method stub
try {
socket = new Socket("localhost", port);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
}
private void getResponse(String msg) {
// TODO Auto-generated method stub
msg = " Command = Invite " + " Call Id = 5" + " Seq No = [1-20]"
+ " Src Address = client IP:Port"
+ " Dest Address = udpMBean: Port";
msg=msg.replaceAll( "client IP:Port", "Server1Address 192.168.1.42:5070");
msg= msg.replaceAll("UdpMBean IP :Port","UdpMBean IP :Port" );
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server1 : " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server1 : " );
try{
Sinput.close();
Soutput.close();
}
catch(Exception e1) {
}
}
}
private int port;
private Socket socket;
}
udp همون loadbalancer ماست.