Profile logic for UI
This commit is contained in:
@@ -10,6 +10,7 @@ import org.to.telegramfinalproject.Models.SearchRequestModel;
|
||||
import org.to.telegramfinalproject.Models.SearchResultModel;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -26,6 +27,8 @@ public class ActionHandler {
|
||||
private final Scanner scanner;
|
||||
public static volatile boolean forceExitChat = false;
|
||||
public static ActionHandler instance;
|
||||
private final DataOutputStream outBin; // NEW
|
||||
|
||||
|
||||
//use for UI
|
||||
private volatile String lastStatus = "error"; // success | error
|
||||
@@ -57,14 +60,23 @@ public class ActionHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public ActionHandler(PrintWriter out, BufferedReader in, Scanner scanner) {
|
||||
|
||||
public ActionHandler(PrintWriter out, BufferedReader in, DataOutputStream outBin, Scanner scanner) {
|
||||
this.out = out;
|
||||
this.in = in;
|
||||
this.in = in;
|
||||
this.outBin = outBin; // NEW
|
||||
this.scanner = scanner;
|
||||
ActionHandler.instance = this;
|
||||
|
||||
}
|
||||
|
||||
// public ActionHandler(PrintWriter out, BufferedReader in, Scanner scanner) {
|
||||
// this.out = out;
|
||||
// this.in = in;
|
||||
// this.scanner = scanner;
|
||||
// ActionHandler.instance = this;
|
||||
//
|
||||
// }
|
||||
|
||||
public void login(String username , String password){
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "login");
|
||||
|
||||
@@ -39,16 +39,31 @@ public class IncomingMessageListener implements Runnable {
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
if (TelegramClient.mediaBusy.get()) {
|
||||
try { Thread.sleep(15); } catch (InterruptedException ignored) {}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
JSONObject response = new JSONObject(line);
|
||||
System.out.println("📥 Received raw line: " + line);
|
||||
|
||||
//if it has reqID answer
|
||||
String mid = response.optString("message_id", "");
|
||||
if (!mid.isEmpty()) {
|
||||
BlockingQueue<JSONObject> q = TelegramClient.pendingResponses.get(mid);
|
||||
if (q != null) {
|
||||
q.put(response);
|
||||
continue; // این پیام مصرف شد
|
||||
}
|
||||
}
|
||||
|
||||
if (response.has("request_id")) {
|
||||
String requestId = response.getString("request_id");
|
||||
System.out.println("📬 Response with request_id: " + requestId);
|
||||
System.out.println("📬 Full response: " + response.toString(2));
|
||||
|
||||
|
||||
BlockingQueue<JSONObject> queue = TelegramClient.pendingResponses.get(requestId);
|
||||
if (queue != null) {
|
||||
queue.put(response);
|
||||
|
||||
@@ -128,10 +128,7 @@ package org.to.telegramfinalproject.Client;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
@@ -158,6 +155,14 @@ public class TelegramClient {
|
||||
public static final BlockingQueue<JSONObject> responseQueue = new LinkedBlockingQueue<>();
|
||||
public static final Map<String, BlockingQueue<JSONObject>> pendingResponses = new ConcurrentHashMap<>();
|
||||
public static UUID loggedInUserId = null;
|
||||
private DataInputStream inBin; // NEW
|
||||
private DataOutputStream outBin; // NEW
|
||||
private static SocketMediaDownloader downloader; // NEW
|
||||
public static final java.util.concurrent.atomic.AtomicBoolean mediaBusy = new java.util.concurrent.atomic.AtomicBoolean(false); // NEW
|
||||
|
||||
public static SocketMediaDownloader getDownloader() { return downloader; }
|
||||
public DataInputStream getInBin() { return inBin; }
|
||||
public DataOutputStream getOutBin() { return outBin; }
|
||||
|
||||
private volatile boolean listenerStarted = false;
|
||||
|
||||
@@ -219,12 +224,26 @@ public class TelegramClient {
|
||||
socket = new Socket(SERVER_HOST, SERVER_PORT);
|
||||
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
|
||||
out = new PrintWriter(socket.getOutputStream(), true);
|
||||
|
||||
InputStream rawIn = socket.getInputStream();
|
||||
OutputStream rawOut = socket.getOutputStream();
|
||||
|
||||
in = new BufferedReader(new InputStreamReader(rawIn, java.nio.charset.StandardCharsets.UTF_8));
|
||||
out = new PrintWriter(new OutputStreamWriter(rawOut, java.nio.charset.StandardCharsets.UTF_8), true);
|
||||
|
||||
// NEW: binary streams
|
||||
inBin = new DataInputStream(rawIn);
|
||||
outBin = new DataOutputStream(rawOut);
|
||||
|
||||
// NEW: media downloader helper
|
||||
downloader = new SocketMediaDownloader(out, inBin, outBin);
|
||||
|
||||
System.out.println("✅ Connected to Telegram Server");
|
||||
}
|
||||
|
||||
private synchronized void initHandlerIfNeeded() {
|
||||
if (handler == null) {
|
||||
handler = new ActionHandler(out, in, scanner);
|
||||
handler = new ActionHandler(out, in, outBin, scanner);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user