public void closeStreams() { try { if (getStreamsOpen()) { toClient.close(); os.close(); fromClient.close(); is.close(); gui.setLabel("...Streams closed down", Color.RED); log.writeToLog("...Streams closed down"); streamsOpen = false; socket.close(); setDisconnected(); } } catch (IOException e) { gui.setLabel("Could not close streams", Color.RED); gui.setTextArea1("Could not close streams", Color.ORANGE); log.writeToLog("Could not close streams"); } }
// set up streams for communicating with the client protected void openStreams() throws IOException { streamsOpen = true; final boolean AUTO_FLUSH = true; is = socket.getInputStream(); fromClient = new BufferedReader(new InputStreamReader(is)); os = socket.getOutputStream(); toClient = new PrintWriter(os, AUTO_FLUSH); gui.setTextArea1("Streams Open ", Color.ORANGE); log.writeToLog("Streams Open "); // System.out.println("Streams Open "); }
@Override public void run() { try { // loop endlessly waiting for client connections while (true) { if (!streamsOpen) { // wait for a connection request socket = ss.accept(); gui.setTextArea1("Connected to QD on port " + port, Color.ORANGE); gui.setLabel("Connected to QD on port " + port, Color.GREEN); log.writeToLog("Connected to QD on port " + port); openStreams(); setConnected(); processClientRequests(); } else { gui.setTextArea1("Streams already open on port" + port, Color.ORANGE); gui.setLabel("Streams already open on port " + port, Color.GREEN); log.writeToLog("Streams already open on port " + port); closeStreams(); } } } catch (IOException e) { gui.setLabel("Trouble with accepting Socket on port " + port + ": " + e, Color.RED); gui.setTextArea1("Trouble with accepting Socket on port " + port, Color.ORANGE); log.writeToLog("Trouble with accepting Socket on port " + port); closeStreams(); } }
// Constructor public ServerListener(String portNumber, MSeriesGUI theGui) // Start Constructor { gui = theGui; port = Integer.parseInt(portNumber); streamsOpen = false; printerName = gui.getPrinterName(); log = new MLogs(gui, printerName); log.createLogFile(); // establish a ServerSocket try { ss = new ServerSocket(port); gui.setTextArea1("Waiting for connection ... ", Color.ORANGE); log.writeToLog("Waiting for connection ... "); } catch (BindException b) { gui.setLabel("Port " + port + " already in use", Color.RED); gui.setTextArea1("Port " + port + " already in use", Color.ORANGE); log.writeToLog("Port " + port + " already in use"); closeStreams(); } catch (IOException e) { gui.setLabel("Trouble with ServerSocket creation on port " + port + ": " + e, Color.RED); gui.setTextArea1("Trouble with ServerSocket creation on port " + port, Color.ORANGE); log.writeToLog("Trouble with ServerSocket creation on port " + port); } } // End constructor
public void setDisconnected() { gui.setDisconnected(); }
public void setConnected() { gui.setConnected(); }
/** @throws IOException */ public void processClientRequests() throws IOException { byte[] receivedByteArray = new byte[1025]; String stringReceivedByteArray = ""; int iBytesAvailable = 0; int iBytesRead = 0; boolean bOk = true; while (bOk) { iBytesAvailable = is.available(); if (iBytesAvailable > 0) { iBytesRead = is.read(receivedByteArray); // Get next request byte[] bytesReadArray = new byte[iBytesRead]; for (int i = 0; i < iBytesRead; i++) { stringReceivedByteArray = stringReceivedByteArray + String.valueOf(receivedByteArray[i]) + " "; bytesReadArray[i] = receivedByteArray[i]; } String ascii = new String(bytesReadArray, "US-ASCII"); String subascii = ascii.substring(1, bytesReadArray.length - 1); // if recieve <STX>02F0D8<CR> Dip switch 0 read (Left or Right) if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 50 && receivedByteArray[3] == 70 && receivedByteArray[4] == 48) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 50 && receivedByteArray[2] == 70 && receivedByteArray[3] == 48 && receivedByteArray[4] == 68)) { sReplyDescription = "(Dip Switch 0 Read (right or left) )"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } leftRight = gui.getVariant(); ; switch (leftRight) { case "T50L": variantReply = "6P"; break; case "T50R": variantReply = "6P"; break; case "T60L": variantReply = "6P"; break; case "T60R": variantReply = "6P"; break; } sReply0 = "0A2F" + "6P" + "??"; // sReply0 = "0A2F" + leftRightBinary + "??"; byte[] br0 = new byte[sReply0.length() + 2]; byte[] blah = sReply0.getBytes(); br0[0] = 1; for (int i = 0; i < blah.length; i++) { br0[i + 1] = blah[i]; } br0[blah.length + 1] = 13; os.write(br0); if (!gui.getStatusSuppression()) { String asciibr1 = new String(br0, "US-ASCII"); gui.setTextArea1(asciibr1 + " Dip Switch Read 0 - " + "RightPrinter" + ")", Color.GRAY); log.writeToLog(asciibr1 + " Dip Switch Read 0 - " + "RightPrinter" + ")"); // gui.setTextArea1(asciibr1 + " Dip Switch Read 0 - " + variantAnswer + // ")",Color.GRAY); // log.writeToLog(asciibr1 + " Dip Switch Read 0 - " + variantAnswer + ")"); } } // if recieve <STX>02F1D9<CR> Dip switch 1 read (Dots) else if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 50 && receivedByteArray[3] == 70 && receivedByteArray[4] == 49) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 50 && receivedByteArray[2] == 70 && receivedByteArray[3] == 49)) { sReplyDescription = "(Dip Switch 1 Read)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } sDots = gui.getDots(); switch (sDots) { case "6 Dot": sDotReply = "<0"; break; case "8 Dot": sDotReply = "<0"; break; case "12 Dot": sDotReply = "<0"; break; } sReply1 = "0A2F" + "<0" + "??"; byte[] br1 = new byte[sReply1.length() + 2]; byte[] blah = sReply1.getBytes(); br1[0] = 1; for (int i = 0; i < blah.length; i++) { br1[i + 1] = blah[i]; } br1[blah.length + 1] = 13; os.write(br1); if (!gui.getStatusSuppression()) { String asciibr1 = new String(br1, "US-ASCII"); gui.setTextArea1(asciibr1 + " Dip Switch Read 1 - " + sDots + ")", Color.GRAY); log.writeToLog(asciibr1 + " Dip Switch Read 1 - " + sDots + ")"); } } // if recieve <STX>02F7615<CR> Check print head width else if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 50 && receivedByteArray[3] == 70 && receivedByteArray[4] == 55) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 50 && receivedByteArray[2] == 70 && receivedByteArray[3] == 55 && receivedByteArray[4] == 54)) { sReplyDescription = "(Check Print Head Width)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } sReply7 = "0A2F" + sWidth + "??"; byte[] br7 = new byte[sReply7.length() + 2]; byte[] blah = sReply7.getBytes(); br7[0] = 1; for (int i = 0; i < blah.length; i++) { br7[i + 1] = blah[i]; } br7[blah.length + 1] = 13; os.write(br7); if (!gui.getStatusSuppression()) { String asciibr1 = new String(br7, "US-ASCII"); gui.setTextArea1(asciibr1 + " Print Head Width - " + sWidth + ")", Color.GRAY); log.writeToLog(asciibr1 + " Print Head Width - " + sWidth + ")"); } } // if recieve <STX>02F9516<CR> Read Dip Switch 95 else if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 50 && receivedByteArray[3] == 70 && receivedByteArray[4] == 57 && receivedByteArray[5] == 53) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 50 && receivedByteArray[2] == 70 && receivedByteArray[3] == 57 && receivedByteArray[4] == 53)) { sReplyDescription = "(Read Dip Switch 95)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } sReply6 = "0A2F" + "00" + "??"; byte[] br6 = new byte[sReply6.length() + 2]; byte[] blah = sReply6.getBytes(); br6[0] = 1; for (int i = 0; i < blah.length; i++) { br6[i + 1] = blah[i]; } br6[blah.length + 1] = 13; os.write(br6); if (!gui.getStatusSuppression()) { String asciibr1 = new String(br6, "US-ASCII"); gui.setTextArea1(asciibr1 + " Read Dip Switch 95", Color.GRAY); log.writeToLog(asciibr1 + " Read Dip Switch 95"); } } // if recieve <STX>02F9617<CR> Read Dip Switch 96 else if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 50 && receivedByteArray[3] == 70 && receivedByteArray[4] == 57 && receivedByteArray[5] == 54) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 50 && receivedByteArray[2] == 70 && receivedByteArray[3] == 57 && receivedByteArray[4] == 54)) { sReplyDescription = "(Read Dip Switch 96)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } sReply8 = "0A2F" + "50" + "??"; byte[] br8 = new byte[sReply8.length() + 2]; byte[] blah = sReply8.getBytes(); br8[0] = 1; for (int i = 0; i < blah.length; i++) { br8[i + 1] = blah[i]; } br8[blah.length + 1] = 13; os.write(br8); if (!gui.getStatusSuppression()) { String asciibr1 = new String(br8, "US-ASCII"); gui.setTextArea1(asciibr1 + " Read Dip Switch 96", Color.GRAY); log.writeToLog(asciibr1 + " Read Dip Switch 96"); } } // if recieve <STX>00090<CR> Read printer Status else if (receivedByteArray[0] == 48 && receivedByteArray[1] == 48 && receivedByteArray[2] == 48 && receivedByteArray[3] == 57 && receivedByteArray[4] == 48) { sReplyDescription = "(Printer Status Check)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } sMSeriesErrorCode3 = gui.getMSeriesErrorCode(); switch (sMSeriesErrorCode3) { case "999": sMSeriesErrorCodeText = "Printer Ready (no errors)"; break; case "801": sMSeriesErrorCodeText = "Print Head Up"; break; case "802": sMSeriesErrorCodeText = "Print Head Overheating"; break; case "803": sMSeriesErrorCodeText = "Warning - Low level labels"; break; case "804": sMSeriesErrorCodeText = "Warning - Low level film"; break; case "805": sMSeriesErrorCodeText = "No labels"; break; case "806": sMSeriesErrorCodeText = "No film"; break; case "807": sMSeriesErrorCodeText = "5 volt to print head missing"; break; case "808": sMSeriesErrorCodeText = "24 volt to print head missing"; break; case "809": sMSeriesErrorCodeText = "36 volt to stepper motor missing"; break; case "810": sMSeriesErrorCodeText = "ACC (Applicator Control Card) Error"; break; case "812": sMSeriesErrorCodeText = "Bar-code not readable"; break; case "813": sMSeriesErrorCodeText = "F20: No printout or no ribbon has been detected by the extra sensor"; break; case "814": sMSeriesErrorCodeText = "F20: Print head is down in the wrong occasion"; break; case "815": sMSeriesErrorCodeText = "F20: Print head is up in the wrong occasion"; break; case "994": sMSeriesErrorCodeText = "Printing ready, application in progress."; break; case "995": sMSeriesErrorCodeText = "Printer interrupted by head up, print head overheat no labels or no film."; break; case "996": sMSeriesErrorCodeText = "Special"; break; case "997": sMSeriesErrorCodeText = "Reset in progress (Command 01 or 02)"; break; case "998": sMSeriesErrorCodeText = "Printing in progress"; break; } sReply2 = "0A00" + sMSeriesErrorCode3 + "??"; byte[] br2 = new byte[sReply2.length() + 2]; byte[] blah = sReply2.getBytes(); br2[0] = 1; for (int i = 0; i < blah.length; i++) { br2[i + 1] = blah[i]; } br2[blah.length + 1] = 13; os.write(br2); if (!gui.getStatusSuppression()) { String asciibr2 = new String(br2, "UTF-8"); gui.setTextArea1( asciibr2 + " (" + sMSeriesErrorCode3 + " " + sMSeriesErrorCodeText + ")", Color.GRAY); // 0A009997C - 999 Printer ready (no errors) log.writeToLog( asciibr2 + " (" + sMSeriesErrorCode3 + " " + sMSeriesErrorCodeText + ")"); } } // if recieve <STX>03699<CR> Get counters else if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 51 && receivedByteArray[3] == 54 && receivedByteArray[4] == 57 && receivedByteArray[5] == 57) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 51 && receivedByteArray[2] == 54 && receivedByteArray[3] == 57 && receivedByteArray[4] == 57)) { sReplyDescription = "(Get Counters)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } printCount = gui.getPrintCount(); if ("".equals(printCount)) { printCount = "0"; } String countValue = bc.convertValue(printCount); sReply3a = "0A36" + countValue; // sReply3b = "0000505É"; sReply3b = "0000" + countValue; // sReply3c = "0000:07P??"; sReply3c = "0000" + countValue + "??"; byte[] br3 = new byte[1 + sReply3a.length() + 1 + sReply3b.length() + 1 + sReply3c.length() + 1]; byte[] blah1 = sReply3a.getBytes("Cp437"); br3[0] = 1; for (int i = 0; i < blah1.length; i++) { br3[i + 1] = blah1[i]; } br3[blah1.length + 1] = 10; byte[] blah2 = sReply3b.getBytes("Cp437"); for (int i = 0; i < blah2.length; i++) { br3[i + 10] = blah2[i]; } br3[blah1.length + 2 + blah2.length] = 10; byte[] blah3 = sReply3c.getBytes("Cp437"); for (int i = 0; i < blah3.length; i++) { br3[i + 19] = blah3[i]; } br3[blah1.length + 1 + blah2.length + 2 + blah3.length] = 13; os.write(br3); if (!gui.getStatusSuppression()) { String asciibr3 = new String(br3, "Cp437"); gui.setTextArea1(asciibr3 + " (ACK)", Color.GRAY); // ACK log.writeToLog(asciibr3 + " (ACK)"); } } // if recieve <STX>01596<CR> Read Date and Time else if ((receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 49 && receivedByteArray[3] == 53 && receivedByteArray[4] == 57) || (receivedByteArray[0] == 48 && receivedByteArray[1] == 49 && receivedByteArray[2] == 53 && receivedByteArray[3] == 57)) { sReplyDescription = "(Read Date and Time)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } DateFormat df2 = new SimpleDateFormat("yyMMddHHmm"); sReply4 = "0A15" + df2.format(new Date()) + "??"; byte[] br4 = new byte[sReply4.length() + 2]; byte[] blah = sReply4.getBytes(); br4[0] = 1; for (int i = 0; i < blah.length; i++) { br4[i + 1] = blah[i]; } br4[blah.length + 1] = 13; os.write(br4); if (!gui.getStatusSuppression()) { String asciibr4 = new String(br4, "US-ASCII"); DateFormat df3 = new SimpleDateFormat("yyMMdd"); DateFormat df4 = new SimpleDateFormat("HHmm"); gui.setTextArea1( asciibr4 + " (Date " + df3.format(new Date()) + " and Time " + df4.format(new Date()) + ")", Color.GRAY); // 0A15yyMMddHHmm?? log.writeToLog( asciibr4 + " (Date " + df3.format(new Date()) + " and Time " + df4.format(new Date()) + ")"); } } // if recieve <STX>044B??<CR> Set Pause Time to 66 else if ((receivedByteArray[0] == 48 && receivedByteArray[1] == 52 && receivedByteArray[2] == 52 && receivedByteArray[3] == 66) || (receivedByteArray[0] == 2 && receivedByteArray[1] == 48 && receivedByteArray[2] == 52 && receivedByteArray[3] == 52 && receivedByteArray[4] == 66)) { sReplyDescription = "(Set Pause Time to 66)"; if (!gui.getStatusSuppression()) { gui.setTextArea1(subascii + " " + sReplyDescription, Color.DARK_GRAY); log.writeToLog(subascii + " " + sReplyDescription); } sReply5 = "0A440000000000??"; byte[] br5 = new byte[sReply5.length() + 2]; byte[] blah = sReply5.getBytes(); br5[0] = 1; for (int i = 0; i < blah.length; i++) { br5[i + 1] = blah[i]; } br5[blah.length + 1] = 13; os.write(br5); if (!gui.getStatusSuppression()) { String asciibr5 = new String(br5, "US-ASCII"); gui.setTextArea1(asciibr5 + " (ACK)", Color.GRAY); // ACK log.writeToLog(asciibr5 + " (ACK)"); } } // ACK else { sReplyDescription = "(Command)"; gui.setTextArea1(subascii + " " + sReplyDescription, Color.RED); log.writeToLog(subascii + " " + sReplyDescription); int brLength = bytesReadArray.length; brLength = brLength + 2; byte[] br6 = new byte[brLength]; br6[0] = 1; br6[1] = 48; br6[2] = 65; br6[3] = bytesReadArray[2]; br6[4] = bytesReadArray[3]; for (int i = 5; i < br6.length - 3; i++) { br6[i] = bytesReadArray[i - 1]; } br6[brLength - 3] = 63; br6[brLength - 2] = 63; br6[brLength - 1] = 13; os.write(br6); String asciibr6 = new String(br6, "US-ASCII"); gui.setTextArea1(asciibr6 + " (ACK)", Color.BLUE); // ACK log.writeToLog(asciibr6 + " (ACK)"); } stringReceivedByteArray = ""; } else { int i = is.read(); if (-1 == i) { closeStreams(); bOk = false; } } } }