Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filetransfers #41

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/zoffcc/applications/sorm/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ public Message ft_accepted(boolean ft_accepted)

public Message state(int state)
{
Log.i(TAG, "DB:state="+state);
if (this.sql_set.equals(""))
{
this.sql_set = " set ";
Expand Down
136 changes: 120 additions & 16 deletions src/main/java/com/zoffcc/applications/trifa/HelperFiletransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand All @@ -15,12 +16,16 @@
import java.util.Random;

import static com.zoffcc.applications.sorm.OrmaDatabase.sqldb;
import static com.zoffcc.applications.trifa.HelperMessage.set_message_state_from_id;
import static com.zoffcc.applications.trifa.HelperGroup.bytebuffer_to_hexstring;
import static com.zoffcc.applications.trifa.HelperMessage.*;
import static com.zoffcc.applications.trifa.HelperMessage.update_single_message_from_messge_id;
import static com.zoffcc.applications.trifa.MainActivity.*;
import static com.zoffcc.applications.trifa.TRIFAGlobals.*;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_FT_DIRECTION.TRIFA_FT_DIRECTION_INCOMING;
import static com.zoffcc.applications.trifa.ToxVars.TOX_CAPABILITY_DECODE;
import static com.zoffcc.applications.trifa.ToxVars.TOX_FILE_CONTROL.*;
import static com.zoffcc.applications.trifa.ToxVars.TOX_FILE_CONTROL.TOX_FILE_CONTROL_CANCEL;
import static com.zoffcc.applications.trifa.ToxVars.TOX_FILE_ID_LENGTH;
import static com.zoffcc.applications.trifa.ToxVars.TOX_FILE_KIND.TOX_FILE_KIND_DATA;
import static com.zoffcc.applications.trifa.ToxVars.TOX_FILE_KIND.TOX_FILE_KIND_FTV2;

Expand Down Expand Up @@ -582,18 +587,13 @@ static void cancel_filetransfer(long friend_number, long file_number)
// delete FT in DB
// Log.i(TAG, "FTFTFT:002");
delete_filetransfers_from_friendnum_and_filenum(friend_number, file_number);

// update UI
// TODO: updates all messages, this is bad
// update_all_messages_global(false);
try
{
if (f.id != -1)
{
//**//HelperMessage.update_single_message_from_messge_id(msg_id, true);
}
final Message m = TrifaToxService.Companion.getOrma().selectFromMessage().idEq(msg_id).tox_friendpubkeyEq(tox_friend_get_public_key(friend_number)).toList().get(0);
modify_message_with_ft(m, null);
}
catch (Exception e)
catch(Exception e)
{
}
}
Expand Down Expand Up @@ -623,16 +623,13 @@ static void cancel_filetransfer(long friend_number, long file_number)
// delete FT in DB
// Log.i(TAG, "FTFTFT:OGFT:002");
delete_filetransfers_from_friendnum_and_filenum(friend_number, file_number);

// update UI
try
{
if (f.id != -1)
{
//**//HelperMessage.update_single_message_from_messge_id(msg_id, true);
}
final Message m = TrifaToxService.Companion.getOrma().selectFromMessage().idEq(msg_id).tox_friendpubkeyEq(tox_friend_get_public_key(friend_number)).toList().get(0);
modify_message_with_ft(m, null);
}
catch (Exception e)
catch(Exception e)
{
}
}
Expand Down Expand Up @@ -661,7 +658,6 @@ static void cancel_filetransfer(long friend_number, long file_number)
}
}


public static long get_filetransfer_id_from_friendnum_and_filenum(long friend_number, long file_number)
{
try
Expand Down Expand Up @@ -711,4 +707,112 @@ public static void delete_filetransfer_tmpfile(long friend_number, long file_num
e.printStackTrace();
}
}

public static void set_filetransfer_start_sending_from_id(long filetransfer_id)
{
try
{
TrifaToxService.Companion.getOrma().updateFiletransfer().idEq(filetransfer_id).
ft_outgoing_started(true).execute();
}
catch (Exception e)
{
e.printStackTrace();
}
}

