I am using a PriorityBlockingQueue in my server program for a network printer manager, but the thing is whenever I print the size of the queue its always 1. I think it is cause i am using threads in my program to support multiple clients. How do i solve this problem? And can I print the queue with out removing the objects in the queue?
The queue contains objects of PrintJob class.
The code for the server is below....
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.Comparator;
public class Server_Frame extends javax.swing.JFrame {
/** Creates new form Server_Frame */
public Server_Frame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Network Printer Manager - Server");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 616, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 370, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[])throws IOException
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run() {
new Server_Frame().setVisible(true);
}
});
Server server = new Server();
server.connect();
}
}
class Server implements Runnable
{
ServerSocket sock;
Socket client = null;
ObjectOutputStream out;
ObjectInputStream in;
public JobQ jq = new JobQ(); //creating an object of the q
int count=0;
int ID;
Server()
{ }
Server(Socket s,int i)
{
this.client = s;
this.ID = i;
}
void connect()
{
try
{
sock = new ServerSocket(5000);
while(true)
{
client = sock.accept();
JOptionPane.showMessageDialog(null,"Client Connected","Connected",JOptionPane.INFORMATION_MESSAGE);
//PrintJob pj = new PrintJob();
Runnable runnable = new Server(client, ++count);
Thread thread = new Thread(runnable);
thread.start();
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
}
public void run()
{
try
{
out = new ObjectOutputStream(client.getOutputStream());
out.flush();
in = new ObjectInputStream(client.getInputStream());
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
String u = (String)recieve_from_client();
System.out.println(u);
String p = (String)recieve_from_client();
System.out.println(p);
String flag = authenticate(u, p);
System.out.print(flag);
send_to_client(flag);
if(flag.equals("false"))
{
try
{
client.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
}
get_Job();
jq.PrintQ(); //printing the q
}
void send_to_client(Object s)
{
try
{
out.writeObject(s);
out.flush();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
}
Object recieve_from_client()
{
Object s = null;
try
{
s = in.readObject();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
return s;
}
String authenticate(String usr ,String pwd)
{
String n=null,p=null;
try
{
FileReader fn=new FileReader("username.txt");
BufferedReader fnbr=new BufferedReader(fn);
FileReader fp=new FileReader("password.txt");
BufferedReader fpbr=new BufferedReader(fp);
while(((n=fnbr.readLine())!=null)&&(p=fpbr.readLine())!=null)
{
if(n.equals(usr))
{
if(p.equals(pwd))
return "true";
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
return "false";
}
void get_Job()
{
PrintJob pj = new PrintJob();
pj.file_name = (String)this.recieve_from_client();
System.out.println(pj.file_name);
String temp=" ";
try
{
FileWriter fw = new FileWriter(pj.file_name);
BufferedWriter fbw = new BufferedWriter(fw);
while(!(temp=(String)this.recieve_from_client()).equals("stop"))
{
//temp = this.recieve_from_client();
fbw.write(temp);
fbw.newLine();
fbw.flush();
System.out.println(temp);
}
fbw.close();
fw.close();
pj.priority = (Integer)this.recieve_from_client();
System.out.println(pj.priority);
pj.no_of_copies = (Integer)this.recieve_from_client();
System.out.println(pj.no_of_copies);
jq.insertJob(pj);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e,"SError",JOptionPane.ERROR_MESSAGE);
}
}
}
class PrintJob
{
String file_name;
int priority;
Object no_of_copies;
PrintJob()
{ }
}
class JobQ
{
PriorityBlockingQueue<PrintJob> pq =null;
Comparator <Object> comparator = new PriorityComparer();
JobQ()
{
pq = new PriorityBlockingQueue(20,comparator);
}
void insertJob(PrintJob job)
{
pq.add(job);
}
void PrintQ()
{
PrintJob j=null;
PriorityBlockingQueue<PrintJob> q = new PriorityBlockingQueue();
int i;
System.out.println(pq.size()); //printing the size
/* while(!pq.isEmpty()) //Trying to print q...will this work?
{
for(i=0;i<pq.size();i++)
{
j=pq.poll();
System.out.println(j.priority);
q.add(j);
}
}
while(!q.isEmpty())
{
for(i=0;i<q.size();i++)
{
j=q.poll();
//System.out.println(j.priority);
pq.add(j);
}
}*/
}
}
class PriorityComparer implements Comparator<Object>
{
public int compare(Object j1, Object j2)
{
int p1 = ((PrintJob)j1).priority;
int p2 = ((PrintJob)j2).priority;
if(p1>p2)
return 1;
else if(p1<p2)
return -1;
else
return 0;
}
}