static void start_outgoing_ft(Message element)
{
try
{
set_message_queueing_from_id(element.id, false);
// accept FT
set_message_start_sending_from_id(element.id);
set_filetransfer_start_sending_from_id(element.filetransfer_id);

Filetransfer ft = TrifaToxService.Companion.getOrma().selectFromFiletransfer().
idEq(element.filetransfer_id).
orderByIdDesc().toList().get(0);

// update message view
update_single_message_from_messge_id(element.id, ft.filesize ,true);

Log.i(TAG, "MM2MM:8:ft.filesize=" + ft.filesize + " ftid=" + ft.id + " ft.mid=" + ft.message_id + " mid=" +
element.id);

// ------ DEBUG ------
Log.i(TAG, "MM2MM:8a:ft full=" + ft);
// ------ DEBUG ------

ByteBuffer file_id_buffer = ByteBuffer.allocateDirect(TOX_FILE_ID_LENGTH);
MainActivity.tox_messagev3_get_new_message_id(file_id_buffer);

final String file_id_buffer_hex = bytebuffer_to_hexstring(file_id_buffer, true);
Log.i(TAG, "TOX_FILE_ID_LENGTH=" + TOX_FILE_ID_LENGTH + " file_id_buffer_hex=" + file_id_buffer_hex);
ft.tox_file_id_hex = file_id_buffer_hex;

// actually start sending the file to friend
long file_number = -1;
if (TOX_CAPABILITY_DECODE(
tox_friend_get_capabilities(
tox_friend_by_public_key(element.tox_friendpubkey))).ftv2)
{
Log.i(TAG, "TOX_FILE_KIND_FTV2");
file_number = tox_file_send(tox_friend_by_public_key(element.tox_friendpubkey),
TOX_FILE_KIND_FTV2.value, ft.filesize, file_id_buffer, ft.file_name,
ft.file_name.length());
ft.kind = TOX_FILE_KIND_FTV2.value;
element.filetransfer_kind = TOX_FILE_KIND_FTV2.value;
}
else
{
Log.i(TAG, "TOX_FILE_KIND_DATA");
file_number = tox_file_send(tox_friend_by_public_key(element.tox_friendpubkey),
ToxVars.TOX_FILE_KIND.TOX_FILE_KIND_DATA.value, ft.filesize, file_id_buffer,
ft.file_name, ft.file_name.length());
ft.kind = ToxVars.TOX_FILE_KIND.TOX_FILE_KIND_DATA.value;
element.filetransfer_kind = TOX_FILE_KIND_DATA.value;
}
// TODO: handle errors from tox_file_send() here -------

update_message_in_db_filetransfer_kind(element);

// @formatter:off
Log.i(TAG,
"DEBUG_FT:OUT:file_chunk_request:file_number=" +
file_number +
" fn=" + tox_friend_by_public_key(element.tox_friendpubkey) +
" filetransfer_id=" + element.filetransfer_id+
" pk="+element.tox_friendpubkey+
" path_name="+ft.path_name+
" file_name=" + ft.file_name
);
// @formatter:on

if (file_number < 0)
{
Log.i(TAG, "tox_file_send:EE:" + file_number);

// cancel FT
set_filetransfer_state_from_id(element.filetransfer_id, TOX_FILE_CONTROL_CANCEL.value);
set_message_state_from_id(element.id, TOX_FILE_CONTROL_CANCEL.value);
// update message view
update_single_message_from_messge_id(element.id, ft.filesize,true);
}
else
{
Log.i(TAG, "MM2MM:9:new filenum=" + file_number);
// update the tox file number in DB -----------
ft.file_number = file_number;
update_filetransfer_db_full(ft);
// update the tox file number in DB -----------
}
Log.i(TAG, "button_ok:OnTouch:009:f_num=" + file_number);
}
catch (Exception e2)
{
e2.printStackTrace();
Log.i(TAG, "MM2MM:EE1:" + e2.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static incoming_group_file_meta_data handle_incoming_group_file(long group_numbe
GroupMessage m = new GroupMessage();
m.is_new = false;
m.tox_group_peer_pubkey = tox_peerpk;
m.direction = 0; // msg received
m.direction = TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value;
m.TOX_MESSAGE_TYPE = 0;
m.read = false;
m.tox_group_peername = peername;
Expand Down
51 changes: 49 additions & 2 deletions src/main/java/com/zoffcc/applications/trifa/HelperMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ static void send_msgv3_high_level_ack(final long friend_number, String msgV3hash
}
ByteBuffer hash_bytes = HelperGeneric.hexstring_to_bytebuffer(msgV3hash_hex_string);


if (hash_bytes == null) {
return;
} else {
Expand Down Expand Up @@ -93,7 +92,7 @@ public static void set_message_state_from_id(long mid, int state)
try
{
TrifaToxService.Companion.getOrma().updateMessage().idEq(mid).state(state).execute();
// Log.i(TAG, "set_message_state_from_id:message_id=" + message_id + " state=" + state);
Log.i(TAG, "set_message_state_from_id:message_id=" + mid + " state=" + state);
}
catch (Exception e)
{
Expand Down Expand Up @@ -219,5 +218,53 @@ public static void set_message_queueing_from_id(long mid, boolean ft_outgoing_qu
e.printStackTrace();
Log.i(TAG, "set_message_start_queueing_from_id:EE:" + e.getMessage());
}
try
{
Message msg = TrifaToxService.Companion.getOrma().selectFromMessage().idEq(mid).toList().get(0);
if (msg != null)
{
final Filetransfer ft = new Filetransfer();
ft.filesize(0);
if (ft_outgoing_queued == true) {
msg.state = ToxVars.TOX_FILE_CONTROL.TOX_FILE_CONTROL_RESUME.value;
}
Log.i(TAG, "modify_message_with_ft: state="+msg.state);
set_message_state_from_id(msg.id, msg.state);
modify_message_with_ft(msg, ft);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

static void update_message_in_db_filetransfer_kind(final Message m)
{
try
{
TrifaToxService.Companion.getOrma().updateMessage().
idEq(m.id).
filetransfer_kind(m.filetransfer_kind).
execute();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void set_message_start_sending_from_id(long message_id)
{
try
{
TrifaToxService.Companion.getOrma().updateMessage().
idEq(message_id).ft_outgoing_started(true).execute();
}
catch (Exception e)
{
e.printStackTrace();
Log.i(TAG, "set_message_start_sending_from_id:EE:" + e.getMessage());
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ private TRIFA_FT_DIRECTION(int value)

}

public static enum TRIFA_MSG_DIRECTION
{
TRIFA_MSG_DIRECTION_RECVD(0), TRIFA_MSG_DIRECTION_SENT(1) ;

public int value;

private TRIFA_MSG_DIRECTION(int value)
{
this.value = value;
}

}

public static enum TRIFA_MSG_TYPE
{
TRIFA_MSG_TYPE_TEXT(0), TRIFA_MSG_FILE(1);
Expand Down
25 changes: 17 additions & 8 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -770,20 +770,29 @@ fun load_messages_for_friend(selectedContactPubkey: String?)
{
val toxpk = selectedContactPubkey.uppercase()
val uimessages = ArrayList<UIMessage>()
val messages = orma!!.selectFromMessage().tox_friendpubkeyEq(toxpk).orderBySent_timestampAsc().toList()
messages.forEach() { // 0 -> msg received, 1 -> msg sent
val messages = orma!!.selectFromMessage().
tox_friendpubkeyEq(toxpk).orderBySent_timestampAsc().toList()
messages.forEach() {
when (it.direction)
{
0 ->
TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value ->
{
val friendnum = tox_friend_by_public_key(it.tox_friendpubkey.uppercase())
val fname = tox_friend_get_name(friendnum)
val friend_user = User(fname!!, picture = "friend_avatar.png", toxpk = selectedContactPubkey, color = ColorProvider.getColor(false))
uimessages.add(UIMessage(msgDatabaseId = it.id, user = friend_user, timeMs = it.rcvd_timestamp, text = it.text, toxpk = it.tox_friendpubkey.uppercase(), trifaMsgType = it.TRIFA_MESSAGE_TYPE, filename_fullpath = it.filename_fullpath))
uimessages.add(UIMessage(direction = TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value,
user = friend_user, timeMs = it.rcvd_timestamp,
text = it.text, toxpk = it.tox_friendpubkey.uppercase(),
trifaMsgType = it.TRIFA_MESSAGE_TYPE, msgDatabaseId = it.id,
filename_fullpath = it.filename_fullpath, file_state = it.state))
}
1 ->
TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_SENT.value ->
{
uimessages.add(UIMessage(msgDatabaseId = it.id, user = myUser, timeMs = it.sent_timestamp, text = it.text, toxpk = myUser.toxpk, trifaMsgType = it.TRIFA_MESSAGE_TYPE, filename_fullpath = it.filename_fullpath))
uimessages.add(UIMessage(direction = TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_SENT.value,
user = myUser, timeMs = it.sent_timestamp,
text = it.text, toxpk = it.tox_friendpubkey.uppercase(),
trifaMsgType = it.TRIFA_MESSAGE_TYPE, msgDatabaseId = it.id,
filename_fullpath = it.filename_fullpath, file_state = it.state))
}
else ->
{
Expand Down Expand Up @@ -811,7 +820,7 @@ fun load_groupmessages_for_friend(selectedGroupId: String?)
messages.forEach() { // 0 -> msg received, 1 -> msg sent
when (it.direction)
{
0 ->
TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_RECVD.value ->
{
val friend_user = User(it.tox_group_peername + " / " + PubkeyShort(it.tox_group_peer_pubkey), picture = "friend_avatar.png", toxpk = it.tox_group_peer_pubkey.uppercase(), color = ColorProvider.getColor(true, it.tox_group_peer_pubkey.uppercase()))
when (it.TRIFA_MESSAGE_TYPE)
Expand All @@ -823,7 +832,7 @@ fun load_groupmessages_for_friend(selectedGroupId: String?)

}
}
1 ->
TRIFAGlobals.TRIFA_MSG_DIRECTION.TRIFA_MSG_DIRECTION_SENT.value ->
{
uigroupmessages.add(UIGroupMessage(message_id_tox = it.message_id_tox, msgDatabaseId = it.id, user = myUser, timeMs = it.sent_timestamp, text = it.text, toxpk = myUser.toxpk, groupId = it.group_identifier.lowercase(), trifaMsgType = TRIFAGlobals.TRIFA_MSG_TYPE.TRIFA_MSG_TYPE_TEXT.value, filename_fullpath = null))
}
Expand Down
Loading
Loading